From a72a1b60fc9c3781eaedaa301f7a497b8bbe0a45 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Sat, 16 Feb 2019 13:31:13 +0100 Subject: [PATCH] find by directory and by path --- catcli/catcli.py | 7 ++++++- catcli/noder.py | 14 +++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/catcli/catcli.py b/catcli/catcli.py index e8920f2..3ab5e43 100755 --- a/catcli/catcli.py +++ b/catcli/catcli.py @@ -39,7 +39,7 @@ Usage: {1} index [--catalog=] [--meta=...] [-acfuV] {1} update [--catalog=] [-acfuV] {1} ls [--catalog=] [-arVS] [] - {1} find [--catalog=] [-abVP] + {1} find [--catalog=] [-abdVP] [--path=] {1} rm [--catalog=] [-fV] {1} tree [--catalog=] [-aVS] [] {1} rename [--catalog=] [-fV] @@ -52,9 +52,11 @@ Usage: Options: --catalog= Path to the catalog [default: {2}]. --meta= Additional attribute to store [default: ]. + -p --path= Start path. -u --subsize Store size of directories [default: False]. -a --archive Handle archive file [default: False]. -f --force Do not ask when updating the catalog [default: False]. + -d --directory Only directory (default: False). -b --script Output script to manage found file(s) [default: False]. -S --sortsize Sort by size, largest first [default: False]. -c --hash Calculate md5 hash [default: False]. @@ -153,7 +155,10 @@ def cmd_rm(args, noder, catalog, top): def cmd_find(args, noder, top): fromtree = args['--parent'] + directory = args['--directory'] + startpath = args['--path'] return noder.find_name(top, args[''], script=args['--script'], + startpath=startpath, directory=directory, parentfromtree=fromtree) diff --git a/catcli/noder.py b/catcli/noder.py index 4ea726a..b10951a 100644 --- a/catcli/noder.py +++ b/catcli/noder.py @@ -245,6 +245,7 @@ class Noder: name = os.sep.join([self._get_parents(node.parent), name]) else: name = node.relpath + name = name.lstrip(os.sep) if withstorage: storage = self._get_storage(node) attr = '' @@ -262,6 +263,7 @@ class Noder: name = os.sep.join([self._get_parents(node.parent), name]) else: name = node.relpath + name = name.lstrip(os.sep) depth = '' if withdepth: depth = len(node.children) @@ -309,18 +311,24 @@ class Noder: # searching ############################################################### def find_name(self, root, key, - script=False, - parentfromtree=False): + script=False, directory=False, + startpath=None, parentfromtree=False): '''find files based on their names''' if self.verbose: Logger.info('searching for \"{}\"'.format(key)) + start = root + if startpath: + start = self.get_node(root, startpath) self.term = key - found = anytree.findall(root, filter_=self._find_name) + found = anytree.findall(start, filter_=self._find_name) paths = [] for f in found: if f.type == self.TYPE_STORAGE: # ignore storage nodes continue + if directory and f.type != self.TYPE_DIR: + # ignore non directory + continue self._print_node(f, withpath=True, withdepth=True, withstorage=True, recalcparent=parentfromtree) if parentfromtree: