Skip to content

Commit

Permalink
refactor coloring, make file controller, add banner
Browse files Browse the repository at this point in the history
  • Loading branch information
juhakivekas committed Dec 10, 2017
1 parent 418fa1f commit 149c0d7
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 77 deletions.
20 changes: 0 additions & 20 deletions add_diff_dump.py

This file was deleted.

36 changes: 25 additions & 11 deletions mdcli.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
#!/usr/bin/python3
import argparse
from multidiff import MultidiffModel, StreamView, SocketController, FileController

from multidiff import MultidiffModel, StreamView, SocketController
import sys

parser = argparse.ArgumentParser(description='The multidiff tool of the future.')
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description="""
\x1b[1mN E O N S E N S E\x1b[0m
augmentations inc
┌───────────────┐
\x1b[1mM U L T I\x1b[0m │
\x1b[1mD I F F\x1b[0m │
│ sensor module │
└───────────────┘
""")

parser.add_argument('file',
metavar='file',
type=str,
nargs='*',
help='a file or directory whose content is to be included in the multidiff')
help='file or directory to include in multidiff')

parser.add_argument('-m', '--mode',
dest='mode',
default='sequence',
help='mode of operation, either "baseline" or "sequence"')

parser.add_argument('-e','--encode',
dest='encode',
default='hexdump',
help='choose encoding of the data. One of "hex", "hexdump", or "utf8"')
help='encoding of the output data, one of "hex", "hexdump", or "utf8"')

parser.add_argument('-p','--port',
dest='port',
default=0,
type=int,
help='start a local socket server on a port')

if __name__ == '__main__':
args = parser.parse_args()
m = MultidiffModel()
v = StreamView(m, encoding=args.encode)
#f = FileController(m)
#f.add_files(args.files)
s = SocketController(('127.0.0.1', args.port), m)
s.serve_forever()
f = FileController(m)
f.add_paths(args.file)
if args.port != 0:
s = SocketController(('127.0.0.1', args.port), m)
s.serve_forever()
7 changes: 7 additions & 0 deletions multidiff/Ansi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Ansi:
reset = '\x1b[0m'
bold = '\x1b[1m'
on_red = '\x1b[41m'
on_green = '\x1b[42m'
on_blue = '\x1b[44m'
white = '\x1b[37m'
19 changes: 19 additions & 0 deletions multidiff/FileController.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os

class FileController:
def __init__(self, model):
self.model = model

def add_paths(self, paths):
for path in paths:
self.add_path(path)

def add_path(self, path):
if os.path.isdir(path):
for name in os.listdir(path):
self.add_path(os.path.join(path, name))
elif os.path.isfile(path):
data = open(path, 'rb').read(-1)
self.model.add(data, name=path)
else:
raise NotImplemented
9 changes: 4 additions & 5 deletions multidiff/StreamView.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from multidiff import Render
from multidiff import Render, Ansi

class StreamView():
def __init__(self, model, encoding='hexdump'):
Expand All @@ -7,12 +7,11 @@ def __init__(self, model, encoding='hexdump'):
model.add_listener(self)

def object_added(self, index):
if index == 0:
return
self.model.diff(index - 1, index)
if index > 0:
self.model.diff(index - 1, index)

def diff_added(self, diff):
print(self.model.objects[diff.target].name)
print(Ansi.bold + self.model.objects[diff.target].name + Ansi.reset)
print(self.render.render(self.model, diff))
#clean up model to not leak memory in long runs
del(self.model.diffs[0])
Expand Down
17 changes: 2 additions & 15 deletions multidiff/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
"""
The tool of future
___________________
| |
| M U L T I |
| |
| D I F F |
|___________________|
i n t e l l i g e c e
"""

from .multidiffmodel import MultidiffModel
from .render import Render, HexdumpEncoder
from .Ansi import Ansi
from .baselineview import BaselineView
from .SocketController import SocketController
from .StreamView import StreamView
from .FileController import FileController
10 changes: 5 additions & 5 deletions multidiff/render.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from multidiff.Ansi import Ansi
import binascii
import termcolor
import html

class Render():
Expand Down Expand Up @@ -130,12 +130,12 @@ def ansi_colored(string, op):
if op == 'equal':
return string
elif op == 'replace':
color = 'blue'
color = Ansi.on_blue
elif op == 'insert':
color = 'green'
color = Ansi.on_green
elif op == 'delete':
color = 'red'
return termcolor.colored(string, 'white', 'on_{}'.format(color))
color = Ansi.on_red
return Ansi.white + color + string + Ansi.reset
#return termcolor.colored(string, color, None)

def html_colored(string, op):
Expand Down
24 changes: 15 additions & 9 deletions test/hexdump_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from multidiff.render import *
from multidiff import Ansi

def mono(string, color):
return string
Expand All @@ -14,15 +15,20 @@ def test_multiple_line_mono():
hd = HexdumpEncoder(mono)
hd.append(bytes("0123456789abcdef012345678", "utf8"), "mono")
result = hd.final()
dump='000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|\n000010: 30 31 32 33 34 35 36 37 38 |012345678 |'
dump = '000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66 |0123456789abcdef|\n'
dump+= '000010: 30 31 32 33 34 35 36 37 38 |012345678 |'
assert(result == dump)

def test_single_line_insert():
hd = HexdumpEncoder(ansi_colored)
hd.append(bytes("foobar", "utf8"), "insert")
result = hd.final()
print(result)
dump = '000000: \x1b[42m\x1b[37m66 6f 6f 62 61 72\x1b[0m |\x1b[42m\x1b[37mfoobar\x1b[0m |'
dump = '000000: '
dump+= Ansi.white + Ansi.on_green + '66 6f 6f 62 61 72' + Ansi.reset
dump+= ' |'
dump+= Ansi.white + Ansi.on_green + 'foobar' + Ansi.reset
dump+= ' |'
assert(result == dump)

def test_multiple_line_insert():
Expand All @@ -32,13 +38,13 @@ def test_multiple_line_insert():
result = hd.final()
#jesus!
dump = '000000: 30 31 32 33 34 35 36 37 38 61 '
dump += '\x1b[44m\x1b[37m30 31 32 33 34 35\x1b[0m'
dump += Ansi.white + Ansi.on_blue + '30 31 32 33 34 35' + Ansi.reset
dump += ' |012345678a'
dump += '\x1b[44m\x1b[37m012345\x1b[0m'
dump += Ansi.white + Ansi.on_blue + '012345' + Ansi.reset
dump += '|\n000010: '
dump += '\x1b[44m\x1b[37m36 37 38 61\x1b[0m'
dump += Ansi.white + Ansi.on_blue + '36 37 38 61' + Ansi.reset
dump += ' |'
dump += '\x1b[44m\x1b[37m678a\x1b[0m'
dump += Ansi.white + Ansi.on_blue + '678a' + Ansi.reset
dump += ' |'
assert(result == dump)

Expand All @@ -49,9 +55,9 @@ def test_empty_data_insert():
result = hd.final()
print(result)
dump = '000000: '
dump += '\x1b[44m\x1b[37m41\x1b[0m'
dump += '\x1b[41m\x1b[37m \x1b[0m'
dump += Ansi.white + Ansi.on_blue + '41' + Ansi.reset
dump += Ansi.white + Ansi.on_red + ' ' + Ansi.reset
dump += ' |'
dump += '\x1b[44m\x1b[37mA\x1b[0m'
dump += Ansi.white + Ansi.on_blue + 'A' + Ansi.reset
dump += ' |'
assert(result == dump)
10 changes: 5 additions & 5 deletions test/minimal_res.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
foobar
foobag
geebar
fooxbar
fobar

foobag
geebar
fooxbar
fobar

14 changes: 7 additions & 7 deletions test/pretty_json_res.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"d": {
"r": 454,
"r": 454,
"b": {
"p": "/ps/6W1HJ648/chats/-KpBNFrYEBtFQ41l9wJg",
"p": "/ps/6W1HJ648/chats/-KpBNFrYEBtFQ41l9wJg",
"d": {
"message": "a",
"timestamp": "2017-07-16T17:10:15.875Z"
"timestamp": "2017-07-16T17:10:15.875Z"
}
}
}
}
{
"d": {
"r": 451,
"r": 451,
"b": {
"p": "/ps/6W1HJ648/chats/-KpBNFIYZdU8KGwTKf2T",
"p": "/ps/6W1HJ648/chats/-KpBNFIYZdU8KGwTKf2T",
"d": {
"message": "a",
"timestamp": "2017-07-16T17:10:13.570Z"
"timestamp": "2017-07-16T17:10:13.570Z"
}
}
}
}
}


0 comments on commit 149c0d7

Please sign in to comment.