add csv format to ls and find

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

@ -36,10 +36,10 @@ USAGE = """
{0}
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} 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} tree [--catalog=<path>] [-aBCVS] [<path>]
{1} rename [--catalog=<path>] [-BCfV] <storage> <name>
@ -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=<fmt> Export format [default: csv].
-F --format=<fmt> 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> 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['<path>']))
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['<term>'], 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)

@ -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

Loading…
Cancel
Save