mirror of https://github.com/deadc0de6/catcli
refactoring
parent
691396c96a
commit
0dcbfa94bd
@ -1,73 +0,0 @@
|
|||||||
"""
|
|
||||||
author: deadc0de6 (https://github.com/deadc0de6)
|
|
||||||
Copyright (c) 2022, deadc0de6
|
|
||||||
|
|
||||||
Class for printing nodes
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
from typing import TypeVar, Type, Optional, Tuple, List, \
|
|
||||||
Dict, Any
|
|
||||||
|
|
||||||
from catcli.colors import Colors
|
|
||||||
from catcli.utils import fix_badchars
|
|
||||||
|
|
||||||
|
|
||||||
CLASSTYPE = TypeVar('CLASSTYPE', bound='NodePrinter')
|
|
||||||
|
|
||||||
|
|
||||||
class NodePrinter:
|
|
||||||
"""a node printer class"""
|
|
||||||
|
|
||||||
STORAGE = 'storage'
|
|
||||||
ARCHIVE = 'archive'
|
|
||||||
NBFILES = 'nbfiles'
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def print_storage_native(cls: Type[CLASSTYPE], pre: str,
|
|
||||||
name: str, args: str,
|
|
||||||
attr: Dict[str, Any]) -> None:
|
|
||||||
"""print a storage node"""
|
|
||||||
end = ''
|
|
||||||
if attr:
|
|
||||||
end = f' {Colors.GRAY}({attr}){Colors.RESET}'
|
|
||||||
out = f'{pre}{Colors.UND}{cls.STORAGE}{Colors.RESET}:'
|
|
||||||
out += ' ' + Colors.PURPLE + fix_badchars(name) + \
|
|
||||||
Colors.RESET + end + '\n'
|
|
||||||
out += f' {Colors.GRAY}{args}{Colors.RESET}'
|
|
||||||
sys.stdout.write(f'{out}\n')
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def print_file_native(cls: Type[CLASSTYPE], pre: str,
|
|
||||||
name: str, attr: str) -> None:
|
|
||||||
"""print a file node"""
|
|
||||||
nobad = fix_badchars(name)
|
|
||||||
out = f'{pre}{nobad}'
|
|
||||||
out += f' {Colors.GRAY}[{attr}]{Colors.RESET}'
|
|
||||||
sys.stdout.write(f'{out}\n')
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def print_dir_native(cls: Type[CLASSTYPE], pre: str,
|
|
||||||
name: str,
|
|
||||||
nbchildren: int = 0,
|
|
||||||
attr: Optional[List[Tuple[str, str]]] = None) -> None:
|
|
||||||
"""print a directory node"""
|
|
||||||
end = []
|
|
||||||
if nbchildren > 0:
|
|
||||||
end.append(f'{cls.NBFILES}:{nbchildren}')
|
|
||||||
if attr:
|
|
||||||
end.append(' '.join([f'{x}:{y}' for x, y in attr]))
|
|
||||||
end_string = ''
|
|
||||||
if end:
|
|
||||||
end_string = f' [{", ".join(end)}]'
|
|
||||||
out = pre + Colors.BLUE + fix_badchars(name) + Colors.RESET
|
|
||||||
out += f'{Colors.GRAY}{end_string}{Colors.RESET}'
|
|
||||||
sys.stdout.write(f'{out}\n')
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def print_archive_native(cls: Type[CLASSTYPE], pre: str,
|
|
||||||
name: str, archive: str) -> None:
|
|
||||||
"""archive to stdout"""
|
|
||||||
out = pre + Colors.YELLOW + fix_badchars(name) + Colors.RESET
|
|
||||||
out += f' {Colors.GRAY}[{cls.ARCHIVE}:{archive}]{Colors.RESET}'
|
|
||||||
sys.stdout.write(f'{out}\n')
|
|
@ -0,0 +1,84 @@
|
|||||||
|
"""
|
||||||
|
author: deadc0de6 (https://github.com/deadc0de6)
|
||||||
|
Copyright (c) 2024, deadc0de6
|
||||||
|
|
||||||
|
Class for printing nodes in csv format
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
from catcli.nodes import NodeAny, NodeStorage, TYPE_DIR
|
||||||
|
from catcli.utils import size_to_str, epoch_to_str, \
|
||||||
|
has_attr
|
||||||
|
|
||||||
|
|
||||||
|
class CsvPrinter:
|
||||||
|
"""a node printer class"""
|
||||||
|
|
||||||
|
DEFSEP = ','
|
||||||
|
CSV_HEADER = ('name,type,path,size,indexed_at,'
|
||||||
|
'maccess,md5,nbfiles,free_space,'
|
||||||
|
'total_space,meta')
|
||||||
|
|
||||||
|
def _print_entries(self, entries: List[str], sep: str = DEFSEP) -> None:
|
||||||
|
line = sep.join(['"' + o + '"' for o in entries])
|
||||||
|
if len(line) > 0:
|
||||||
|
sys.stdout.write(f'{line}\n')
|
||||||
|
|
||||||
|
def print_header(self) -> None:
|
||||||
|
"""print csv header"""
|
||||||
|
sys.stdout.write(f'{self.CSV_HEADER}\n')
|
||||||
|
|
||||||
|
def print_storage(self, node: NodeStorage,
|
||||||
|
sep: str = DEFSEP,
|
||||||
|
raw: bool = False) -> None:
|
||||||
|
"""print a storage node"""
|
||||||
|
out = []
|
||||||
|
out.append(node.name) # name
|
||||||
|
out.append(node.type) # type
|
||||||
|
out.append('') # fake full path
|
||||||
|
size = node.get_rec_size()
|
||||||
|
out.append(size_to_str(size, raw=raw)) # size
|
||||||
|
out.append(epoch_to_str(node.ts)) # indexed_at
|
||||||
|
out.append('') # fake maccess
|
||||||
|
out.append('') # fake md5
|
||||||
|
out.append(str(len(node.children))) # nbfiles
|
||||||
|
# fake free_space
|
||||||
|
out.append(size_to_str(node.free, raw=raw))
|
||||||
|
# fake total_space
|
||||||
|
out.append(size_to_str(node.total, raw=raw))
|
||||||
|
out.append(node.attr) # meta
|
||||||
|
self._print_entries(out, sep=sep)
|
||||||
|
|
||||||
|
def print_node(self, node: NodeAny,
|
||||||
|
sep: str = DEFSEP,
|
||||||
|
raw: bool = False) -> None:
|
||||||
|
"""print other nodes"""
|
||||||
|
out = []
|
||||||
|
out.append(node.name.replace('"', '""')) # name
|
||||||
|
out.append(node.type) # type
|
||||||
|
parents = node.get_parent_hierarchy()
|
||||||
|
storage = node.get_storage_node()
|
||||||
|
fullpath = os.path.join(storage.name, parents)
|
||||||
|
out.append(fullpath.replace('"', '""')) # full path
|
||||||
|
|
||||||
|
out.append(size_to_str(node.nodesize, raw=raw)) # size
|
||||||
|
out.append(epoch_to_str(storage.ts)) # indexed_at
|
||||||
|
if has_attr(node, 'maccess'):
|
||||||
|
out.append(epoch_to_str(node.maccess)) # maccess
|
||||||
|
else:
|
||||||
|
out.append('') # fake maccess
|
||||||
|
if has_attr(node, 'md5'):
|
||||||
|
out.append(node.md5) # md5
|
||||||
|
else:
|
||||||
|
out.append('') # fake md5
|
||||||
|
if node.type == TYPE_DIR:
|
||||||
|
out.append(str(len(node.children))) # nbfiles
|
||||||
|
else:
|
||||||
|
out.append('') # fake nbfiles
|
||||||
|
out.append('') # fake free_space
|
||||||
|
out.append('') # fake total_space
|
||||||
|
out.append('') # fake meta
|
||||||
|
self._print_entries(out, sep=sep)
|
@ -0,0 +1,151 @@
|
|||||||
|
"""
|
||||||
|
author: deadc0de6 (https://github.com/deadc0de6)
|
||||||
|
Copyright (c) 2022, deadc0de6
|
||||||
|
|
||||||
|
Class for printing nodes in native format
|
||||||
|
"""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
from catcli.nodes import NodeFile, NodeDir, NodeStorage
|
||||||
|
from catcli.colors import Colors
|
||||||
|
from catcli.logger import Logger
|
||||||
|
from catcli.utils import fix_badchars, size_to_str, \
|
||||||
|
has_attr, epoch_to_ls_str
|
||||||
|
|
||||||
|
|
||||||
|
TS_LJUST = 13
|
||||||
|
SIZE_LJUST = 6
|
||||||
|
NAME_LJUST = 20
|
||||||
|
|
||||||
|
class NativePrinter:
|
||||||
|
"""a node printer class"""
|
||||||
|
|
||||||
|
STORAGE = 'storage'
|
||||||
|
ARCHIVE = 'archive'
|
||||||
|
NBFILES = 'nbfiles'
|
||||||
|
|
||||||
|
def print_top(self, pre: str, name: str) -> None:
|
||||||
|
"""print top node"""
|
||||||
|
sys.stdout.write(f'{pre}{name}\n')
|
||||||
|
|
||||||
|
def print_storage(self, pre: str,
|
||||||
|
node: NodeStorage,
|
||||||
|
raw: bool = False) -> None:
|
||||||
|
"""print a storage node"""
|
||||||
|
# construct name
|
||||||
|
name = node.name
|
||||||
|
name = fix_badchars(name)
|
||||||
|
# construct attrs
|
||||||
|
attrs = []
|
||||||
|
# nb files
|
||||||
|
attrs.append(f'nbfiles:{len(node.children)}')
|
||||||
|
# the children size
|
||||||
|
recsize = node.get_rec_size()
|
||||||
|
sizestr = size_to_str(recsize, raw=raw)
|
||||||
|
attrs.append(f'totsize:{sizestr}')
|
||||||
|
# free
|
||||||
|
pcent = 0.0
|
||||||
|
if node.total > 0:
|
||||||
|
pcent = node.free * 100 / node.total
|
||||||
|
attrs.append(f'free:{pcent:.1f}%')
|
||||||
|
# du
|
||||||
|
sztotal = size_to_str(node.total, raw=raw)
|
||||||
|
szused = size_to_str(node.total - node.free, raw=raw)
|
||||||
|
attrs.append(f'du:{szused}/{sztotal}')
|
||||||
|
# timestamp
|
||||||
|
if has_attr(node, 'ts'):
|
||||||
|
attrs.append(f'date:{epoch_to_ls_str(node.ts)}')
|
||||||
|
|
||||||
|
# print
|
||||||
|
out = f'{pre}{Colors.UND}{self.STORAGE}{Colors.RESET}: '
|
||||||
|
out += f'{Colors.PURPLE}{name}{Colors.RESET}'
|
||||||
|
if attrs:
|
||||||
|
out += f'\n{" "*len(name)}{Colors.GRAY}{"|".join(attrs)}{Colors.RESET}'
|
||||||
|
sys.stdout.write(f'{out}\n')
|
||||||
|
|
||||||
|
def print_file(self, pre: str,
|
||||||
|
node: NodeFile,
|
||||||
|
withpath: bool = False,
|
||||||
|
withstorage: bool = False,
|
||||||
|
raw: bool = False) -> None:
|
||||||
|
"""print a file node"""
|
||||||
|
# construct name
|
||||||
|
name = node.name
|
||||||
|
storage = node.get_storage_node()
|
||||||
|
if withpath:
|
||||||
|
name = os.sep.join([
|
||||||
|
storage.name,
|
||||||
|
node.parent.get_parent_hierarchy(),
|
||||||
|
name])
|
||||||
|
name = fix_badchars(name)
|
||||||
|
# construct attributes
|
||||||
|
attrs = []
|
||||||
|
if node.md5:
|
||||||
|
attrs.append(f'md5:{node.md5}')
|
||||||
|
if withstorage:
|
||||||
|
content = Logger.get_bold_text(storage.name)
|
||||||
|
attrs.append(f', storage:{content}')
|
||||||
|
# print
|
||||||
|
out = []
|
||||||
|
out .append(f'{pre}')
|
||||||
|
line = name.ljust(NAME_LJUST, ' ')
|
||||||
|
out.append(f'{line}')
|
||||||
|
size = 0
|
||||||
|
if node.nodesize:
|
||||||
|
size = node.nodesize
|
||||||
|
line = size_to_str(size, raw=raw).ljust(SIZE_LJUST, ' ')
|
||||||
|
out.append(f'{Colors.BLUE}{line}{Colors.RESET}')
|
||||||
|
line = epoch_to_ls_str(node.maccess).ljust(TS_LJUST, ' ')
|
||||||
|
out.append(f'{Colors.PURPLE}{line}{Colors.RESET}')
|
||||||
|
if attrs:
|
||||||
|
out.append(f'{Colors.GRAY}[{",".join(attrs)}]{Colors.RESET}')
|
||||||
|
sys.stdout.write(f'{" ".join(out)}\n')
|
||||||
|
|
||||||
|
def print_dir(self, pre: str,
|
||||||
|
node: NodeDir,
|
||||||
|
withpath: bool = False,
|
||||||
|
withstorage: bool = False,
|
||||||
|
withnbchildren: bool = False,
|
||||||
|
raw: bool = False) -> None:
|
||||||
|
"""print a directory node"""
|
||||||
|
# construct name
|
||||||
|
name = node.name
|
||||||
|
storage = node.get_storage_node()
|
||||||
|
if withpath:
|
||||||
|
name = os.sep.join([
|
||||||
|
storage.name,
|
||||||
|
node.parent.get_parent_hierarchy(),
|
||||||
|
name])
|
||||||
|
name = fix_badchars(name)
|
||||||
|
# construct attrs
|
||||||
|
attrs = []
|
||||||
|
if withnbchildren:
|
||||||
|
nbchildren = len(node.children)
|
||||||
|
attrs.append(f'{self.NBFILES}:{nbchildren}')
|
||||||
|
if withstorage:
|
||||||
|
attrs.append(f'storage:{Logger.get_bold_text(storage.name)}')
|
||||||
|
# print
|
||||||
|
out = []
|
||||||
|
out.append(f'{pre}')
|
||||||
|
line = name.ljust(NAME_LJUST, ' ')
|
||||||
|
out.append(f'{Colors.BLUE}{line}{Colors.RESET}')
|
||||||
|
size = 0
|
||||||
|
if node.nodesize:
|
||||||
|
size = node.nodesize
|
||||||
|
line = size_to_str(size, raw=raw).ljust(SIZE_LJUST, ' ')
|
||||||
|
out.append(f'{Colors.GRAY}{line}{Colors.RESET}')
|
||||||
|
line = epoch_to_ls_str(node.maccess).ljust(TS_LJUST, ' ')
|
||||||
|
out.append(f'{Colors.GRAY}{line}{Colors.RESET}')
|
||||||
|
if attrs:
|
||||||
|
out.append(f'{Colors.GRAY}[{",".join(attrs)}]{Colors.RESET}')
|
||||||
|
sys.stdout.write(f'{" ".join(out)}\n')
|
||||||
|
|
||||||
|
def print_archive(self, pre: str,
|
||||||
|
name: str, archive: str) -> None:
|
||||||
|
"""print an archive"""
|
||||||
|
name = fix_badchars(name)
|
||||||
|
out = f'{pre}{Colors.YELLOW}{name}{Colors.RESET} '
|
||||||
|
out += f'{Colors.GRAY}[{self.ARCHIVE}:{archive}]{Colors.RESET}'
|
||||||
|
sys.stdout.write(f'{out}\n')
|
Loading…
Reference in New Issue