adding meta node with meta information on tree

pull/6/head
deadc0de6 7 years ago
parent c9c53c1468
commit d401613011

@ -21,9 +21,15 @@ class Catalog:
self.path = path # catalog path
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):
''' remove the metanode until tree is re-written '''
self.metanode = metanode
self.metanode.parent = None
def restore(self):
''' restore the catalog '''
if not self.path:
@ -49,6 +55,8 @@ class Catalog:
if d and not os.path.exists(d):
Logger.err('Cannot write to \"{}\"'.format(d))
return False
if self.metanode:
self.metanode.parent = node
if self.pickle:
return self._save_pickle(node)
return self._save_json(node)

@ -155,6 +155,10 @@ def main():
if not top:
top = noder.new_top_node()
# handle the meta node
meta = noder.update_metanode(noder.get_meta_node(top))
catalog.set_metanode(meta)
# parse command
if args['index']:
cmd_index(args, noder, catalog, top)

@ -11,6 +11,7 @@ import psutil
import time
# local imports
from . import __version__ as VERSION
import catcli.utils as utils
from catcli.logger import Logger
@ -26,10 +27,12 @@ There are 4 types of node:
class Noder:
TOPNAME = 'top'
METANAME = 'meta'
TYPE_TOP = 'top' # tip top ;-)
TYPE_FILE = 'file'
TYPE_DIR = 'dir'
TYPE_STORAGE = 'storage'
TYPE_META = 'meta'
def __init__(self, verbose=False):
self.hash = True
@ -58,6 +61,19 @@ class Noder:
''' create a new top node'''
return anytree.AnyNode(name=self.TOPNAME, type=self.TYPE_TOP)
def update_metanode(self, meta):
''' create or update meta node information '''
epoch = int(time.time())
if not meta:
attr = {}
attr['created'] = epoch
attr['created_version'] = VERSION
meta = anytree.AnyNode(name=self.METANAME, type=self.TYPE_META,
attr=attr)
meta.attr['access'] = epoch
meta.attr['access_version'] = VERSION
return meta
def file_node(self, name, path, parent, storagepath):
''' create a new node representing a file '''
if not os.path.exists(path):
@ -88,7 +104,7 @@ class Noder:
path = os.path.abspath(path)
free = psutil.disk_usage(path).free
total = psutil.disk_usage(path).total
epoch = time.time()
epoch = int(time.time())
return anytree.AnyNode(name=name, type=self.TYPE_STORAGE, free=free,
total=total, parent=parent, attr=attr, ts=epoch)
@ -226,6 +242,14 @@ class Noder:
else:
return self._get_storage(node.parent)
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:

Loading…
Cancel
Save