fix formats

pull/30/head
deadc0de6 1 year ago
parent a333f60fa7
commit d294aa59e1

@ -6,12 +6,12 @@ Class that represents the catcli catalog
""" """
import os import os
import pickle
from typing import Optional from typing import Optional
from anytree.exporter import JsonExporter # type: ignore from anytree.exporter import JsonExporter # type: ignore
from anytree.importer import JsonImporter # type: ignore from anytree.importer import JsonImporter # type: ignore
# local imports # local imports
from catcli import nodes
from catcli.nodes import NodeMeta, NodeTop from catcli.nodes import NodeMeta, NodeTop
from catcli.utils import ask from catcli.utils import ask
from catcli.logger import Logger from catcli.logger import Logger
@ -21,7 +21,6 @@ class Catalog:
"""the catalog""" """the catalog"""
def __init__(self, path: str, def __init__(self, path: str,
usepickle: bool = False,
debug: bool = False, debug: bool = False,
force: bool = False) -> None: force: bool = False) -> None:
""" """
@ -34,7 +33,6 @@ class Catalog:
self.debug = debug self.debug = debug
self.force = force self.force = force
self.metanode: Optional[NodeMeta] = None self.metanode: Optional[NodeMeta] = None
self.pickle = usepickle
def set_metanode(self, metanode: NodeMeta) -> None: def set_metanode(self, metanode: NodeMeta) -> None:
"""remove the metanode until tree is re-written""" """remove the metanode until tree is re-written"""
@ -56,8 +54,6 @@ class Catalog:
return None return None
if not os.path.exists(self.path): if not os.path.exists(self.path):
return None return None
if self.pickle:
return self._restore_pickle()
with open(self.path, 'r', encoding='UTF-8') as file: with open(self.path, 'r', encoding='UTF-8') as file:
content = file.read() content = file.read()
return self._restore_json(content) return self._restore_json(content)
@ -79,8 +75,6 @@ class Catalog:
return False return False
if self.metanode: if self.metanode:
self.metanode.parent = node self.metanode.parent = node
if self.pickle:
return self._save_pickle(node)
return self._save_json(node) return self._save_json(node)
def _debug(self, text: str) -> None: def _debug(self, text: str) -> None:
@ -88,38 +82,24 @@ class Catalog:
return return
Logger.debug(text) 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: def _save_json(self, top: NodeTop) -> bool:
"""export the catalog in json""" """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) exp = JsonExporter(indent=2, sort_keys=True)
with open(self.path, 'w', encoding='UTF-8') as file: with open(self.path, 'w', encoding='UTF-8') as file:
exp.write(top, file) exp.write(top, file)
self._debug(f'Catalog saved to json \"{self.path}\"') self._debug(f'Catalog saved to json \"{self.path}\"')
return True return True
def _restore_json(self, string: str) -> NodeTop: def _restore_json(self, string: str) -> Optional[NodeTop]:
"""restore the tree from json""" """restore the tree from json"""
imp = JsonImporter() imp = JsonImporter()
Logger.debug(f'import from string: {string}') self._debug(f'import from string: {string}')
root = imp.import_(string) root = imp.import_(string)
self._debug(f'Catalog imported from json \"{self.path}\"') self._debug(f'Catalog imported from json \"{self.path}\"')
top = NodeTop(root) self._debug(f'root imported: {root}')
Logger.debug(f'top imported: {top}') if root.type != nodes.TYPE_TOP:
return None
top = NodeTop(root.name, children=root.children)
self._debug(f'top imported: {top}')
return top return top
# return cast(NodeTop, root)

@ -293,8 +293,8 @@ def print_supported_formats() -> None:
print('"native" : native format') print('"native" : native format')
print('"csv" : CSV format') print('"csv" : CSV format')
print(f' {Noder.CSV_HEADER}') print(f' {Noder.CSV_HEADER}')
print('"fzf-native" : fzf to native (only for find)') print('"fzf-native" : fzf to native (only valid for find)')
print('"fzf-csv" : fzf to csv (only for find)') print('"fzf-csv" : fzf to csv (only valid for find)')
def main() -> bool: def main() -> bool:

@ -91,17 +91,17 @@ class CatcliFilesystem(fuse.LoggingMixIn, fuse.Operations): # type: ignore
curt = time() curt = time()
mode: Any = S_IFREG mode: Any = S_IFREG
if isinstance(entry, nodes.NodeArchived): if entry.type == nodes.TYPE_ARCHIVED:
mode = S_IFREG mode = S_IFREG
elif isinstance(entry, nodes.NodeDir): elif entry.type == nodes.TYPE_DIR:
mode = S_IFDIR mode = S_IFDIR
elif isinstance(entry, nodes.NodeFile): elif entry.type == nodes.TYPE_FILE:
mode = S_IFREG mode = S_IFREG
elif isinstance(entry, nodes.NodeStorage): elif entry.type == nodes.TYPE_STORAGE:
mode = S_IFDIR mode = S_IFDIR
elif isinstance(entry, nodes.NodeMeta): elif entry.type == nodes.TYPE_META:
mode = S_IFREG mode = S_IFREG
elif isinstance(entry, nodes.NodeTop): elif entry.type == nodes.TYPE_TOP:
mode = S_IFREG mode = S_IFREG
return { return {
'st_mode': (mode), 'st_mode': (mode),

@ -67,7 +67,7 @@ class Noder:
""" """
found = None found = None
for node in top.children: for node in top.children:
if not isinstance(node, nodes.NodeStorage): if node.type != nodes.TYPE_STORAGE:
continue continue
if node.name == name: if node.name == name:
found = node found = node
@ -138,19 +138,19 @@ class Noder:
recursively traverse tree and return size recursively traverse tree and return size
@store: store the size in the node @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}\"') self._debug(f'getting node size for \"{node.name}\"')
return node.size return node.size
msg = f'getting node size recursively for \"{node.name}\"' msg = f'getting node size recursively for \"{node.name}\"'
self._debug(msg) self._debug(msg)
size: int = 0 size: int = 0
for i in node.children: for i in node.children:
if isinstance(node, nodes.NodeDir): if node.type == nodes.TYPE_DIR:
size = self.rec_size(i, store=store) size = self.rec_size(i, store=store)
if store: if store:
i.size = size i.size = size
size += size size += size
if isinstance(node, nodes.NodeStorage): if node.type == nodes.TYPE_STORAGE:
size = self.rec_size(i, store=store) size = self.rec_size(i, store=store)
if store: if store:
i.size = size i.size = size
@ -284,7 +284,7 @@ class Noder:
def _get_meta_node(self, top: NodeTop) -> Optional[NodeMeta]: def _get_meta_node(self, top: NodeTop) -> Optional[NodeMeta]:
"""return the meta node if any""" """return the meta node if any"""
try: try:
found = next(filter(lambda x: isinstance(x, nodes.NodeMeta), found = next(filter(lambda x: x.type == nodes.TYPE_META,
top.children)) top.children))
return cast(NodeMeta, found) return cast(NodeMeta, found)
except StopIteration: except StopIteration:
@ -294,7 +294,7 @@ class Noder:
"""remove any node not flagged and clean flags""" """remove any node not flagged and clean flags"""
cnt = 0 cnt = 0
for node in anytree.PreOrderIter(top): 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 continue
if self._clean(node): if self._clean(node):
cnt += 1 cnt += 1
@ -361,7 +361,7 @@ class Noder:
out.append(node.md5) # md5 out.append(node.md5) # md5
else: else:
out.append('') # fake md5 out.append('') # fake md5
if isinstance(node, nodes.NodeDir): if node.type == nodes.TYPE_DIR:
out.append(str(len(node.children))) # nbfiles out.append(str(len(node.children))) # nbfiles
else: else:
out.append('') # fake nbfiles out.append('') # fake nbfiles
@ -390,10 +390,10 @@ class Noder:
@recalcparent: get relpath from tree instead of relpath field @recalcparent: get relpath from tree instead of relpath field
@raw: print raw size rather than human readable @raw: print raw size rather than human readable
""" """
if isinstance(node, nodes.NodeTop): if node.type == nodes.TYPE_TOP:
# top node # top node
Logger.stdout_nocolor(f'{pre}{node.name}') Logger.stdout_nocolor(f'{pre}{node.name}')
elif isinstance(node, nodes.NodeFile): elif node.type == nodes.TYPE_FILE:
# node of type file # node of type file
name = node.name name = node.name
if withpath: if withpath:
@ -413,7 +413,7 @@ class Noder:
content = Logger.get_bold_text(storage.name) content = Logger.get_bold_text(storage.name)
compl += f', storage:{content}' compl += f', storage:{content}'
NodePrinter.print_file_native(pre, name, compl) NodePrinter.print_file_native(pre, name, compl)
elif isinstance(node, nodes.NodeDir): elif node.type == nodes.TYPE_DIR:
# node of type directory # node of type directory
name = node.name name = node.name
if withpath: if withpath:
@ -433,7 +433,7 @@ class Noder:
if withstorage: if withstorage:
attr.append(('storage', Logger.get_bold_text(storage.name))) attr.append(('storage', Logger.get_bold_text(storage.name)))
NodePrinter.print_dir_native(pre, name, depth=depth, attr=attr) 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 # node of type storage
sztotal = size_to_str(node.total, raw=raw) sztotal = size_to_str(node.total, raw=raw)
szused = size_to_str(node.total - node.free, raw=raw) szused = size_to_str(node.total - node.free, raw=raw)
@ -463,7 +463,7 @@ class Noder:
name, name,
argsstring, argsstring,
node.attr) node.attr)
elif isinstance(node, nodes.NodeArchived): elif node.type == nodes.TYPE_ARCHIVED:
# archive node # archive node
if self.arc: if self.arc:
NodePrinter.print_archive_native(pre, node.name, node.archive) 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: def _callback_find_name(self, term: str, only_dir: bool) -> Any:
"""callback for finding files""" """callback for finding files"""
def find_name(node: NodeAny) -> bool: def find_name(node: NodeAny) -> bool:
if isinstance(node, nodes.NodeStorage): if node.type == nodes.TYPE_STORAGE:
# ignore storage nodes # ignore storage nodes
return False return False
if isinstance(node, nodes.NodeTop): if node.type == nodes.TYPE_TOP:
# ignore top nodes # ignore top nodes
return False return False
if isinstance(node, nodes.NodeMeta): if node.type == nodes.TYPE_META:
# ignore meta nodes # ignore meta nodes
return False return False
if only_dir and isinstance(node, nodes.NodeDir): if only_dir and node.type == nodes.TYPE_DIR:
# ignore non directory # ignore non directory
return False return False
@ -773,7 +773,7 @@ class Noder:
def _get_storage(self, node: NodeAny) -> NodeStorage: def _get_storage(self, node: NodeAny) -> NodeStorage:
"""recursively traverse up to find storage""" """recursively traverse up to find storage"""
if isinstance(node, nodes.NodeStorage): if node.type == nodes.TYPE_STORAGE:
return node return node
return cast(NodeStorage, node.ancestors[1]) return cast(NodeStorage, node.ancestors[1])
@ -784,9 +784,9 @@ class Noder:
def _get_parents(self, node: NodeAny) -> str: def _get_parents(self, node: NodeAny) -> str:
"""get all parents recursively""" """get all parents recursively"""
if isinstance(node, nodes.NodeStorage): if node.type == nodes.TYPE_STORAGE:
return '' return ''
if isinstance(node, nodes.NodeTop): if node.type == nodes.TYPE_TOP:
return '' return ''
parent = self._get_parents(node.parent) parent = self._get_parents(node.parent)
if parent: if parent:

@ -10,12 +10,12 @@ from typing import Dict, Any
from anytree import NodeMixin # type: ignore from anytree import NodeMixin # type: ignore
_TYPE_TOP = 'top' TYPE_TOP = 'top'
_TYPE_FILE = 'file' TYPE_FILE = 'file'
_TYPE_DIR = 'dir' TYPE_DIR = 'dir'
_TYPE_ARC = 'arc' TYPE_ARCHIVED = 'arc'
_TYPE_STORAGE = 'storage' TYPE_STORAGE = 'storage'
_TYPE_META = 'meta' TYPE_META = 'meta'
NAME_TOP = 'top' NAME_TOP = 'top'
NAME_META = 'meta' NAME_META = 'meta'
@ -69,7 +69,7 @@ class NodeTop(NodeAny):
"""build a top node""" """build a top node"""
super().__init__() # type: ignore[no-untyped-call] super().__init__() # type: ignore[no-untyped-call]
self.name = name self.name = name
self.type = _TYPE_TOP self.type = TYPE_TOP
self.parent = None self.parent = None
if children: if children:
self.children = children self.children = children
@ -92,7 +92,7 @@ class NodeFile(NodeAny):
"""build a file node""" """build a file node"""
super().__init__() # type: ignore[no-untyped-call] super().__init__() # type: ignore[no-untyped-call]
self.name = name self.name = name
self.type = _TYPE_FILE self.type = TYPE_FILE
self.relpath = relpath self.relpath = relpath
self.size = size self.size = size
self.md5 = md5 self.md5 = md5
@ -118,7 +118,7 @@ class NodeDir(NodeAny):
"""build a directory node""" """build a directory node"""
super().__init__() # type: ignore[no-untyped-call] super().__init__() # type: ignore[no-untyped-call]
self.name = name self.name = name
self.type = _TYPE_DIR self.type = TYPE_DIR
self.relpath = relpath self.relpath = relpath
self.size = size self.size = size
self.maccess = maccess self.maccess = maccess
@ -144,7 +144,7 @@ class NodeArchived(NodeAny):
"""build an archived node""" """build an archived node"""
super().__init__() # type: ignore[no-untyped-call] super().__init__() # type: ignore[no-untyped-call]
self.name = name self.name = name
self.type = _TYPE_ARC self.type = TYPE_ARCHIVED
self.relpath = relpath self.relpath = relpath
self.size = size self.size = size
self.md5 = md5 self.md5 = md5
@ -172,12 +172,12 @@ class NodeStorage(NodeAny):
"""build a storage node""" """build a storage node"""
super().__init__() # type: ignore[no-untyped-call] super().__init__() # type: ignore[no-untyped-call]
self.name = name self.name = name
self.type = _TYPE_STORAGE self.type = TYPE_STORAGE
self.free = free self.free = free
self.total = total self.total = total
self.attr = attr self.attr = attr
self.size = size self.size = size
self.ts = ts self.ts = ts # pylint: disable=C0103
self.parent = parent self.parent = parent
if children: if children:
self.children = children self.children = children
@ -197,7 +197,7 @@ class NodeMeta(NodeAny):
"""build a meta node""" """build a meta node"""
super().__init__() # type: ignore[no-untyped-call] super().__init__() # type: ignore[no-untyped-call]
self.name = name self.name = name
self.type = _TYPE_META self.type = TYPE_META
self.attr = attr self.attr = attr
self.parent = parent self.parent = parent
if children: if children:

Loading…
Cancel
Save