From 56d40c12cc246eab3df30a54b8ed808a40742e88 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Mon, 17 Jan 2022 22:31:37 +0100 Subject: [PATCH] adding header switch for tree --- catcli/catcli.py | 6 ++++-- catcli/noder.py | 25 ++++++++++++++++++++----- tests/test_tree.py | 1 + 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/catcli/catcli.py b/catcli/catcli.py index 2f2f6ab..ac39a23 100755 --- a/catcli/catcli.py +++ b/catcli/catcli.py @@ -38,7 +38,7 @@ USAGE = """ Usage: {1} ls [--catalog=] [--format=] [-aBCrVS] [] {1} find [--catalog=] [--format=] [-aBCbdVP] [--path=] - {1} tree [--catalog=] [--format=] [-aBCVS] [] + {1} tree [--catalog=] [--format=] [-aBCVSH] [] {1} index [--catalog=] [--meta=...] [-aBCcfnV] {1} update [--catalog=] [-aBCcfnV] [--lpath=] {1} rm [--catalog=] [-BCfV] @@ -61,6 +61,7 @@ Options: -d --directory Only directory [default: False]. -F --format= 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 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[''] 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): diff --git a/catcli/noder.py b/catcli/noder.py index f54e425..0997b49 100644 --- a/catcli/noder.py +++ b/catcli/noder.py @@ -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: diff --git a/tests/test_tree.py b/tests/test_tree.py index 320ce23..46cfe4f 100644 --- a/tests/test_tree.py +++ b/tests/test_tree.py @@ -28,6 +28,7 @@ class TestTree(unittest.TestCase): '': path, '--verbose': True, '--format': 'native', + '--header': False, } # print tree and wait for any errors