You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
catcli/catcli/noder.py

849 lines
30 KiB
Python

7 years ago
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2017, deadc0de6
1 year ago
Class that process nodes in the catalog tree
7 years ago
"""
import os
import shutil
7 years ago
import time
from typing import List, Union, Tuple, Any, Optional, Dict, cast
1 year ago
import anytree # type: ignore
7 years ago
# local imports
1 year ago
from catcli import nodes
from catcli.nodes import NodeAny, NodeStorage, \
10 months ago
NodeTop, NodeFile, NodeArchived, NodeDir, NodeMeta, \
typcast_node
2 years ago
from catcli.utils import size_to_str, epoch_to_str, md5sum, fix_badchars
7 years ago
from catcli.logger import Logger
2 years ago
from catcli.nodeprinter import NodePrinter
from catcli.decomp import Decomp
2 years ago
from catcli.version import __version__ as VERSION
2 years ago
from catcli.exceptions import CatcliException
7 years ago
2 years ago
class Noder:
"""
handles node in the catalog tree
There are 4 types of node:
7 years ago
* "top" node representing the top node (generic node)
* "storage" node representing a storage
* "dir" node representing a directory
* "file" node representing a file
2 years ago
"""
7 years ago
CSV_HEADER = ('name,type,path,size,indexed_at,'
'maccess,md5,nbfiles,free_space,'
'total_space,meta')
7 years ago
1 year ago
def __init__(self, debug: bool = False,
sortsize: bool = False,
arc: bool = False) -> None:
2 years ago
"""
4 years ago
@debug: debug mode
@sortsize: sort nodes by size
@arch: handle archive
2 years ago
"""
7 years ago
self.hash = True
self.debug = debug
self.sortsize = sortsize
self.arc = arc
if self.arc:
self.decomp = Decomp()
7 years ago
2 years ago
@staticmethod
def get_storage_names(top: NodeTop) -> List[str]:
2 years ago
"""return a list of all storage names"""
7 years ago
return [x.name for x in list(top.children)]
def get_storage_node(self, top: NodeTop,
name: str,
newpath: str = '') -> NodeStorage:
2 years ago
"""
3 years ago
return the storage node if any
1 year ago
if newpath is submitted, it will update the media info
2 years ago
"""
3 years ago
found = None
2 years ago
for node in top.children:
1 year ago
if node.type != nodes.TYPE_STORAGE:
continue
2 years ago
if node.name == name:
found = node
3 years ago
break
if found and newpath and os.path.exists(newpath):
found.free = shutil.disk_usage(newpath).free
found.total = shutil.disk_usage(newpath).total
3 years ago
found.ts = int(time.time())
return cast(NodeStorage, found)
2 years ago
@staticmethod
def get_node(top: NodeTop,
path: str,
quiet: bool = False) -> Optional[NodeAny]:
2 years ago
"""get the node by internal tree path"""
resolv = anytree.resolver.Resolver('name')
try:
2 years ago
bpath = os.path.basename(path)
the_node = resolv.get(top, bpath)
10 months ago
typcast_node(the_node)
return cast(NodeAny, the_node)
except anytree.resolver.ChildResolverError:
if not quiet:
2 years ago
Logger.err(f'No node at path \"{bpath}\"')
return None
1 year ago
def get_node_if_changed(self,
top: NodeTop,
1 year ago
path: str,
treepath: str) -> Tuple[Optional[NodeAny], bool]:
2 years ago
"""
return the node (if any) and if it has changed
@top: top node (storage)
@path: abs path to file
@treepath: rel path from indexed directory
2 years ago
"""
treepath = treepath.lstrip(os.sep)
node = self.get_node(top, treepath, quiet=True)
# node does not exist
if not node:
self._debug('\tchange: node does not exist')
return None, True
if os.path.isdir(path):
return node, False
# force re-indexing if no maccess
maccess = os.path.getmtime(path)
if not self._has_attr(node, 'maccess') or \
not node.maccess:
self._debug('\tchange: no maccess found')
return node, True
# maccess changed
old_maccess = node.maccess
if float(maccess) != float(old_maccess):
self._debug(f'\tchange: maccess changed for \"{path}\"')
return node, True
# test hash
if self.hash and node.md5:
md5 = self._get_hash(path)
2 years ago
if md5 and md5 != node.md5:
2 years ago
msg = f'\tchange: checksum changed for \"{path}\"'
self._debug(msg)
return node, True
self._debug(f'\tchange: no change for \"{path}\"')
return node, False
def rec_size(self, node: Union[NodeDir, NodeStorage],
store: bool = True) -> int:
2 years ago
"""
recursively traverse tree and return size
@store: store the size in the node
2 years ago
"""
1 year ago
if node.type == nodes.TYPE_FILE:
node.__class__ = NodeFile
msg = f'size of {node.type} \"{node.name}\": {node.nodesize}'
self._debug(msg)
return node.nodesize
2 years ago
msg = f'getting node size recursively for \"{node.name}\"'
self._debug(msg)
fullsize: int = 0
6 years ago
for i in node.children:
1 year ago
if node.type == nodes.TYPE_DIR:
sub_size = self.rec_size(i, store=store)
if store:
i.nodesize = sub_size
fullsize += sub_size
continue
1 year ago
if node.type == nodes.TYPE_STORAGE:
sub_size = self.rec_size(i, store=store)
if store:
i.nodesize = sub_size
fullsize += sub_size
6 years ago
continue
self._debug(f'skipping {node.name}')
if store:
node.nodesize = fullsize
self._debug(f'size of {node.type} \"{node.name}\": {fullsize}')
return fullsize
6 years ago
###############################################################
# public helpers
###############################################################
2 years ago
@staticmethod
def attrs_to_string(attr: Union[List[str], Dict[str, str], str]) -> str:
2 years ago
"""format the storage attr for saving"""
6 years ago
if not attr:
return ''
2 years ago
if isinstance(attr, list):
3 years ago
return ', '.join(attr)
if isinstance(attr, dict):
ret = []
for key, val in attr.items():
ret.append(f'{key}={val}')
return ', '.join(ret)
3 years ago
attr = attr.rstrip()
return attr
6 years ago
def do_hashing(self, val: bool) -> None:
2 years ago
"""hash files when indexing"""
6 years ago
self.hash = val
7 years ago
###############################################################
2 years ago
# node creation
7 years ago
###############################################################
def new_top_node(self) -> NodeTop:
2 years ago
"""create a new top node"""
1 year ago
top = NodeTop(nodes.NAME_TOP)
self._debug(f'new top node: {top}')
return top
7 years ago
1 year ago
def new_file_node(self, name: str, path: str,
5 months ago
parent: NodeAny) -> Optional[NodeFile]:
2 years ago
"""create a new node representing a file"""
if not os.path.exists(path):
Logger.err(f'File \"{path}\" does not exist')
return None
7 years ago
path = os.path.abspath(path)
try:
2 years ago
stat = os.lstat(path)
except OSError as exc:
Logger.err(f'OSError: {exc}')
return None
1 year ago
md5 = ''
7 years ago
if self.hash:
md5 = self._get_hash(path)
maccess = os.path.getmtime(path)
node = NodeFile(name,
stat.st_size,
md5,
maccess,
parent=parent)
if self.arc:
ext = os.path.splitext(path)[1][1:]
if ext.lower() in self.decomp.get_formats():
self._debug(f'{path} is an archive')
names = self.decomp.get_names(path)
2 years ago
self.list_to_tree(node, names)
else:
self._debug(f'{path} is NOT an archive')
2 years ago
return node
7 years ago
1 year ago
def new_dir_node(self, name: str, path: str,
5 months ago
parent: NodeAny) -> NodeDir:
2 years ago
"""create a new node representing a directory"""
7 years ago
path = os.path.abspath(path)
maccess = os.path.getmtime(path)
return NodeDir(name,
0,
maccess,
parent=parent)
2 years ago
1 year ago
def new_storage_node(self, name: str,
path: str,
parent: str,
attrs: Dict[str, Any]) \
-> NodeStorage:
2 years ago
"""create a new node representing a storage"""
path = os.path.abspath(path)
free = shutil.disk_usage(path).free
total = shutil.disk_usage(path).total
epoch = int(time.time())
return NodeStorage(name,
free,
total,
0,
epoch,
1 year ago
self.attrs_to_string(attrs),
parent=parent)
2 years ago
def new_archive_node(self,
name: str,
parent: str,
archive: str) -> NodeArchived:
2 years ago
"""create a new node for archive data"""
return NodeArchived(name=name,
parent=parent, nodesize=0, md5='',
archive=archive)
2 years ago
###############################################################
# node management
###############################################################
def update_metanode(self, top: NodeTop) -> NodeMeta:
2 years ago
"""create or update meta node information"""
meta = self._get_meta_node(top)
epoch = int(time.time())
if not meta:
attrs: Dict[str, Any] = {}
attrs['created'] = epoch
attrs['created_version'] = VERSION
1 year ago
meta = NodeMeta(name=nodes.NAME_META,
attr=attrs)
meta.attr['access'] = epoch
meta.attr['access_version'] = VERSION
2 years ago
return meta
def _get_meta_node(self, top: NodeTop) -> Optional[NodeMeta]:
2 years ago
"""return the meta node if any"""
try:
1 year ago
found = next(filter(lambda x: x.type == nodes.TYPE_META,
top.children))
return cast(NodeMeta, found)
2 years ago
except StopIteration:
return None
7 years ago
def clean_not_flagged(self, top: NodeTop) -> int:
2 years ago
"""remove any node not flagged and clean flags"""
cnt = 0
for node in anytree.PreOrderIter(top):
10 months ago
typcast_node(node)
1 year ago
if node.type not in [nodes.TYPE_DIR, nodes.TYPE_FILE]:
continue
if self._clean(node):
cnt += 1
return cnt
def _clean(self, node: NodeAny) -> bool:
2 years ago
"""remove node if not flagged"""
if not node.flagged():
node.parent = None
return True
node.unflag()
return False
7 years ago
###############################################################
# printing
###############################################################
def _node_to_csv(self, node: NodeAny,
1 year ago
sep: str = ',',
raw: bool = False) -> None:
2 years ago
"""
print a node to csv
@node: the node to consider
@sep: CSV separator character
@raw: print raw size rather than human readable
2 years ago
"""
if not node:
2 years ago
return
1 year ago
if node.type == nodes.TYPE_TOP:
2 years ago
return
out = []
1 year ago
if node.type == nodes.TYPE_STORAGE:
# handle storage
out.append(node.name) # name
out.append(node.type) # type
out.append('') # fake full path
2 years ago
size = self.rec_size(node, store=False)
2 years ago
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
2 years ago
out.append(size_to_str(node.free, raw=raw))
# fake total_space
2 years ago
out.append(size_to_str(node.total, raw=raw))
out.append(node.attr) # meta
else:
# handle other nodes
out.append(node.name.replace('"', '""')) # name
out.append(node.type) # type
parents = self._get_parents(node)
storage = self._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
2 years ago
out.append(epoch_to_str(storage.ts)) # indexed_at
if self._has_attr(node, 'maccess'):
2 years ago
out.append(epoch_to_str(node.maccess)) # maccess
else:
2 years ago
out.append('') # fake maccess
1 year ago
if self._has_attr(node, 'md5'):
out.append(node.md5) # md5
else:
out.append('') # fake md5
1 year ago
if node.type == nodes.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
line = sep.join(['"' + o + '"' for o in out])
if len(line) > 0:
2 years ago
Logger.stdout_nocolor(line)
def node_has_subs(self, node: Any) -> bool:
"""
node may have children
we explicitely handle all case
for clarity
"""
if not node:
return False
if node.type == nodes.TYPE_TOP:
return True
if node.type == nodes.TYPE_FILE:
return False
if node.type == nodes.TYPE_DIR:
return True
if node.type == nodes.TYPE_STORAGE:
return True
if node.type == nodes.TYPE_ARCHIVED:
return True
return False
def _print_node_native(self, node: NodeAny,
1 year ago
pre: str = '',
withpath: bool = False,
withnbchildren: bool = False,
1 year ago
withstorage: bool = False,
raw: bool = False) -> None:
2 years ago
"""
print a node
@node: the node to print
@pre: string to print before node
@withpath: print the node path
@withnbchildren: print the node nb children
@withstorage: print the node storage it belongs to
@raw: print raw size rather than human readable
2 years ago
"""
1 year ago
if node.type == nodes.TYPE_TOP:
# top node
node.__class__ = NodeTop
2 years ago
Logger.stdout_nocolor(f'{pre}{node.name}')
1 year ago
elif node.type == nodes.TYPE_FILE:
# node of type file
node.__class__ = NodeFile
7 years ago
name = node.name
storage = self._get_storage(node)
7 years ago
if withpath:
name = os.sep.join([
storage.name,
self._get_parents(node.parent),
name])
name = name.lstrip(os.sep)
1 year ago
attr_str = ''
7 years ago
if node.md5:
1 year ago
attr_str = f', md5:{node.md5}'
size = size_to_str(node.nodesize, raw=raw)
1 year ago
compl = f'size:{size}{attr_str}'
if withstorage:
2 years ago
content = Logger.get_bold_text(storage.name)
compl += f', storage:{content}'
2 years ago
NodePrinter.print_file_native(pre, name, compl)
1 year ago
elif node.type == nodes.TYPE_DIR:
# node of type directory
node.__class__ = NodeDir
7 years ago
name = node.name
storage = self._get_storage(node)
7 years ago
if withpath:
name = os.sep.join([
storage.name,
self._get_parents(node.parent),
name])
name = name.lstrip(os.sep)
nbchildren = 0
if withnbchildren:
nbchildren = len(node.children)
1 year ago
attr: List[Tuple[str, str]] = []
if node.nodesize:
attr.append(('totsize', size_to_str(node.nodesize, raw=raw)))
if withstorage:
1 year ago
attr.append(('storage', Logger.get_bold_text(storage.name)))
NodePrinter.print_dir_native(pre,
name,
nbchildren=nbchildren,
attr=attr)
1 year ago
elif node.type == nodes.TYPE_STORAGE:
# node of type storage
node.__class__ = NodeStorage
2 years ago
sztotal = size_to_str(node.total, raw=raw)
szused = size_to_str(node.total - node.free, raw=raw)
nbchildren = len(node.children)
1 year ago
pcent = 0
if node.total > 0:
pcent = node.free * 100 / node.total
freepercent = f'{pcent:.1f}%'
# get the date
2 years ago
timestamp = ''
if self._has_attr(node, 'ts'):
2 years ago
timestamp = 'date:'
timestamp += epoch_to_str(node.ts)
disksize = ''
# the children size
1 year ago
recsize = self.rec_size(node, store=False)
sizestr = size_to_str(recsize, raw=raw)
disksize = 'totsize:' + f'{sizestr}'
# format the output
name = node.name
args = [
'nbfiles:' + f'{nbchildren}',
2 years ago
disksize,
f'free:{freepercent}',
'du:' + f'{szused}/{sztotal}',
2 years ago
timestamp]
argsstring = ' | '.join(args)
2 years ago
NodePrinter.print_storage_native(pre,
name,
argsstring,
node.attr)
1 year ago
elif node.type == nodes.TYPE_ARCHIVED:
# archive node
node.__class__ = NodeArchived
if self.arc:
2 years ago
NodePrinter.print_archive_native(pre, node.name, node.archive)
7 years ago
else:
Logger.err(f'bad node encountered: {node}')
7 years ago
def print_tree(self, node: NodeAny,
1 year ago
fmt: str = 'native',
raw: bool = False) -> None:
2 years ago
"""
print the tree in different format
@node: start node
@style: when fmt=native, defines the tree style
@fmt: output format
@raw: print the raw size rather than human readable
2 years ago
"""
if fmt == 'native':
# "tree" style
rend = anytree.RenderTree(node, childiter=self._sort_tree)
2 years ago
for pre, _, thenode in rend:
2 years ago
self._print_node_native(thenode, pre=pre,
withnbchildren=True, raw=raw)
elif fmt == 'csv':
# csv output
2 years ago
self._to_csv(node, raw=raw)
elif fmt == 'csv-with-header':
# csv output
2 years ago
Logger.stdout_nocolor(self.CSV_HEADER)
2 years ago
self._to_csv(node, raw=raw)
def _to_csv(self, node: NodeAny,
1 year ago
raw: bool = False) -> None:
2 years ago
"""print the tree to csv"""
rend = anytree.RenderTree(node, childiter=self._sort_tree)
2 years ago
for _, _, item in rend:
self._node_to_csv(item, raw=raw)
2 years ago
@staticmethod
1 year ago
def _fzf_prompt(strings: Any) -> Any:
"""prompt with fzf"""
try:
from pyfzf.pyfzf import FzfPrompt # type: ignore # pylint: disable=C0415 # noqa
fzf = FzfPrompt()
selected = fzf.prompt(strings)
return selected
except ModuleNotFoundError:
Logger.err('install pyfzf to use fzf')
return None
def _to_fzf(self, node: NodeAny, fmt: str) -> None:
"""
2 years ago
fzf prompt with list and print selected node(s)
@node: node to start with
@fmt: output format for selected nodes
"""
2 years ago
rendered = anytree.RenderTree(node, childiter=self._sort_tree)
1 year ago
the_nodes = {}
# construct node names list
2 years ago
for _, _, rend in rendered:
if not rend:
continue
2 years ago
parents = self._get_parents(rend)
storage = self._get_storage(rend)
fullpath = os.path.join(storage.name, parents)
1 year ago
the_nodes[fullpath] = rend
# prompt with fzf
1 year ago
paths = self._fzf_prompt(the_nodes.keys())
# print the resulting tree
subfmt = fmt.replace('fzf-', '')
for path in paths:
if not path:
continue
1 year ago
if path not in the_nodes:
continue
1 year ago
rend = the_nodes[path]
2 years ago
self.print_tree(rend, fmt=subfmt)
2 years ago
@staticmethod
def to_dot(top: NodeTop,
1 year ago
path: str = 'tree.dot') -> str:
2 years ago
"""export to dot for graphing"""
anytree.exporter.DotExporter(top).to_dotfile(path)
Logger.info(f'dot file created under \"{path}\"')
return f'dot {path} -T png -o /tmp/tree.png'
6 years ago
7 years ago
###############################################################
# searching
###############################################################
def find_name(self, top: NodeTop,
1 year ago
key: str,
script: bool = False,
only_dir: bool = False,
startnode: Optional[NodeAny] = None,
1 year ago
fmt: str = 'native',
raw: bool = False) -> List[NodeAny]:
2 years ago
"""
find files based on their names
@top: top node
@key: term to search for
@script: output script
@directory: only search for directories
@startpath: node to start with
@fmt: output format
@raw: raw size output
2 years ago
returns the found nodes
2 years ago
"""
self._debug(f'searching for \"{key}\"')
2 years ago
# search for nodes based on path
start: Optional[NodeAny] = top
1 year ago
if startnode:
start = self.get_node(top, startnode)
2 years ago
filterfunc = self._callback_find_name(key, only_dir)
2 years ago
found = anytree.findall(start, filter_=filterfunc)
1 year ago
self._debug(f'found {len(found)} node(s)')
# compile found nodes
2 years ago
paths = {}
for item in found:
item.name = fix_badchars(item.name)
1 year ago
storage = self._get_storage(item)
parents = self._get_parents(item)
key = f'{storage}/{parents}/{item.name}'
paths[parents] = item
2 years ago
# handle fzf mode
if fmt.startswith('fzf'):
selected = self._fzf_prompt(paths.keys())
2 years ago
newpaths = {}
subfmt = fmt.replace('fzf-', '')
2 years ago
for item in selected:
if item not in paths:
continue
2 years ago
newpaths[item] = paths[item]
2 years ago
self.print_tree(newpaths[item], fmt=subfmt)
paths = newpaths
2 years ago
else:
if fmt == 'native':
for _, item in paths.items():
2 years ago
self._print_node_native(item, withpath=True,
withnbchildren=True,
2 years ago
withstorage=True,
raw=raw)
2 years ago
elif fmt.startswith('csv'):
if fmt == 'csv-with-header':
2 years ago
Logger.stdout_nocolor(self.CSV_HEADER)
2 years ago
for _, item in paths.items():
self._node_to_csv(item, raw=raw)
2 years ago
# execute script if any
7 years ago
if script:
2 years ago
tmp = ['${source}/' + x for x in paths]
tmpstr = ' '.join(tmp)
cmd = f'op=file; source=/media/mnt; $op {tmpstr}'
7 years ago
Logger.info(cmd)
2 years ago
return list(paths.values())
1 year ago
def _callback_find_name(self, term: str, only_dir: bool) -> Any:
2 years ago
"""callback for finding files"""
def find_name(node: NodeAny) -> bool:
1 year ago
if node.type == nodes.TYPE_STORAGE:
2 years ago
# ignore storage nodes
return False
1 year ago
if node.type == nodes.TYPE_TOP:
2 years ago
# ignore top nodes
return False
1 year ago
if node.type == nodes.TYPE_META:
2 years ago
# ignore meta nodes
return False
1 year ago
if only_dir and node.type == nodes.TYPE_DIR:
2 years ago
# ignore non directory
return False
# filter
if not term:
return True
if term.lower() in node.name.lower():
return True
2 years ago
# ignore
return False
return find_name
7 years ago
###############################################################
2 years ago
# ls
7 years ago
###############################################################
def list(self, top: NodeTop,
1 year ago
path: str,
rec: bool = False,
fmt: str = 'native',
raw: bool = False) -> List[NodeAny]:
2 years ago
"""
2 years ago
list nodes for "ls"
@top: top node
@path: path to search for
@rec: recursive walk
@fmt: output format
@raw: print raw size
2 years ago
"""
self._debug(f'walking path: \"{path}\" from {top}')
2 years ago
resolv = anytree.resolver.Resolver('name')
7 years ago
found = []
try:
if '*' in path or '?' in path:
# we need to handle glob
found = resolv.glob(top, path)
else:
# we have a canonical path
found = resolv.get(top, path)
if found and self.node_has_subs(found):
# let's find its children as well
modpath = os.path.join(path, '*')
found = resolv.glob(top, modpath)
else:
found = [found]
7 years ago
if len(found) < 1:
# nothing found
self._debug('nothing found')
7 years ago
return []
7 years ago
if rec:
# print the entire tree
2 years ago
self.print_tree(found[0].parent, fmt=fmt, raw=raw)
return found
# sort found nodes
found = sorted(found, key=self._sort, reverse=self.sortsize)
# print the parent
if fmt == 'native':
2 years ago
self._print_node_native(found[0].parent,
withpath=False,
withnbchildren=True,
2 years ago
raw=raw)
elif fmt.startswith('csv'):
self._node_to_csv(found[0].parent, raw=raw)
2 years ago
elif fmt.startswith('fzf'):
pass
# print all found nodes
if fmt == 'csv-with-header':
2 years ago
Logger.stdout_nocolor(self.CSV_HEADER)
2 years ago
for item in found:
if fmt == 'native':
2 years ago
self._print_node_native(item, withpath=False,
pre='- ',
withnbchildren=True,
2 years ago
raw=raw)
elif fmt.startswith('csv'):
2 years ago
self._node_to_csv(item, raw=raw)
elif fmt.startswith('fzf'):
2 years ago
self._to_fzf(item, fmt)
7 years ago
except anytree.resolver.ChildResolverError:
pass
return found
###############################################################
4 years ago
# tree creation
###############################################################
1 year ago
def _add_entry(self, name: str,
top: NodeTop,
1 year ago
resolv: Any) -> None:
2 years ago
"""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)
return
sub = os.sep.join(entries[:-1])
2 years ago
nodename = entries[-1]
try:
parent = resolv.get(top, sub)
parent = self.new_archive_node(nodename, parent, top.name)
except anytree.resolver.ChildResolverError:
self.new_archive_node(nodename, top, top.name)
def list_to_tree(self, parent: NodeAny, names: List[str]) -> None:
2 years ago
"""convert list of files to a tree"""
if not names:
return
2 years ago
resolv = anytree.resolver.Resolver('name')
for name in names:
name = name.rstrip(os.sep)
2 years ago
self._add_entry(name, parent, resolv)
7 years ago
###############################################################
# diverse
###############################################################
1 year ago
def _sort_tree(self,
items: List[NodeAny]) -> List[NodeAny]:
2 years ago
"""sorting a list of items"""
return sorted(items, key=self._sort, reverse=self.sortsize)
7 years ago
def _sort(self, lst: NodeAny) -> Any:
2 years ago
"""sort a list"""
if self.sortsize:
return self._sort_size(lst)
return self._sort_fs(lst)
2 years ago
@staticmethod
def _sort_fs(node: NodeAny) -> Tuple[str, str]:
2 years ago
"""sorting nodes dir first and alpha"""
return (node.type, node.name.lstrip('.').lower())
7 years ago
2 years ago
@staticmethod
def _sort_size(node: NodeAny) -> float:
2 years ago
"""sorting nodes by size"""
try:
if not node.nodesize:
return 0
return float(node.nodesize)
except AttributeError:
return 0
def _get_storage(self, node: NodeAny) -> NodeStorage:
2 years ago
"""recursively traverse up to find storage"""
1 year ago
if node.type == nodes.TYPE_STORAGE:
return node
return cast(NodeStorage, node.ancestors[1])
2 years ago
@staticmethod
def _has_attr(node: NodeAny, attr: str) -> bool:
2 years ago
"""return True if node has attr as attribute"""
return attr in node.__dict__.keys()
def _get_parents(self, node: NodeAny) -> str:
2 years ago
"""get all parents recursively"""
1 year ago
if node.type == nodes.TYPE_STORAGE:
return ''
1 year ago
if node.type == nodes.TYPE_TOP:
return ''
parent = self._get_parents(node.parent)
if parent:
return os.sep.join([parent, node.name])
1 year ago
return str(node.name)
2 years ago
@staticmethod
1 year ago
def _get_hash(path: str) -> str:
"""return md5 hash of node"""
2 years ago
try:
return md5sum(path)
except CatcliException as exc:
Logger.err(str(exc))
1 year ago
return ''
1 year ago
def _debug(self, string: str) -> None:
2 years ago
"""print debug"""
if not self.debug:
return
4 years ago
Logger.debug(string)