refactor and replace export with tree

pull/17/head
deadc0de6 2 years ago
parent a3ebd2d5a4
commit ecd15e66bd

@ -75,7 +75,7 @@ See the [examples](#examples) for an overview of the available features.
* [Catalog graph](#catalog-graph)
* [Edit storage](#edit-storage)
* [Update catalog](#update-catalog)
* [Export catalog](#export-catalog)
* [CSV format](#csv-format)
* [Examples](#examples)
* [Contribution](#contribution)
@ -208,9 +208,9 @@ Updates are based on the access time of each of the files and on the
hash checksum if present (catalog was indexed with `-c --hash` and
`update` is called with the switch `-c --hash`).
## Export catalog
## CSV format
The catalog can be exported to CSV using the `export` command.
Results can be printed to CSV using `--format=csv`.
Fields are separated by a comma (`,`) and are quoted with double quotes (`"`).
Each line format is `name,type,path,size,indexed_at,maccess,md5`.

@ -37,15 +37,14 @@ 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} index [--catalog=<path>] [--meta=<meta>...] [-aBCcfnV] <name> <path>
{1} update [--catalog=<path>] [-aBCcfnV] [--lpath=<path>] <name> <path>
{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>
{1} edit [--catalog=<path>] [-BCfV] <storage>
{1} graph [--catalog=<path>] [-BCV] [<path>]
{1} export [--catalog=<path>] [-BH] [--format=<fmt>] [<path>]
{1} print_supported_formats
{1} help
{1} --help
@ -60,8 +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: native].
-H --header Export with header [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].
-l --lpath=<path> Path where changes are logged [default: ]
-n --no-subsize Do not store size of directories [default: False].
@ -179,11 +177,16 @@ def cmd_find(args, noder, top):
def cmd_tree(args, noder, top):
path = args['<path>']
fmt = args['--format']
# find node to start with
node = top
if path:
node = noder.get_node(top, path)
if node:
noder.print_tree(node)
# print the tree
noder.print_tree(node, fmt=fmt)
def cmd_graph(args, noder, top):
@ -209,22 +212,6 @@ def cmd_rename(args, noder, catalog, top):
return top
def cmd_export(args, noder, catalog, top):
path = args['<path>']
node = top
if path:
node = noder.get_node(top, path)
header = args['--header']
fmt = args['--format']
if fmt == 'native':
# equivalent to tree
noder.print_tree(node)
elif fmt == 'csv':
noder.to_csv(node, with_header=header)
def cmd_edit(args, noder, catalog, top):
storage = args['<storage>']
storages = list(x.name for x in top.children)
@ -305,11 +292,10 @@ def main():
cmd_rename(args, noder, catalog, top)
elif args['edit']:
cmd_edit(args, noder, catalog, top)
elif args['export']:
cmd_export(args, noder, catalog, top)
elif args['print_supported_formats']:
print('"native": native format')
print('"csv" : CSV format')
print(' {}'.format(noder.CSV_HEADER))
return True

@ -290,40 +290,38 @@ class Noder:
return ''
if node.type == self.TYPE_TOP:
return ''
if node.type == self.TYPE_STORAGE:
return ''
out = []
# node name
out.append(node.name)
# node type
out.append(node.type)
# node full path
parents = self._get_parents(node)
storage = self._get_storage(node)
fullpath = os.path.join(storage.name, parents)
out.append(fullpath)
# size
if node.size:
out.append(utils.human(node.size))
if node.type == self.TYPE_STORAGE:
# handle storage
out.append(node.name)
out.append(node.type)
out.append('') # full path
# size
sz = self._rec_size(node, store=False)
out.append(utils.human(sz))
out.append(utils.epoch_to_str(node.ts))
out.append('') # maccess
out.append('') # md5
else:
out.append('')
out.append(node.name)
out.append(node.type)
# indexed date/time
out.append(utils.epoch_to_str(storage.ts))
# node full path
parents = self._get_parents(node)
storage = self._get_storage(node)
fullpath = os.path.join(storage.name, parents)
out.append(fullpath)
# maccess
out.append(utils.epoch_to_str(node.maccess))
out.append(utils.human(node.size))
out.append(utils.epoch_to_str(storage.ts))
out.append(utils.epoch_to_str(node.maccess))
# md5 if any
if node.md5:
out.append(node.md5)
else:
out.append('')
# md5 if any
if node.md5:
out.append(node.md5)
else:
out.append('')
line = sep.join(['"' + o + '"' for o in out])
if len(line) > 0:
@ -419,17 +417,17 @@ class Noder:
else:
Logger.err('bad node encountered: {}'.format(node))
def print_tree(self, node, style=anytree.ContRoundStyle()):
def print_tree(self, node, style=anytree.ContRoundStyle(), fmt='native'):
'''print the tree similar to unix tool "tree"'''
rend = anytree.RenderTree(node, childiter=self._sort_tree)
for pre, fill, node in rend:
self._print_node(node, pre=pre, withdepth=True)
def to_csv(self, node, with_header=False):
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)
def _to_csv(self, node, with_header=True):
'''print the tree to csv'''
if with_header:
Logger.out(self.CSV_HEADER)
rend = anytree.RenderTree(node, childiter=self._sort_tree)
for _, _, node in rend:
self._node_to_csv(node)
@ -513,10 +511,7 @@ class Noder:
if rec:
# print the entire tree
if fmt == 'native':
self.print_tree(found[0].parent)
elif fmt == 'csv':
self.to_csv(found[0].parent)
self.print_tree(found[0].parent, fmt=fmt)
return found
# sort found nodes

@ -24,7 +24,11 @@ class TestTree(unittest.TestCase):
noder = Noder()
# create fake args dict
args = {'<path>': path, '--verbose': True}
args = {
'<path>': path,
'--verbose': True,
'--format': 'native',
}
# print tree and wait for any errors
cmd_tree(args, noder, top)

Loading…
Cancel
Save