Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
deadc0de6 committed Feb 17, 2024
1 parent e6ca6e2 commit c9b4043
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 90 deletions.
2 changes: 1 addition & 1 deletion catcli/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def _restore_json(self, string: str) -> Optional[NodeTop]:
if root.type != nodes.TYPE_TOP:
return None
top = NodeTop(root.name, children=root.children)
self._debug(f'top imported: {top.name}')
self._debug(f'top imported: {top.get_name()}')
return top


Expand Down
15 changes: 8 additions & 7 deletions catcli/catcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from catcli.catalog import Catalog
from catcli.walker import Walker
from catcli.noder import Noder
from catcli.utils import ask, edit, path_to_search_all
from catcli.utils import ask, edit
from catcli.nodes_utils import path_to_search_all
from catcli.exceptions import BadFormatException, CatcliException

NAME = 'catcli'
Expand Down Expand Up @@ -241,7 +242,7 @@ def cmd_find(args: Dict[str, Any],
script = args['--script']
search_for = args['<term>']
if args['--verbose']:
Logger.debug(f'search for \"{search_for}\" under \"{top.name}\"')
Logger.debug(f'search for "{search_for}" under "{top.get_name()}"')
found = noder.find(top, search_for,
script=script,
startnode=startpath,
Expand Down Expand Up @@ -279,10 +280,10 @@ def cmd_rename(args: Dict[str, Any],
"""rename action"""
storage = args['<storage>']
new = args['<name>']
storages = list(x.name for x in top.children)
storages = list(x.get_name() for x in top.children)
if storage in storages:
node = next(filter(lambda x: x.name == storage, top.children))
node.name = new
node = next(filter(lambda x: x.get_name() == storage, top.children))
node.set_name(new)
if catalog.save(top):
msg = f'Storage \"{storage}\" renamed to \"{new}\"'
Logger.info(msg)
Expand All @@ -296,9 +297,9 @@ def cmd_edit(args: Dict[str, Any],
top: NodeTop) -> None:
"""edit action"""
storage = args['<storage>']
storages = list(x.name for x in top.children)
storages = list(x.get_name() for x in top.children)
if storage in storages:
node = next(filter(lambda x: x.name == storage, top.children))
node = next(filter(lambda x: x.get_name() == storage, top.children))
attr = node.attr
if not attr:
attr = ''
Expand Down
4 changes: 2 additions & 2 deletions catcli/fuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# local imports
from catcli.noder import Noder
from catcli.nodes import NodeTop, NodeAny
from catcli.utils import path_to_search_all, path_to_top
from catcli.nodes_utils import path_to_search_all, path_to_top
from catcli import nodes


Expand Down Expand Up @@ -129,5 +129,5 @@ def readdir(self, path: str, _fh: Any) -> List[str]:
content = ['.', '..']
entries = self._get_entries(path)
for entry in entries:
content.append(entry.name)
content.append(entry.get_name())
return content
20 changes: 10 additions & 10 deletions catcli/noder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from catcli.nodes import NodeAny, NodeStorage, \
NodeTop, NodeFile, NodeArchived, NodeDir, NodeMeta, \
typcast_node
from catcli.utils import md5sum, fix_badchars, has_attr
from catcli.utils import md5sum
from catcli.logger import Logger
from catcli.printer_native import NativePrinter
from catcli.printer_csv import CsvPrinter
Expand Down Expand Up @@ -117,7 +117,7 @@ def get_node_if_changed(self,
return node, False
# force re-indexing if no maccess
maccess = os.path.getmtime(path)
if not has_attr(node, 'maccess') or \
if not node.has_attr('maccess') or \
not node.maccess:
self._debug('\tchange: no maccess found')
return node, True
Expand Down Expand Up @@ -336,7 +336,7 @@ def _print_node_native(self, node: NodeAny,
typcast_node(node)
if node.type == nodes.TYPE_TOP:
# top node
self.native_printer.print_top(pre, node.name)
self.native_printer.print_top(pre, node.get_name())
elif node.type == nodes.TYPE_FILE:
# node of type file
self.native_printer.print_file(pre, node,
Expand Down Expand Up @@ -420,7 +420,7 @@ def _to_fzf(self, node: NodeAny, fmt: str) -> None:
continue
parents = rend.get_fullpath()
storage = rend.get_storage_node()
fullpath = os.path.join(storage.name, parents)
fullpath = os.path.join(storage.get_name(), parents)
the_nodes[fullpath] = rend
# prompt with fzf
paths = self._fzf_prompt(the_nodes.keys())
Expand Down Expand Up @@ -477,7 +477,7 @@ def find(self, top: NodeTop,
paths = {}
for item in found:
typcast_node(item)
item.name = fix_badchars(item.name)
item.set_name(item.get_name())
key = item.get_fullpath()
paths[key] = item

Expand Down Expand Up @@ -574,7 +574,7 @@ def list(self, top: NodeTop,
@fmt: output format
@raw: print raw size
"""
self._debug(f'ls walking path: \"{path}\" from \"{top.name}\"')
self._debug(f'ls walking path: \"{path}\" from \"{top.get_name()}\"')
resolv = anytree.resolver.Resolver('name')
found = []
try:
Expand Down Expand Up @@ -633,7 +633,7 @@ def diskusage(self, top: NodeTop,
path: str,
raw: bool = False) -> List[NodeAny]:
"""disk usage"""
self._debug(f'du walking path: \"{path}\" from \"{top.name}\"')
self._debug(f'du walking path: \"{path}\" from \"{top.get_name()}\"')
resolv = anytree.resolver.Resolver('name')
found: NodeAny
try:
Expand All @@ -660,15 +660,15 @@ def _add_entry(self, name: str,
"""add an entry to the tree"""
entries = name.rstrip(os.sep).split(os.sep)
if len(entries) == 1:
self.new_archive_node(name, top, top.name)
self.new_archive_node(name, top, top.get_name())
return
sub = os.sep.join(entries[:-1])
nodename = entries[-1]
try:
parent = resolv.get(top, sub)
parent = self.new_archive_node(nodename, parent, top.name)
parent = self.new_archive_node(nodename, parent, top.get_name())
except anytree.resolver.ChildResolverError:
self.new_archive_node(nodename, top, top.name)
self.new_archive_node(nodename, top, top.get_name())

def list_to_tree(self, parent: NodeAny, names: List[str]) -> None:
"""convert list of files to a tree"""
Expand Down
17 changes: 15 additions & 2 deletions catcli/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from anytree import NodeMixin

from catcli.exceptions import CatcliException
from catcli.utils import fix_badchars


TYPE_TOP = 'top'
Expand Down Expand Up @@ -58,6 +59,18 @@ def __init__(self, # type: ignore[no-untyped-def]
if children:
self.children = children

def get_name(self) -> str:
"""get node name"""
return fix_badchars(self.name)

def set_name(self, name: str) -> None:
"""set node name"""
self.name = fix_badchars(name)

def has_attr(self, attr: str) -> bool:
"""return True if node has attr as attribute"""
return attr in self.__dict__

def may_have_children(self) -> bool:
"""can node contains sub"""
raise NotImplementedError
Expand All @@ -75,12 +88,12 @@ def __str__(self) -> str:

def get_fullpath(self) -> str:
"""return full path to this node"""
path = self.name
path = self.get_name()
if self.parent:
typcast_node(self.parent)
ppath = self.parent.get_fullpath()
path = os.path.join(ppath, path)
return str(path)
return fix_badchars(path)

def get_rec_size(self) -> int:
"""recursively traverse tree and return size"""
Expand Down
39 changes: 39 additions & 0 deletions catcli/nodes_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2024, deadc0de6
nodes helpers
"""

import os

# local imports
from catcli import nodes


def path_to_top(path: str) -> str:
"""path pivot under top"""
pre = f"{os.path.sep}{nodes.NAME_TOP}"
if not path.startswith(pre):
# prepend with top node path
path = pre + path
return path


def path_to_search_all(path: str) -> str:
"""path to search for all subs"""
if not path:
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(os.path.sep):
# # ensure ends with a separator
# path += os.path.sep
# if not path.endswith(WILD):
# # add wild card
# path += WILD
return path
11 changes: 5 additions & 6 deletions catcli/printer_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from typing import List

from catcli.nodes import NodeAny, NodeStorage, TYPE_DIR
from catcli.utils import size_to_str, epoch_to_str, \
has_attr
from catcli.utils import size_to_str, epoch_to_str


class CsvPrinter:
Expand All @@ -35,7 +34,7 @@ def print_storage(self, node: NodeStorage,
raw: bool = False) -> None:
"""print a storage node"""
out = []
out.append(node.name) # name
out.append(node.get_name()) # name
out.append(node.type) # type
out.append('') # fake full path
size = node.get_rec_size()
Expand All @@ -56,19 +55,19 @@ def print_node(self, node: NodeAny,
raw: bool = False) -> None:
"""print other nodes"""
out = []
out.append(node.name.replace('"', '""')) # name
out.append(node.get_name().replace('"', '""')) # name
out.append(node.type) # type
fullpath = node.get_fullpath()
out.append(fullpath.replace('"', '""')) # full path

out.append(size_to_str(node.nodesize, raw=raw)) # size
storage = node.get_storage_node()
out.append(epoch_to_str(storage.ts)) # indexed_at
if has_attr(node, 'maccess'):
if node.has_attr('maccess'):
out.append(epoch_to_str(node.maccess)) # maccess
else:
out.append('') # fake maccess
if has_attr(node, 'md5'):
if node.has_attr('md5'):
out.append(node.md5) # md5
else:
out.append('') # fake md5
Expand Down
19 changes: 9 additions & 10 deletions catcli/printer_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from catcli.colors import Colors
from catcli.logger import Logger
from catcli.utils import fix_badchars, size_to_str, \
has_attr, epoch_to_str
epoch_to_str


COLOR_STORAGE = Colors.YELLOW
Expand Down Expand Up @@ -54,8 +54,7 @@ def print_storage(self, pre: str,
raw: bool = False) -> None:
"""print a storage node"""
# construct name
name = node.name
name = fix_badchars(name)
name = node.get_name()
# construct attrs
attrs = []
# nb files
Expand All @@ -74,7 +73,7 @@ def print_storage(self, pre: str,
szused = size_to_str(node.total - node.free, raw=raw)
attrs.append(f'du:{szused}/{sztotal}')
# timestamp
if has_attr(node, 'ts'):
if node.has_attr('ts'):
attrs.append(f'date:{epoch_to_str(node.ts)}')

# print
Expand All @@ -91,7 +90,7 @@ def print_file(self, pre: str,
raw: bool = False) -> None:
"""print a file node"""
# construct name
name = node.name
name = node.get_name()
storage = node.get_storage_node()
if withpath:
name = node.get_fullpath()
Expand All @@ -100,7 +99,7 @@ def print_file(self, pre: str,
if node.md5:
attrs.append(f'md5:{node.md5}')
if withstorage:
content = Logger.get_bold_text(storage.name)
content = Logger.get_bold_text(storage.get_name())
attrs.append(f'storage:{content}')
# print
out = []
Expand All @@ -111,7 +110,7 @@ def print_file(self, pre: str,
size = node.nodesize
line = size_to_str(size, raw=raw)
out.append(f'{COLOR_SIZE}{line}{Colors.RESET}')
if has_attr(node, 'maccess'):
if node.has_attr('maccess'):
line = epoch_to_str(node.maccess)
out.append(f'{COLOR_TS}{line}{Colors.RESET}')
if attrs:
Expand All @@ -128,7 +127,7 @@ def print_dir(self, pre: str,
raw: bool = False) -> None:
"""print a directory node"""
# construct name
name = node.name
name = node.get_name()
storage = node.get_storage_node()
if withpath:
name = node.get_fullpath()
Expand All @@ -138,7 +137,7 @@ def print_dir(self, pre: str,
nbchildren = len(node.children)
attrs.append(f'{self.NBFILES}:{nbchildren}')
if withstorage:
attrs.append(f'storage:{Logger.get_bold_text(storage.name)}')
attrs.append(f"storage:{Logger.get_bold_text(storage.get_name())}")
# print
out = []
out.append(f'{pre}')
Expand All @@ -148,7 +147,7 @@ def print_dir(self, pre: str,
size = node.nodesize
line = size_to_str(size, raw=raw)
out.append(f'{COLOR_SIZE}{line}{Colors.RESET}')
if has_attr(node, 'maccess'):
if node.has_attr('maccess'):
line = epoch_to_str(node.maccess)
out.append(f'{COLOR_TS}{line}{Colors.RESET}')
if attrs:
Expand Down
Loading

0 comments on commit c9b4043

Please sign in to comment.