diff --git a/catcli/catalog.py b/catcli/catalog.py index e415a39..65993d1 100644 --- a/catcli/catalog.py +++ b/catcli/catalog.py @@ -6,12 +6,12 @@ Class that represents the catcli catalog """ import os -import pickle from typing import Optional from anytree.exporter import JsonExporter # type: ignore from anytree.importer import JsonImporter # type: ignore # local imports +from catcli import nodes from catcli.nodes import NodeMeta, NodeTop from catcli.utils import ask from catcli.logger import Logger @@ -21,7 +21,6 @@ class Catalog: """the catalog""" def __init__(self, path: str, - usepickle: bool = False, debug: bool = False, force: bool = False) -> None: """ @@ -34,7 +33,6 @@ class Catalog: self.debug = debug self.force = force self.metanode: Optional[NodeMeta] = None - self.pickle = usepickle def set_metanode(self, metanode: NodeMeta) -> None: """remove the metanode until tree is re-written""" @@ -56,8 +54,6 @@ class Catalog: return None if not os.path.exists(self.path): return None - if self.pickle: - return self._restore_pickle() with open(self.path, 'r', encoding='UTF-8') as file: content = file.read() return self._restore_json(content) @@ -79,8 +75,6 @@ class Catalog: return False if self.metanode: self.metanode.parent = node - if self.pickle: - return self._save_pickle(node) return self._save_json(node) def _debug(self, text: str) -> None: @@ -88,38 +82,24 @@ class Catalog: return Logger.debug(text) - def _save_pickle(self, top: NodeTop) -> bool: - """pickle the catalog""" - with open(self.path, 'wb') as file: - pickle.dump(top, file) - self._debug(f'Catalog saved to pickle \"{self.path}\"') - return True - - def _restore_pickle(self) -> NodeTop: - """restore the pickled tree""" - with open(self.path, 'rb') as file: - root = pickle.load(file) - msg = f'Catalog imported from pickle \"{self.path}\"' - self._debug(msg) - top = NodeTop(root) - return top - def _save_json(self, top: NodeTop) -> bool: """export the catalog in json""" - Logger.debug(f'saving {top} to json...') + self._debug(f'saving {top} to json...') exp = JsonExporter(indent=2, sort_keys=True) with open(self.path, 'w', encoding='UTF-8') as file: exp.write(top, file) self._debug(f'Catalog saved to json \"{self.path}\"') return True - def _restore_json(self, string: str) -> NodeTop: + def _restore_json(self, string: str) -> Optional[NodeTop]: """restore the tree from json""" imp = JsonImporter() - Logger.debug(f'import from string: {string}') + self._debug(f'import from string: {string}') root = imp.import_(string) self._debug(f'Catalog imported from json \"{self.path}\"') - top = NodeTop(root) - Logger.debug(f'top imported: {top}') + self._debug(f'root imported: {root}') + if root.type != nodes.TYPE_TOP: + return None + top = NodeTop(root.name, children=root.children) + self._debug(f'top imported: {top}') return top - # return cast(NodeTop, root) diff --git a/catcli/catcli.py b/catcli/catcli.py index 0323f4f..46b4c6c 100755 --- a/catcli/catcli.py +++ b/catcli/catcli.py @@ -293,8 +293,8 @@ def print_supported_formats() -> None: print('"native" : native format') print('"csv" : CSV format') print(f' {Noder.CSV_HEADER}') - print('"fzf-native" : fzf to native (only for find)') - print('"fzf-csv" : fzf to csv (only for find)') + print('"fzf-native" : fzf to native (only valid for find)') + print('"fzf-csv" : fzf to csv (only valid for find)') def main() -> bool: diff --git a/catcli/fuser.py b/catcli/fuser.py index 4e8701c..6641bc2 100644 --- a/catcli/fuser.py +++ b/catcli/fuser.py @@ -91,17 +91,17 @@ class CatcliFilesystem(fuse.LoggingMixIn, fuse.Operations): # type: ignore curt = time() mode: Any = S_IFREG - if isinstance(entry, nodes.NodeArchived): + if entry.type == nodes.TYPE_ARCHIVED: mode = S_IFREG - elif isinstance(entry, nodes.NodeDir): + elif entry.type == nodes.TYPE_DIR: mode = S_IFDIR - elif isinstance(entry, nodes.NodeFile): + elif entry.type == nodes.TYPE_FILE: mode = S_IFREG - elif isinstance(entry, nodes.NodeStorage): + elif entry.type == nodes.TYPE_STORAGE: mode = S_IFDIR - elif isinstance(entry, nodes.NodeMeta): + elif entry.type == nodes.TYPE_META: mode = S_IFREG - elif isinstance(entry, nodes.NodeTop): + elif entry.type == nodes.TYPE_TOP: mode = S_IFREG return { 'st_mode': (mode), diff --git a/catcli/noder.py b/catcli/noder.py index 43677ed..996c733 100644 --- a/catcli/noder.py +++ b/catcli/noder.py @@ -67,7 +67,7 @@ class Noder: """ found = None for node in top.children: - if not isinstance(node, nodes.NodeStorage): + if node.type != nodes.TYPE_STORAGE: continue if node.name == name: found = node @@ -138,19 +138,19 @@ class Noder: recursively traverse tree and return size @store: store the size in the node """ - if isinstance(node, nodes.NodeFile): + if node.type == nodes.TYPE_FILE: self._debug(f'getting node size for \"{node.name}\"') return node.size msg = f'getting node size recursively for \"{node.name}\"' self._debug(msg) size: int = 0 for i in node.children: - if isinstance(node, nodes.NodeDir): + if node.type == nodes.TYPE_DIR: size = self.rec_size(i, store=store) if store: i.size = size size += size - if isinstance(node, nodes.NodeStorage): + if node.type == nodes.TYPE_STORAGE: size = self.rec_size(i, store=store) if store: i.size = size @@ -284,7 +284,7 @@ class Noder: def _get_meta_node(self, top: NodeTop) -> Optional[NodeMeta]: """return the meta node if any""" try: - found = next(filter(lambda x: isinstance(x, nodes.NodeMeta), + found = next(filter(lambda x: x.type == nodes.TYPE_META, top.children)) return cast(NodeMeta, found) except StopIteration: @@ -294,7 +294,7 @@ class Noder: """remove any node not flagged and clean flags""" cnt = 0 for node in anytree.PreOrderIter(top): - if not isinstance(node, (nodes.NodeDir, nodes.NodeFile)): + if node.type not in [nodes.TYPE_DIR, nodes.TYPE_FILE]: continue if self._clean(node): cnt += 1 @@ -361,7 +361,7 @@ class Noder: out.append(node.md5) # md5 else: out.append('') # fake md5 - if isinstance(node, nodes.NodeDir): + if node.type == nodes.TYPE_DIR: out.append(str(len(node.children))) # nbfiles else: out.append('') # fake nbfiles @@ -390,10 +390,10 @@ class Noder: @recalcparent: get relpath from tree instead of relpath field @raw: print raw size rather than human readable """ - if isinstance(node, nodes.NodeTop): + if node.type == nodes.TYPE_TOP: # top node Logger.stdout_nocolor(f'{pre}{node.name}') - elif isinstance(node, nodes.NodeFile): + elif node.type == nodes.TYPE_FILE: # node of type file name = node.name if withpath: @@ -413,7 +413,7 @@ class Noder: content = Logger.get_bold_text(storage.name) compl += f', storage:{content}' NodePrinter.print_file_native(pre, name, compl) - elif isinstance(node, nodes.NodeDir): + elif node.type == nodes.TYPE_DIR: # node of type directory name = node.name if withpath: @@ -433,7 +433,7 @@ class Noder: if withstorage: attr.append(('storage', Logger.get_bold_text(storage.name))) NodePrinter.print_dir_native(pre, name, depth=depth, attr=attr) - elif isinstance(node, nodes.NodeStorage): + elif node.type == nodes.TYPE_STORAGE: # node of type storage sztotal = size_to_str(node.total, raw=raw) szused = size_to_str(node.total - node.free, raw=raw) @@ -463,7 +463,7 @@ class Noder: name, argsstring, node.attr) - elif isinstance(node, nodes.NodeArchived): + elif node.type == nodes.TYPE_ARCHIVED: # archive node if self.arc: NodePrinter.print_archive_native(pre, node.name, node.archive) @@ -626,16 +626,16 @@ class Noder: def _callback_find_name(self, term: str, only_dir: bool) -> Any: """callback for finding files""" def find_name(node: NodeAny) -> bool: - if isinstance(node, nodes.NodeStorage): + if node.type == nodes.TYPE_STORAGE: # ignore storage nodes return False - if isinstance(node, nodes.NodeTop): + if node.type == nodes.TYPE_TOP: # ignore top nodes return False - if isinstance(node, nodes.NodeMeta): + if node.type == nodes.TYPE_META: # ignore meta nodes return False - if only_dir and isinstance(node, nodes.NodeDir): + if only_dir and node.type == nodes.TYPE_DIR: # ignore non directory return False @@ -773,7 +773,7 @@ class Noder: def _get_storage(self, node: NodeAny) -> NodeStorage: """recursively traverse up to find storage""" - if isinstance(node, nodes.NodeStorage): + if node.type == nodes.TYPE_STORAGE: return node return cast(NodeStorage, node.ancestors[1]) @@ -784,9 +784,9 @@ class Noder: def _get_parents(self, node: NodeAny) -> str: """get all parents recursively""" - if isinstance(node, nodes.NodeStorage): + if node.type == nodes.TYPE_STORAGE: return '' - if isinstance(node, nodes.NodeTop): + if node.type == nodes.TYPE_TOP: return '' parent = self._get_parents(node.parent) if parent: diff --git a/catcli/nodes.py b/catcli/nodes.py index 76308ec..e8f30ba 100644 --- a/catcli/nodes.py +++ b/catcli/nodes.py @@ -10,12 +10,12 @@ from typing import Dict, Any from anytree import NodeMixin # type: ignore -_TYPE_TOP = 'top' -_TYPE_FILE = 'file' -_TYPE_DIR = 'dir' -_TYPE_ARC = 'arc' -_TYPE_STORAGE = 'storage' -_TYPE_META = 'meta' +TYPE_TOP = 'top' +TYPE_FILE = 'file' +TYPE_DIR = 'dir' +TYPE_ARCHIVED = 'arc' +TYPE_STORAGE = 'storage' +TYPE_META = 'meta' NAME_TOP = 'top' NAME_META = 'meta' @@ -69,7 +69,7 @@ class NodeTop(NodeAny): """build a top node""" super().__init__() # type: ignore[no-untyped-call] self.name = name - self.type = _TYPE_TOP + self.type = TYPE_TOP self.parent = None if children: self.children = children @@ -92,7 +92,7 @@ class NodeFile(NodeAny): """build a file node""" super().__init__() # type: ignore[no-untyped-call] self.name = name - self.type = _TYPE_FILE + self.type = TYPE_FILE self.relpath = relpath self.size = size self.md5 = md5 @@ -118,7 +118,7 @@ class NodeDir(NodeAny): """build a directory node""" super().__init__() # type: ignore[no-untyped-call] self.name = name - self.type = _TYPE_DIR + self.type = TYPE_DIR self.relpath = relpath self.size = size self.maccess = maccess @@ -144,7 +144,7 @@ class NodeArchived(NodeAny): """build an archived node""" super().__init__() # type: ignore[no-untyped-call] self.name = name - self.type = _TYPE_ARC + self.type = TYPE_ARCHIVED self.relpath = relpath self.size = size self.md5 = md5 @@ -172,12 +172,12 @@ class NodeStorage(NodeAny): """build a storage node""" super().__init__() # type: ignore[no-untyped-call] self.name = name - self.type = _TYPE_STORAGE + self.type = TYPE_STORAGE self.free = free self.total = total self.attr = attr self.size = size - self.ts = ts + self.ts = ts # pylint: disable=C0103 self.parent = parent if children: self.children = children @@ -197,7 +197,7 @@ class NodeMeta(NodeAny): """build a meta node""" super().__init__() # type: ignore[no-untyped-call] self.name = name - self.type = _TYPE_META + self.type = TYPE_META self.attr = attr self.parent = parent if children: