cleaning
This commit is contained in:
parent
f6424891b9
commit
c0db9563cb
Binary file not shown.
Binary file not shown.
@ -70,7 +70,8 @@ class NerdTreeFind(NerdTree):
|
||||
|
||||
if dont_show_children_nodes:
|
||||
for result in results:
|
||||
result['nchildren'] = len(result['children'])
|
||||
children = result.get('children') or []
|
||||
result['nchildren'] = len(children)
|
||||
result['children'] = []
|
||||
|
||||
tree_struct = {}
|
||||
|
248
src/tree_repr.py
248
src/tree_repr.py
@ -46,251 +46,3 @@ def produce_treeline(prec_seps, lsep):
|
||||
|
||||
return tree_line.rstrip()
|
||||
|
||||
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 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']
|
||||
if rootnode.get('color_formatter'):
|
||||
nerd_tree_txt = rootnode['color_formatter'](rootnode_name)
|
||||
else:
|
||||
nerd_tree_txt = 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, {'name' : ''}))
|
||||
treeline = produce_treeline(prec_seps, (sep, {'name' : ' '})) + ' ' + nerd_tree_from_struct(item, prec_seps=seps)
|
||||
else:
|
||||
treeline = produce_treeline(prec_seps, (sep, item))
|
||||
|
||||
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({
|
||||
'name' : rootnode,
|
||||
'type' : 'd',
|
||||
'abs_path' : startpath,
|
||||
})
|
||||
else:
|
||||
path = [{
|
||||
'name' : './',
|
||||
'type' : 'd',
|
||||
'abs_path' : startpath,
|
||||
}]
|
||||
|
||||
d = {
|
||||
'name' : rootnode,
|
||||
'type' :'d',
|
||||
'abs_path' : startpath,
|
||||
'children' : [],
|
||||
'path': path,
|
||||
}
|
||||
|
||||
# using listdir
|
||||
try:
|
||||
items = nerd_tree_sort(startpath, dirfirst=True)
|
||||
except Exception as ss:
|
||||
print(f'Path: {startpath} not readable because {ss}')
|
||||
items = []
|
||||
d.setdefault('not_readable', 1)
|
||||
|
||||
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({
|
||||
'name' : item,
|
||||
'type' : 'f',
|
||||
'abs_path' : startpath + item
|
||||
})
|
||||
|
||||
d['children'].append({
|
||||
'name' : item,
|
||||
'type' : 'f',
|
||||
'abs_path' : startpath + item,
|
||||
'path' : path_copy,
|
||||
})
|
||||
|
||||
return d
|
||||
|
||||
def find_subtree(node, node_name, results=[]):
|
||||
'''
|
||||
Starting fron node, recursively, find node_name and its subtree
|
||||
'''
|
||||
|
||||
if node['name'] == node_name:
|
||||
node.update({'color_formatter' : YELLOW})
|
||||
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:
|
||||
item_child.update({'color_formatter' : YELLOW})
|
||||
results.append(item_child)
|
||||
|
||||
def find_node(startpath, item_name):
|
||||
'''
|
||||
@param startpath string
|
||||
@param item_name string
|
||||
|
||||
search the node with name==item_name (basically a subtree)
|
||||
starting from startpath
|
||||
Note: it finds all occurrencies
|
||||
'''
|
||||
rnode = nerd_tree_struct(startpath)
|
||||
|
||||
results = []
|
||||
find_subtree(rnode, item_name, results)
|
||||
|
||||
return results
|
||||
|
||||
def find_children(tree, item_abs_path):
|
||||
'''
|
||||
It returns a tuple:
|
||||
first value True if father of node with item_abs_path is found,
|
||||
second value represents the children of parent node with item_abs_path
|
||||
'''
|
||||
if tree['abs_path'] == item_abs_path:
|
||||
if not tree.get('children'):
|
||||
tree['children'] = []
|
||||
return (True, tree['children'])
|
||||
|
||||
children = tree.get('children') or []
|
||||
if not children:
|
||||
return (False, [])
|
||||
|
||||
for child in children:
|
||||
found, fchildren = find_children(child, item_abs_path)
|
||||
if found:
|
||||
return (True, fchildren)
|
||||
|
||||
return (False, [])
|
||||
|
||||
def find_tree_struct(startpath, item_name, opts={}):
|
||||
'''
|
||||
'''
|
||||
results = find_node(startpath, item_name)
|
||||
|
||||
dont_show_children_nodes = True if opts.get('dont_show_children_nodes') else False
|
||||
|
||||
if not results:
|
||||
return {}
|
||||
|
||||
if dont_show_children_nodes:
|
||||
for result in results:
|
||||
result['children'] = []
|
||||
|
||||
tree_struct = {}
|
||||
|
||||
for node in results:
|
||||
path = node['path']
|
||||
|
||||
for n,p in enumerate(path):
|
||||
pcopy = p.copy()
|
||||
pname = p['name']
|
||||
|
||||
if pname == './':
|
||||
if tree_struct: continue
|
||||
tree_struct = pcopy
|
||||
tree_struct['children'] = []
|
||||
continue
|
||||
|
||||
parent = path[n-1]
|
||||
found, children = find_children(tree_struct, parent['abs_path'])
|
||||
|
||||
if not found:
|
||||
raise Exception('Bug!!!')
|
||||
|
||||
if p['abs_path'] in [c['abs_path'] for c in children]:
|
||||
continue
|
||||
|
||||
islastpath = True if (n== len(path)-1) else False
|
||||
|
||||
if not islastpath: children.append(pcopy)
|
||||
else : children.append(node)
|
||||
|
||||
return tree_struct
|
||||
|
||||
def nerd_tree(startpath):
|
||||
tree_struct = nerd_tree_struct(startpath)
|
||||
tree_txt = nerd_tree_from_struct(tree_struct)
|
||||
print(tree_txt)
|
||||
|
||||
def find_nerd_tree(startpath, item_name, opts={}):
|
||||
tree_struct = find_tree_struct(startpath, item_name, opts=opts)
|
||||
if not tree_struct:
|
||||
print('No results')
|
||||
return
|
||||
tree_txt = nerd_tree_from_struct(tree_struct)
|
||||
print(tree_txt)
|
||||
|
Loading…
x
Reference in New Issue
Block a user