add csv format to ls and find

pull/17/head
deadc0de6 2 years ago
parent f5b20c023f
commit 1b2c52fb3e

@ -36,10 +36,10 @@ USAGE = """
{0} {0}
Usage: Usage:
{1} ls [--catalog=<path>] [-aBCrVS] [<path>] {1} ls [--catalog=<path>] [--format=<fmt>] [-aBCrVS] [<path>]
{1} index [--catalog=<path>] [--meta=<meta>...] [-aBCcfnV] <name> <path> {1} index [--catalog=<path>] [--meta=<meta>...] [-aBCcfnV] <name> <path>
{1} update [--catalog=<path>] [-aBCcfnV] [--lpath=<path>] <name> <path> {1} update [--catalog=<path>] [-aBCcfnV] [--lpath=<path>] <name> <path>
{1} find [--catalog=<path>] [-aBCbdVP] [--path=<path>] <term> {1} find [--catalog=<path>] [--format=<fmt>] [-aBCbdVP] [--path=<path>] <term>
{1} rm [--catalog=<path>] [-BCfV] <storage> {1} rm [--catalog=<path>] [-BCfV] <storage>
{1} tree [--catalog=<path>] [-aBCVS] [<path>] {1} tree [--catalog=<path>] [-aBCVS] [<path>]
{1} rename [--catalog=<path>] [-BCfV] <storage> <name> {1} rename [--catalog=<path>] [-BCfV] <storage> <name>
@ -59,7 +59,7 @@ Options:
-C --no-color Do not output colors [default: False]. -C --no-color Do not output colors [default: False].
-c --hash Calculate md5 hash [default: False]. -c --hash Calculate md5 hash [default: False].
-d --directory Only directory [default: False]. -d --directory Only directory [default: False].
-F --format=<fmt> Export format [default: csv]. -F --format=<fmt> Export format [default: native].
-H --header Export with header [default: False]. -H --header Export with header [default: False].
-f --force Do not ask when updating the catalog [default: False]. -f --force Do not ask when updating the catalog [default: False].
-l --lpath=<path> Path where changes are logged [default: ] -l --lpath=<path> Path where changes are logged [default: ]
@ -71,7 +71,7 @@ Options:
-V --verbose Be verbose [default: False]. -V --verbose Be verbose [default: False].
-v --version Show version. -v --version Show version.
-h --help Show this screen. -h --help Show this screen.
""".format(BANNER, NAME, CATALOGPATH) """.format(BANNER, NAME, CATALOGPATH) # nopep8
def cmd_index(args, noder, catalog, top): def cmd_index(args, noder, catalog, top):
@ -146,7 +146,9 @@ def cmd_ls(args, noder, top):
path += SEPARATOR path += SEPARATOR
if not path.endswith(WILD): if not path.endswith(WILD):
path += 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: if not found:
Logger.err('\"{}\": nothing found'.format(args['<path>'])) Logger.err('\"{}\": nothing found'.format(args['<path>']))
return found return found
@ -168,9 +170,10 @@ def cmd_find(args, noder, top):
fromtree = args['--parent'] fromtree = args['--parent']
directory = args['--directory'] directory = args['--directory']
startpath = args['--path'] startpath = args['--path']
fmt = args['--format']
return noder.find_name(top, args['<term>'], script=args['--script'], return noder.find_name(top, args['<term>'], script=args['--script'],
startpath=startpath, directory=directory, startpath=startpath, directory=directory,
parentfromtree=fromtree) parentfromtree=fromtree, fmt=fmt)
def cmd_tree(args, noder, top): def cmd_tree(args, noder, top):
@ -214,10 +217,11 @@ def cmd_export(args, noder, catalog, top):
header = args['--header'] header = args['--header']
fmt = args['--format'] 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) noder.to_csv(node, with_header=header)
else:
Logger.err('Format not supported: {}'.format(fmt))
def cmd_edit(args, noder, catalog, top): def cmd_edit(args, noder, catalog, top):
@ -249,6 +253,12 @@ def main():
print(USAGE) print(USAGE)
return True return True
# check format
fmt = args['--format']
if fmt != 'native' and fmt != 'csv':
Logger.err('bad format: {}'.format(fmt))
return False
if args['--verbose']: if args['--verbose']:
print(args) print(args)

@ -325,7 +325,9 @@ class Noder:
else: else:
out.append('') 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, def _print_node(self, node, pre='', withpath=False,
withdepth=False, withstorage=False, withdepth=False, withstorage=False,
@ -430,9 +432,7 @@ class Noder:
rend = anytree.RenderTree(node, childiter=self._sort_tree) rend = anytree.RenderTree(node, childiter=self._sort_tree)
for _, _, node in rend: for _, _, node in rend:
line = self._node_to_csv(node) self._node_to_csv(node)
if len(line) > 0:
Logger.out(line)
def to_dot(self, node, path='tree.dot'): def to_dot(self, node, path='tree.dot'):
'''export to dot for graphing''' '''export to dot for graphing'''
@ -445,8 +445,16 @@ class Noder:
############################################################### ###############################################################
def find_name(self, root, key, def find_name(self, root, key,
script=False, directory=False, script=False, directory=False,
startpath=None, parentfromtree=False): startpath=None, parentfromtree=False,
'''find files based on their names''' 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)) self._debug('searching for \"{}\"'.format(key))
start = root start = root
if startpath: if startpath:
@ -461,16 +469,26 @@ class Noder:
if directory and f.type != self.TYPE_DIR: if directory and f.type != self.TYPE_DIR:
# ignore non directory # ignore non directory
continue 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: if parentfromtree:
paths.append(self._get_parents(f)) paths.append(self._get_parents(f))
else: else:
paths.append(f.relpath) paths.append(f.relpath)
if script: if script:
tmp = ['${source}/' + x for x in paths] tmp = ['${source}/' + x for x in paths]
cmd = 'op=file; source=/media/mnt; $op {}'.format(' '.join(tmp)) cmd = 'op=file; source=/media/mnt; $op {}'.format(' '.join(tmp))
Logger.info(cmd) Logger.info(cmd)
return found return found
def _find_name(self, node): def _find_name(self, node):
@ -482,7 +500,7 @@ class Noder:
############################################################### ###############################################################
# climbing # 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''' '''walk the tree for ls based on names'''
self._debug('walking path: \"{}\"'.format(path)) self._debug('walking path: \"{}\"'.format(path))
r = anytree.resolver.Resolver('name') r = anytree.resolver.Resolver('name')
@ -490,15 +508,36 @@ class Noder:
try: try:
found = r.glob(root, path) found = r.glob(root, path)
if len(found) < 1: if len(found) < 1:
# nothing found
return [] return []
if rec: 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 return found
# sort found nodes
found = sorted(found, key=self._sort, reverse=self.sortsize) 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: 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: except anytree.resolver.ChildResolverError:
pass pass
return found return found

Loading…
Cancel
Save