From ac4ba145fe84d3a8f5d6dee379863e1af7cb44ca Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 3 Jan 2024 19:41:59 +0100 Subject: [PATCH] clear relpath and add full path --- catcli/catcli.py | 5 +--- catcli/fuser.py | 2 +- catcli/noder.py | 45 +++++++++-------------------- catcli/nodes.py | 6 ---- catcli/utils.py | 15 +++++----- tests-ng/assets/github.catalog.json | 4 --- tests/helpers.py | 8 ----- 7 files changed, 23 insertions(+), 62 deletions(-) diff --git a/catcli/catcli.py b/catcli/catcli.py index 1f2d066..cc88a68 100755 --- a/catcli/catcli.py +++ b/catcli/catcli.py @@ -41,7 +41,7 @@ USAGE = f""" Usage: {NAME} ls [--catalog=] [--format=] [-aBCrVSs] [] {NAME} find [--catalog=] [--format=] - [-aBCbdVsP] [--path=] [] + [-aBCbdVs] [--path=] [] {NAME} index [--catalog=] [--meta=...] [-aBCcfnV] {NAME} update [--catalog=] [-aBCcfnV] [--lpath=] @@ -68,7 +68,6 @@ Options: -f --force Do not ask when updating the catalog [default: False]. -l --lpath= Path where changes are logged [default: ] -n --no-subsize Do not store size of directories [default: False]. - -P --parent Ignore stored relpath [default: True]. -p --path= Start path. -r --recursive Recursive [default: False]. -s --raw-size Print raw size [default: False]. @@ -203,7 +202,6 @@ def cmd_find(args: Dict[str, Any], noder: Noder, top: NodeTop) -> List[NodeAny]: """find action""" - fromtree = args['--parent'] directory = args['--directory'] startpath = args['--path'] fmt = args['--format'] @@ -216,7 +214,6 @@ def cmd_find(args: Dict[str, Any], script=script, startnode=startpath, only_dir=directory, - parentfromtree=fromtree, fmt=fmt, raw=raw) return found diff --git a/catcli/fuser.py b/catcli/fuser.py index 4efc553..f015e36 100644 --- a/catcli/fuser.py +++ b/catcli/fuser.py @@ -107,7 +107,7 @@ class CatcliFilesystem(fuse.LoggingMixIn, fuse.Operations): # type: ignore def getattr(self, path: str, _fh: Any = None) -> Dict[str, Any]: """return attr of file pointed by path""" - if path == '/': + if path == os.path.sep: # mountpoint curt = time() meta = { diff --git a/catcli/noder.py b/catcli/noder.py index cfd21da..9a6c6bf 100644 --- a/catcli/noder.py +++ b/catcli/noder.py @@ -212,11 +212,9 @@ class Noder: md5 = '' if self.hash: md5 = self._get_hash(path) - relpath = os.sep.join([storagepath, name]) maccess = os.path.getmtime(path) node = NodeFile(name, - relpath, stat.st_size, md5, maccess, @@ -235,10 +233,8 @@ class Noder: parent: NodeAny, storagepath: str) -> NodeDir: """create a new node representing a directory""" path = os.path.abspath(path) - relpath = os.sep.join([storagepath, name]) maccess = os.path.getmtime(path) return NodeDir(name, - relpath, 0, maccess, parent=parent) @@ -264,7 +260,7 @@ class Noder: def new_archive_node(self, name: str, path: str, parent: str, archive: str) -> NodeArchived: """create a new node for archive data""" - return NodeArchived(name=name, relpath=path, + return NodeArchived(name=name, parent=parent, nodesize=0, md5='', archive=archive) @@ -383,7 +379,6 @@ class Noder: withpath: bool = False, withdepth: bool = False, withstorage: bool = False, - recalcparent: bool = False, raw: bool = False) -> None: """ print a node @@ -392,7 +387,6 @@ class Noder: @withpath: print the node path @withdepth: print the node depth info @withstorage: print the node storage it belongs to - @recalcparent: get relpath from tree instead of relpath field @raw: print raw size rather than human readable """ if node.type == nodes.TYPE_TOP: @@ -403,14 +397,13 @@ class Noder: # node of type file node.__class__ = NodeFile name = node.name + storage = self._get_storage(node) if withpath: - if recalcparent: - name = os.sep.join([self._get_parents(node.parent), name]) - else: - name = node.relpath + name = os.sep.join([ + storage.name, + self._get_parents(node.parent), + name]) name = name.lstrip(os.sep) - if withstorage: - storage = self._get_storage(node) attr_str = '' if node.md5: attr_str = f', md5:{node.md5}' @@ -424,17 +417,16 @@ class Noder: # node of type directory node.__class__ = NodeDir name = node.name + storage = self._get_storage(node) if withpath: - if recalcparent: - name = os.sep.join([self._get_parents(node.parent), name]) - else: - name = node.relpath + name = os.sep.join([ + storage.name, + self._get_parents(node.parent), + name]) name = name.lstrip(os.sep) depth = 0 if withdepth: depth = len(node.children) - if withstorage: - storage = self._get_storage(node) attr: List[Tuple[str, str]] = [] if node.nodesize: attr.append(('totsize', size_to_str(node.nodesize, raw=raw))) @@ -569,7 +561,6 @@ class Noder: script: bool = False, only_dir: bool = False, startnode: Optional[NodeAny] = None, - parentfromtree: bool = False, fmt: str = 'native', raw: bool = False) -> List[NodeAny]: """ @@ -579,7 +570,6 @@ class Noder: @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 @raw: raw size output returns the found nodes @@ -598,16 +588,10 @@ class Noder: paths = {} for item in found: item.name = fix_badchars(item.name) - if hasattr(item, 'relpath'): - item.relpath = fix_badchars(item.relpath) storage = self._get_storage(item) - if parentfromtree: - parent = self._get_parents(item) - key = f'{storage}/{parent}/{item.relpath}' - paths[parent] = item - else: - key = f'{storage}/{item.path}' - paths[key] = item + parents = self._get_parents(item) + key = f'{storage}/{parents}/{item.name}' + paths[parents] = item # handle fzf mode if fmt.startswith('fzf'): @@ -626,7 +610,6 @@ class Noder: self._print_node_native(item, withpath=True, withdepth=True, withstorage=True, - recalcparent=parentfromtree, raw=raw) elif fmt.startswith('csv'): if fmt == 'csv-with-header': diff --git a/catcli/nodes.py b/catcli/nodes.py index b622410..6743958 100644 --- a/catcli/nodes.py +++ b/catcli/nodes.py @@ -99,7 +99,6 @@ class NodeFile(NodeAny): def __init__(self, # type: ignore[no-untyped-def] name: str, - relpath: str, nodesize: int, md5: str, maccess: float, @@ -109,7 +108,6 @@ class NodeFile(NodeAny): super().__init__() # type: ignore[no-untyped-call] self.name = name self.type = TYPE_FILE - self.relpath = relpath self.nodesize = nodesize self.md5 = md5 self.maccess = maccess @@ -126,7 +124,6 @@ class NodeDir(NodeAny): def __init__(self, # type: ignore[no-untyped-def] name: str, - relpath: str, nodesize: int, maccess: float, parent=None, @@ -135,7 +132,6 @@ class NodeDir(NodeAny): super().__init__() # type: ignore[no-untyped-call] self.name = name self.type = TYPE_DIR - self.relpath = relpath self.nodesize = nodesize self.maccess = maccess self.parent = parent @@ -151,7 +147,6 @@ class NodeArchived(NodeAny): def __init__(self, # type: ignore[no-untyped-def] name: str, - relpath: str, nodesize: int, md5: str, archive: str, @@ -161,7 +156,6 @@ class NodeArchived(NodeAny): super().__init__() # type: ignore[no-untyped-call] self.name = name self.type = TYPE_ARCHIVED - self.relpath = relpath self.nodesize = nodesize self.md5 = md5 self.archive = archive diff --git a/catcli/utils.py b/catcli/utils.py index 4111df8..4dca31b 100644 --- a/catcli/utils.py +++ b/catcli/utils.py @@ -16,13 +16,12 @@ from catcli import nodes from catcli.exceptions import CatcliException -SEPARATOR = '/' WILD = '*' def path_to_top(path: str) -> str: """path pivot under top""" - pre = f'{SEPARATOR}{nodes.NAME_TOP}' + pre = f'{os.path.sep}{nodes.NAME_TOP}' if not path.startswith(pre): # prepend with top node path path = pre + path @@ -32,16 +31,16 @@ def path_to_top(path: str) -> str: def path_to_search_all(path: str) -> str: """path to search for all subs""" if not path: - path = SEPARATOR - if not path.startswith(SEPARATOR): - path = SEPARATOR + path - pre = f'{SEPARATOR}{nodes.NAME_TOP}' + path = os.path.sep + if not path.startswith(os.path.sep): + path = os.path.sep + path + pre = f'{os.path.sep}{nodes.NAME_TOP}' if not path.startswith(pre): # prepend with top node path path = pre + path - if not path.endswith(SEPARATOR): + if not path.endswith(os.path.sep): # ensure ends with a separator - path += SEPARATOR + path += os.path.sep if not path.endswith(WILD): # add wild card path += WILD diff --git a/tests-ng/assets/github.catalog.json b/tests-ng/assets/github.catalog.json index ff4c561..4a13008 100644 --- a/tests-ng/assets/github.catalog.json +++ b/tests-ng/assets/github.catalog.json @@ -7,7 +7,6 @@ "maccess": 1666206037.0786593, "md5": "0c6407a84d412c514007313fb3bca4de", "name": "FUNDING.yml", - "relpath": "/FUNDING.yml", "size": 17, "type": "file" }, @@ -17,7 +16,6 @@ "maccess": 1666206037.078865, "md5": "57699a7a6a03e20e864f220e19f8e197", "name": "pypi-release.yml", - "relpath": "workflows/pypi-release.yml", "size": 691, "type": "file" }, @@ -25,14 +23,12 @@ "maccess": 1678375244.4870229, "md5": "7144a119ef43adb634654522c12ec250", "name": "testing.yml", - "relpath": "workflows/testing.yml", "size": 802, "type": "file" } ], "maccess": 1678375244.4865956, "name": "workflows", - "relpath": "/workflows", "size": 1493, "type": "dir" } diff --git a/tests/helpers.py b/tests/helpers.py index a6da349..ca66377 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -149,21 +149,18 @@ FAKECATALOG = """ { "md5": null, "name": "7544G", - "relpath": "tmpj5602ih7.catcli/7544G", "size": 100, "type": "file" }, { "md5": null, "name": "KF2ZC", - "relpath": "tmpj5602ih7.catcli/KF2ZC", "size": 100, "type": "file" }, { "md5": null, "name": "Z9OII", - "relpath": "tmpj5602ih7.catcli/Z9OII", "size": 100, "type": "file" }, @@ -172,14 +169,12 @@ FAKECATALOG = """ { "md5": null, "name": "M592O9", - "relpath": "tmpj5602ih7.catcli/VNN/M592O9", "size": 100, "type": "file" } ], "md5": null, "name": "VNN", - "relpath": "VNN", "size": 100, "type": "dir" }, @@ -188,21 +183,18 @@ FAKECATALOG = """ { "md5": null, "name": "X37H", - "relpath": "tmpj5602ih7.catcli/P4C/X37H", "size": 100, "type": "file" }, { "md5": null, "name": "I566", - "relpath": "tmpj5602ih7.catcli/P4C/I566", "size": 100, "type": "file" } ], "md5": null, "name": "P4C", - "relpath": "P4C", "size": 200, "type": "dir" }