tree_repr
This commit is contained in:
parent
7b1b57dc59
commit
4c0ee4e244
Binary file not shown.
@ -2,21 +2,13 @@
|
||||
|
||||
def compute_diff(path_filea, path_fileb):
|
||||
'''
|
||||
- a
|
||||
|
||||
hhhh
|
||||
iiii
|
||||
jjjj
|
||||
eee
|
||||
it returns binary diff line per line of two files,
|
||||
it is basically a list that contains:
|
||||
- blocks (lists) with the same lines
|
||||
- blocks of diffs, item with two lists
|
||||
|
||||
|
||||
|
||||
- b
|
||||
|
||||
nnnn
|
||||
llll
|
||||
jjjjj
|
||||
eee
|
||||
[[['hhhh\n', 'iiii\n', 'jjjj\n', 'eee\n', '\n'], ['cio cio cio cio cio ciao\n', ' cj cjcj\n', 'llll\n', 'jjjj\n', 'eee\n']], ['\n', '\n', '\n', '\n']]
|
||||
differente lines same lines
|
||||
|
||||
'''
|
||||
with open(path_filea, 'r') as fa:
|
||||
@ -60,7 +52,7 @@ def compute_diff(path_filea, path_fileb):
|
||||
|
||||
return out
|
||||
|
||||
def txt_diff(path_filea, path_fileb):
|
||||
def format_diff(path_filea, path_fileb):
|
||||
ldiff = compute_diff(path_filea, path_fileb)
|
||||
|
||||
tdiff = ''
|
||||
@ -81,6 +73,9 @@ def txt_diff(path_filea, path_fileb):
|
||||
tdiff += '\n'
|
||||
continue
|
||||
|
||||
if block[0] == '\n' and tdiff.endswith('%s\n' % (sep,)):
|
||||
block = block[1:]
|
||||
|
||||
tdiff += ''.join(block)
|
||||
|
||||
print(tdiff)
|
||||
|
Binary file not shown.
@ -1,16 +1,23 @@
|
||||
import os
|
||||
|
||||
LEVEL_INDENT = 6
|
||||
EMPTY_TREE_LINE_LENGTH = 160
|
||||
EMPTY_TREE_LINE = ' ' * EMPTY_TREE_LINE_LENGTH
|
||||
|
||||
VERTICAL_CONNECTOR = '│'
|
||||
### same length
|
||||
CHILD_CONNECTOR = '├── '
|
||||
LAST_CHILD_CONNECTOR = '└── '
|
||||
### is the length of child connector and last_child_connector
|
||||
LEVEL_INDENT = 4
|
||||
ITEMS_TO_EXCLUDE = ['__pycache__']
|
||||
|
||||
def mount_str_at(source_str, index, mount_str):
|
||||
l = len(mount_str)
|
||||
return source_str[:index] + mount_str + source_str[index+l:]
|
||||
|
||||
def level2indent(l):
|
||||
if not l:
|
||||
return 1
|
||||
return 0
|
||||
|
||||
return LEVEL_INDENT * l
|
||||
|
||||
@ -28,7 +35,32 @@ def produce_treeline(prec_seps, lsep):
|
||||
|
||||
return tree_line.rstrip()
|
||||
|
||||
def nerd_tree(startpath, prec_seps=[]):
|
||||
def nerd_tree_sort_dirfirst(startpath):
|
||||
files = []
|
||||
dirs = []
|
||||
|
||||
for item in os.listdir(startpath):
|
||||
if os.path.isdir(startpath + item):
|
||||
dirs.append(item)
|
||||
continue
|
||||
files.append(item)
|
||||
|
||||
return dirs + files
|
||||
|
||||
def nerd_tree_sort(startpath, dirfirst=True):
|
||||
'''
|
||||
it returns the sorted list of items starting from startpath
|
||||
'''
|
||||
|
||||
# dirs = []
|
||||
# files = []
|
||||
|
||||
if dirfirst:
|
||||
return nerd_tree_sort_dirfirst(startpath)
|
||||
|
||||
return os.listdir(startpath)
|
||||
|
||||
def OLDnerd_tree(startpath, prec_seps=[]):
|
||||
if not startpath.endswith('/'):
|
||||
tosplit = startpath
|
||||
startpath = startpath + '/'
|
||||
@ -36,21 +68,24 @@ def nerd_tree(startpath, prec_seps=[]):
|
||||
tosplit = startpath[:-1]
|
||||
|
||||
rootnode = tosplit.split('/')[-1]
|
||||
items = os.listdir(startpath)
|
||||
|
||||
nerd_tree_txt = rootnode
|
||||
items = nerd_tree_sort(startpath, dirfirst=True)
|
||||
|
||||
## rootnode is always a dir -> colorize
|
||||
nerd_tree_txt = 'd_' + rootnode
|
||||
|
||||
for n, item in enumerate(items):
|
||||
if item == '__pycache__':
|
||||
if item in ITEMS_TO_EXCLUDE:
|
||||
continue
|
||||
|
||||
islast = (n==len(items) -1)
|
||||
sep = '├── '
|
||||
|
||||
sep = CHILD_CONNECTOR
|
||||
if islast:
|
||||
sep = '└── '
|
||||
sep = LAST_CHILD_CONNECTOR
|
||||
|
||||
if not islast:
|
||||
psep_char = '│'
|
||||
psep_char = VERTICAL_CONNECTOR
|
||||
else:
|
||||
psep_char = ''
|
||||
|
||||
@ -65,3 +100,118 @@ def nerd_tree(startpath, prec_seps=[]):
|
||||
|
||||
return nerd_tree_txt
|
||||
|
||||
def nerd_tree_from_struct(rootnode, prec_seps=[]):
|
||||
'''
|
||||
rootnode is a dict
|
||||
'''
|
||||
# items = nerd_tree_sort(startpath, dirfirst=True)
|
||||
|
||||
## rootnode is always a dir -> colorize
|
||||
rootnode_name = rootnode['name']
|
||||
nerd_tree_txt = 'd_' + rootnode_name
|
||||
|
||||
items = rootnode.get('children') or []
|
||||
|
||||
for n, item in enumerate(items):
|
||||
if item['name'] in ITEMS_TO_EXCLUDE:
|
||||
continue
|
||||
|
||||
islast = (n==len(items) -1)
|
||||
|
||||
sep = CHILD_CONNECTOR
|
||||
if islast:
|
||||
sep = LAST_CHILD_CONNECTOR
|
||||
|
||||
if not islast:
|
||||
psep_char = VERTICAL_CONNECTOR
|
||||
else:
|
||||
psep_char = ''
|
||||
|
||||
if item['type'] == 'd':
|
||||
seps = prec_seps[:]
|
||||
seps.append((psep_char, ''))
|
||||
treeline = produce_treeline(prec_seps, (sep, ' ')) + ' ' + nerd_tree_from_struct(item, prec_seps=seps)
|
||||
else:
|
||||
treeline = produce_treeline(prec_seps, (sep, item['name']))
|
||||
|
||||
nerd_tree_txt += '\n' + treeline
|
||||
|
||||
return nerd_tree_txt
|
||||
|
||||
def nerd_tree_struct(startpath, path=[]):
|
||||
'''
|
||||
generate a recursive structure representing the tree
|
||||
starting from startpath
|
||||
'''
|
||||
if not startpath.endswith('/'):
|
||||
tosplit = startpath
|
||||
startpath = startpath + '/'
|
||||
else:
|
||||
tosplit = startpath[:-1]
|
||||
|
||||
## it is always a folder
|
||||
rootnode = tosplit.split('/')[-1]
|
||||
|
||||
# path_copy = path[:]
|
||||
# path_copy.append(rootnode)
|
||||
if path:
|
||||
path.append(rootnode)
|
||||
else:
|
||||
path = ['./']
|
||||
|
||||
d = {
|
||||
'name' : rootnode,
|
||||
'type' :'d',
|
||||
'abs_path' : startpath,
|
||||
'children' : [],
|
||||
'path': path,
|
||||
}
|
||||
|
||||
# using listdir
|
||||
items = nerd_tree_sort(startpath, dirfirst=True)
|
||||
|
||||
for item in items:
|
||||
if item in ITEMS_TO_EXCLUDE:
|
||||
continue
|
||||
|
||||
if os.path.isdir(startpath+item):
|
||||
d['children'].append(nerd_tree_struct(startpath+item, path=path[:]))
|
||||
else:
|
||||
path_copy = path[:]
|
||||
path_copy.append(item)
|
||||
|
||||
d['children'].append({
|
||||
'name' : item,
|
||||
'type' : 'f',
|
||||
'abs_path' : startpath + item,
|
||||
'path' : path_copy,
|
||||
})
|
||||
|
||||
return d
|
||||
|
||||
def find_subtree(node, node_name, results=[]):
|
||||
|
||||
if node['name'] == node_name:
|
||||
results.append(node)
|
||||
|
||||
children = node.get('children') or []
|
||||
|
||||
for item_child in children:
|
||||
if item_child['type'] == 'd':
|
||||
find_subtree(item_child, node_name, results)
|
||||
else:
|
||||
if item_child['name'] == node_name:
|
||||
results.append(item_child)
|
||||
|
||||
def find_tree(startpath, item_name):
|
||||
rnode = nerd_tree_struct(startpath)
|
||||
|
||||
results = []
|
||||
find_subtree(rnode, item_name, results)
|
||||
|
||||
return results
|
||||
|
||||
def nerd_tree(startpath):
|
||||
tree_struct = nerd_tree_struct(startpath)
|
||||
tree_txt = nerd_tree_from_struct(tree_struct)
|
||||
print(tree_txt)
|
||||
|
Loading…
x
Reference in New Issue
Block a user