From 1b2c52fb3e174b0b96dbf80b783d7862f6360c41 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Sat, 15 Jan 2022 14:41:33 +0100 Subject: [PATCH] add csv format to ls and find --- catcli/catcli.py | 28 ++++++++++++++------- catcli/noder.py | 65 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 71 insertions(+), 22 deletions(-) diff --git a/catcli/catcli.py b/catcli/catcli.py index 85fea0a..66a24ef 100755 --- a/catcli/catcli.py +++ b/catcli/catcli.py @@ -36,10 +36,10 @@ USAGE = """ {0} Usage: - {1} ls [--catalog=] [-aBCrVS] [] + {1} ls [--catalog=] [--format=] [-aBCrVS] [] {1} index [--catalog=] [--meta=...] [-aBCcfnV] {1} update [--catalog=] [-aBCcfnV] [--lpath=] - {1} find [--catalog=] [-aBCbdVP] [--path=] + {1} find [--catalog=] [--format=] [-aBCbdVP] [--path=] {1} rm [--catalog=] [-BCfV] {1} tree [--catalog=] [-aBCVS] [] {1} rename [--catalog=] [-BCfV] @@ -59,7 +59,7 @@ Options: -C --no-color Do not output colors [default: False]. -c --hash Calculate md5 hash [default: False]. -d --directory Only directory [default: False]. - -F --format= Export format [default: csv]. + -F --format= Export format [default: native]. -H --header Export with header [default: False]. -f --force Do not ask when updating the catalog [default: False]. -l --lpath= Path where changes are logged [default: ] @@ -71,7 +71,7 @@ Options: -V --verbose Be verbose [default: False]. -v --version Show version. -h --help Show this screen. -""".format(BANNER, NAME, CATALOGPATH) +""".format(BANNER, NAME, CATALOGPATH) # nopep8 def cmd_index(args, noder, catalog, top): @@ -146,7 +146,9 @@ def cmd_ls(args, noder, top): path += SEPARATOR if not path.endswith(WILD): path += WILD - found = noder.walk(top, path, rec=args['--recursive']) + found = noder.walk(top, path, + rec=args['--recursive'], + fmt=args['--format']) if not found: Logger.err('\"{}\": nothing found'.format(args[''])) return found @@ -168,9 +170,10 @@ def cmd_find(args, noder, top): fromtree = args['--parent'] directory = args['--directory'] startpath = args['--path'] + fmt = args['--format'] return noder.find_name(top, args[''], script=args['--script'], startpath=startpath, directory=directory, - parentfromtree=fromtree) + parentfromtree=fromtree, fmt=fmt) def cmd_tree(args, noder, top): @@ -214,10 +217,11 @@ def cmd_export(args, noder, catalog, top): header = args['--header'] fmt = args['--format'] - if fmt == 'csv': + if fmt == 'native': + # equivalent to tree + noder.print_tree(node) + elif fmt == 'csv': noder.to_csv(node, with_header=header) - else: - Logger.err('Format not supported: {}'.format(fmt)) def cmd_edit(args, noder, catalog, top): @@ -249,6 +253,12 @@ def main(): print(USAGE) return True + # check format + fmt = args['--format'] + if fmt != 'native' and fmt != 'csv': + Logger.err('bad format: {}'.format(fmt)) + return False + if args['--verbose']: print(args) diff --git a/catcli/noder.py b/catcli/noder.py index 4a97684..c14ad0f 100644 --- a/catcli/noder.py +++ b/catcli/noder.py @@ -325,7 +325,9 @@ class Noder: else: out.append('') - return sep.join(['"' + o + '"' for o in out]) + line = sep.join(['"' + o + '"' for o in out]) + if len(line) > 0: + Logger.out(line) def _print_node(self, node, pre='', withpath=False, withdepth=False, withstorage=False, @@ -430,9 +432,7 @@ class Noder: rend = anytree.RenderTree(node, childiter=self._sort_tree) for _, _, node in rend: - line = self._node_to_csv(node) - if len(line) > 0: - Logger.out(line) + self._node_to_csv(node) def to_dot(self, node, path='tree.dot'): '''export to dot for graphing''' @@ -445,8 +445,16 @@ class Noder: ############################################################### def find_name(self, root, key, script=False, directory=False, - startpath=None, parentfromtree=False): - '''find files based on their names''' + startpath=None, parentfromtree=False, + fmt='native'): + ''' + find files based on their names + @script: output script + @directory: only search for directories + @startpath: node to start with + @parentfromtree: get path from parent instead of stored relpath + @fmt: output format + ''' self._debug('searching for \"{}\"'.format(key)) start = root if startpath: @@ -461,16 +469,26 @@ class Noder: if directory and f.type != self.TYPE_DIR: # ignore non directory continue - self._print_node(f, withpath=True, withdepth=True, - withstorage=True, recalcparent=parentfromtree) + + # print the node + if fmt == 'native': + self._print_node(f, withpath=True, + withdepth=True, + withstorage=True, + recalcparent=parentfromtree) + elif fmt == 'csv': + self._node_to_csv(f) + if parentfromtree: paths.append(self._get_parents(f)) else: paths.append(f.relpath) + if script: tmp = ['${source}/' + x for x in paths] cmd = 'op=file; source=/media/mnt; $op {}'.format(' '.join(tmp)) Logger.info(cmd) + return found def _find_name(self, node): @@ -482,7 +500,7 @@ class Noder: ############################################################### # climbing ############################################################### - def walk(self, root, path, rec=False): + def walk(self, root, path, rec=False, fmt='native'): '''walk the tree for ls based on names''' self._debug('walking path: \"{}\"'.format(path)) r = anytree.resolver.Resolver('name') @@ -490,15 +508,36 @@ class Noder: try: found = r.glob(root, path) if len(found) < 1: + # nothing found return [] + if rec: - self.print_tree(found[0].parent) + # print the entire tree + if fmt == 'native': + self.print_tree(found[0].parent) + elif fmt == 'csv': + self.to_csv(found[0].parent) return found + + # sort found nodes found = sorted(found, key=self._sort, reverse=self.sortsize) - self._print_node(found[0].parent, - withpath=False, withdepth=True) + + # print the parent + if fmt == 'native': + self._print_node(found[0].parent, + withpath=False, withdepth=True) + elif fmt == 'csv': + self._node_to_csv(found[0].parent) + + # print all found nodes for f in found: - self._print_node(f, withpath=False, pre='- ', withdepth=True) + if fmt == 'native': + self._print_node(f, withpath=False, + pre='- ', + withdepth=True) + elif fmt == 'csv': + self._node_to_csv(f) + except anytree.resolver.ChildResolverError: pass return found