refactoring

pull/6/head
deadc0de6 6 years ago
parent 927d5e2773
commit 39efd9b8fe

@ -22,7 +22,6 @@ class Catalog:
self.verbose = verbose # verbosity
self.force = force # force overwrite if exists
self.metanode = None
# prefer json for git versioning
self.pickle = pickle
def set_metanode(self, metanode):
@ -68,6 +67,14 @@ class Catalog:
Logger.info('Catalog saved to pickle \"{}\"'.format(self.path))
return True
def _restore_pickle(self):
'''restore the pickled tree'''
root = pickle.load(open(self.path, 'rb'))
if self.verbose:
m = 'Catalog imported from pickle \"{}\"'.format(self.path)
Logger.info(m)
return root
def _save_json(self, node):
'''export the catalog in json'''
exp = JsonExporter(indent=2, sort_keys=True)
@ -77,14 +84,6 @@ class Catalog:
Logger.info('Catalog saved to json \"{}\"'.format(self.path))
return True
def _restore_pickle(self):
''' restore the pickled tree '''
root = pickle.load(open(self.path, 'rb'))
if self.verbose:
m = 'Catalog imported from pickle \"{}\"'.format(self.path)
Logger.info(m)
return root
def _restore_json(self, string):
'''restore the tree from json'''
imp = JsonImporter()

@ -80,7 +80,7 @@ def cmd_index(args, noder, catalog, top):
node.parent = None
start = datetime.datetime.now()
walker = Walker(noder, nohash=nohash)
attr = noder.clean_storage_attr(args['--meta'])
attr = noder.format_storage_attr(args['--meta'])
root = noder.storage_node(name, path, parent=top, attr=attr)
_, cnt = walker.index(path, name, parent=root, parentpath=path)
if subsize:

@ -29,6 +29,7 @@ class Decomp:
'zip': self._zip}
def get_format(self):
'''return list of supported extensions'''
return list(self.ext.keys())
def get_names(self, path):
@ -39,12 +40,14 @@ class Decomp:
return None
def _tar(self, path):
'''return list of file names in tar'''
if not tarfile.is_tarfile(path):
return None
tar = tarfile.open(path, "r")
return tar.getnames()
def _zip(self, path):
'''return list of file names in zip'''
if not zipfile.is_zipfile(path):
return None
z = zipfile.ZipFile(path)

@ -88,4 +88,5 @@ class Logger:
sys.stderr.flush()
def bold(string):
'''make it bold'''
return '{}{}{}'.format(Logger.BOLD, string, Logger.RESET)

@ -44,26 +44,18 @@ class Noder:
if self.arc:
self.decomp = Decomp()
def set_hashing(self, val):
self.hash = val
def get_storage_names(self, top):
'''return a list of all storage names'''
return [x.name for x in list(top.children)]
def get_storage_node(self, top, name):
''' return the storage node '''
'''return the storage node if any'''
for n in top.children:
if n.type != self.TYPE_STORAGE:
continue
if n.name == name:
return n
def clean_storage_attr(self, attr):
if not attr:
return ''
return ', '.join(attr)
def get_node(self, top, path):
'''get the node by internal tree path'''
r = anytree.resolver.Resolver('name')
@ -73,6 +65,44 @@ class Noder:
Logger.err('No node at path \"{}\"'.format(path))
return None
def get_meta_node(self, top):
'''return the meta node if any'''
try:
return next(filter(lambda x: x.type == self.TYPE_META,
top.children))
except StopIteration:
return None
def rec_size(self, node):
'''recursively traverse tree and store dir size'''
if self.verbose:
Logger.info('getting folder size recursively')
if node.type == self.TYPE_FILE:
return node.size
size = 0
for i in node.children:
if node.type == self.TYPE_DIR:
size += self.rec_size(i)
if node.type == self.TYPE_STORAGE:
self.rec_size(i)
else:
continue
node.size = size
return size
###############################################################
# public helpers
###############################################################
def format_storage_attr(self, attr):
'''format the storage attr for saving'''
if not attr:
return ''
return ', '.join(attr)
def set_hashing(self, val):
'''hash files when indexing'''
self.hash = val
###############################################################
# node creationg
###############################################################
@ -135,6 +165,7 @@ class Noder:
total=total, parent=parent, attr=attr, ts=epoch)
def archive_node(self, name, path, parent, archive):
'''crete a new node for archive data'''
return anytree.AnyNode(name=name, type=self.TYPE_ARC, relpath=path,
parent=parent, size=0, md5=None,
archive=archive)
@ -198,6 +229,12 @@ class Noder:
for pre, fill, node in rend:
self._print_node(node, pre=pre, withdepth=True)
def to_dot(self, node, path='tree.dot'):
'''export to dot for graphing'''
anytree.exporter.DotExporter(node).to_dotfile(path)
Logger.info('dot file created under \"{}\"'.format(path))
return 'dot {} -T png -o /tmp/tree.png'.format(path)
###############################################################
# searching
###############################################################
@ -303,37 +340,6 @@ class Noder:
except AttributeError:
return 0
def to_dot(self, node, path='tree.dot'):
''' export to dot for graphing '''
anytree.exporter.DotExporter(node).to_dotfile(path)
Logger.info('dot file created under \"{}\"'.format(path))
return 'dot {} -T png -o /tmp/tree.png'.format(path)
def _get_storage(self, node):
'''recursively traverse up to find storage'''
return node.ancestors[1]
def get_meta_node(self, top):
''' return the meta node if any '''
try:
return next(filter(lambda x: x.type == self.TYPE_META,
top.children))
except StopIteration:
return None
def rec_size(self, node):
''' recursively traverse tree and store dir size '''
if self.verbose:
Logger.info('getting folder size recursively')
if node.type == self.TYPE_FILE:
return node.size
size = 0
for i in node.children:
if node.type == self.TYPE_DIR:
size += self.rec_size(i)
if node.type == self.TYPE_STORAGE:
self.rec_size(i)
else:
continue
node.size = size
return size

Loading…
Cancel
Save