adding header switch for tree

pull/17/head
deadc0de6 2 years ago
parent 93f9cf560d
commit 56d40c12cc

@ -38,7 +38,7 @@ USAGE = """
Usage:
{1} ls [--catalog=<path>] [--format=<fmt>] [-aBCrVS] [<path>]
{1} find [--catalog=<path>] [--format=<fmt>] [-aBCbdVP] [--path=<path>] <term>
{1} tree [--catalog=<path>] [--format=<fmt>] [-aBCVS] [<path>]
{1} tree [--catalog=<path>] [--format=<fmt>] [-aBCVSH] [<path>]
{1} index [--catalog=<path>] [--meta=<meta>...] [-aBCcfnV] <name> <path>
{1} update [--catalog=<path>] [-aBCcfnV] [--lpath=<path>] <name> <path>
{1} rm [--catalog=<path>] [-BCfV] <storage>
@ -61,6 +61,7 @@ Options:
-d --directory Only directory [default: False].
-F --format=<fmt> Print format, see command \"print_supported_formats\" [default: native].
-f --force Do not ask when updating the catalog [default: False].
-H --header Print header on CSV format [default: False].
-l --lpath=<path> Path where changes are logged [default: ]
-n --no-subsize Do not store size of directories [default: False].
-P --parent Ignore stored relpath [default: True].
@ -178,6 +179,7 @@ def cmd_find(args, noder, top):
def cmd_tree(args, noder, top):
path = args['<path>']
fmt = args['--format']
hdr = args['--header']
# find node to start with
node = top
@ -186,7 +188,7 @@ def cmd_tree(args, noder, top):
if node:
# print the tree
noder.print_tree(node, fmt=fmt)
noder.print_tree(node, fmt=fmt, header=hdr)
def cmd_graph(args, noder, top):

@ -417,18 +417,27 @@ class Noder:
else:
Logger.err('bad node encountered: {}'.format(node))
def print_tree(self, node, style=anytree.ContRoundStyle(), fmt='native'):
'''print the tree similar to unix tool "tree"'''
def print_tree(self, node, style=anytree.ContRoundStyle(),
fmt='native', header=False):
'''
print the tree similar to unix tool "tree"
@node: start node
@style: when fmt=native, defines the tree style
@fmt: output format
@header: when fmt=csv, print the header
'''
if fmt == 'native':
rend = anytree.RenderTree(node, childiter=self._sort_tree)
for pre, fill, node in rend:
self._print_node(node, pre=pre, withdepth=True)
elif fmt == 'csv':
self._to_csv(node)
self._to_csv(node, with_header=header)
def _to_csv(self, node, with_header=True):
def _to_csv(self, node, with_header=False):
'''print the tree to csv'''
rend = anytree.RenderTree(node, childiter=self._sort_tree)
if with_header:
Logger.out(self.CSV_HEADER)
for _, _, node in rend:
self._node_to_csv(node)
@ -499,8 +508,14 @@ class Noder:
# climbing
###############################################################
def walk(self, root, path, rec=False, fmt='native'):
'''walk the tree for ls based on names'''
'''
walk the tree for ls based on names
@root: start node
@rec: recursive walk
@fmt: output format
'''
self._debug('walking path: \"{}\"'.format(path))
r = anytree.resolver.Resolver('name')
found = []
try:

@ -28,6 +28,7 @@ class TestTree(unittest.TestCase):
'<path>': path,
'--verbose': True,
'--format': 'native',
'--header': False,
}
# print tree and wait for any errors

Loading…
Cancel
Save