refactoring

pull/29/head
deadc0de6 2 years ago
parent d8a360d3b5
commit caebd209e0

@ -34,7 +34,6 @@ BANNER = f""" +-+-+-+-+-+-+
|c|a|t|c|l|i| |c|a|t|c|l|i|
+-+-+-+-+-+-+ v{VERSION}""" +-+-+-+-+-+-+ v{VERSION}"""
# TODO add grep format for output
USAGE = f""" USAGE = f"""
{BANNER} {BANNER}
@ -190,9 +189,9 @@ def cmd_find(args, noder, top):
raw = args['--raw-size'] raw = args['--raw-size']
script = args['--script'] script = args['--script']
search_for = args['<term>'] search_for = args['<term>']
noder.find_name(top, search_for, script=script, return noder.find_name(top, search_for, script=script,
startpath=startpath, directory=directory, startpath=startpath, directory=directory,
parentfromtree=fromtree, fmt=fmt, raw=raw) parentfromtree=fromtree, fmt=fmt, raw=raw)
def cmd_tree(args, noder, top): def cmd_tree(args, noder, top):

@ -7,6 +7,9 @@ Logging helper
import sys import sys
# local imports
from catcli.utils import fix_badchars
class Logger: class Logger:
"""log to stdout/stderr""" """log to stdout/stderr"""
@ -42,11 +45,6 @@ class Logger:
Logger.BOLD = '' Logger.BOLD = ''
Logger.UND = '' Logger.UND = ''
@classmethod
def fix_badchars(cls, line):
"""fix none utf-8 chars in line"""
return line.encode('utf-8', 'ignore').decode('utf-8')
###################################################################### ######################################################################
# node specific output # node specific output
###################################################################### ######################################################################
@ -57,7 +55,7 @@ class Logger:
if attr: if attr:
end = f' {Logger.GRAY}({attr}){Logger.RESET}' end = f' {Logger.GRAY}({attr}){Logger.RESET}'
out = f'{pre}{Logger.UND}{Logger.STORAGE}{Logger.RESET}:' out = f'{pre}{Logger.UND}{Logger.STORAGE}{Logger.RESET}:'
out += ' ' + Logger.PURPLE + Logger.fix_badchars(name) + \ out += ' ' + Logger.PURPLE + fix_badchars(name) + \
Logger.RESET + end + '\n' Logger.RESET + end + '\n'
out += f' {Logger.GRAY}{args}{Logger.RESET}' out += f' {Logger.GRAY}{args}{Logger.RESET}'
sys.stdout.write(f'{out}\n') sys.stdout.write(f'{out}\n')
@ -65,7 +63,7 @@ class Logger:
@classmethod @classmethod
def file(cls, pre, name, attr): def file(cls, pre, name, attr):
"""print a file node""" """print a file node"""
nobad = Logger.fix_badchars(name) nobad = fix_badchars(name)
out = f'{pre}{nobad}' out = f'{pre}{nobad}'
out += f' {Logger.GRAY}[{attr}]{Logger.RESET}' out += f' {Logger.GRAY}[{attr}]{Logger.RESET}'
sys.stdout.write(f'{out}\n') sys.stdout.write(f'{out}\n')
@ -81,14 +79,14 @@ class Logger:
if end: if end:
endstring = ', '.join(end) endstring = ', '.join(end)
end = f' [{endstring}]' end = f' [{endstring}]'
out = pre + Logger.BLUE + Logger.fix_badchars(name) + Logger.RESET out = pre + Logger.BLUE + fix_badchars(name) + Logger.RESET
out += f'{Logger.GRAY}{end}{Logger.RESET}' out += f'{Logger.GRAY}{end}{Logger.RESET}'
sys.stdout.write(f'{out}\n') sys.stdout.write(f'{out}\n')
@classmethod @classmethod
def arc(cls, pre, name, archive): def arc(cls, pre, name, archive):
"""archive to stdout""" """archive to stdout"""
out = pre + Logger.YELLOW + Logger.fix_badchars(name) + Logger.RESET out = pre + Logger.YELLOW + fix_badchars(name) + Logger.RESET
out += f' {Logger.GRAY}[{Logger.ARCHIVE}:{archive}]{Logger.RESET}' out += f' {Logger.GRAY}[{Logger.ARCHIVE}:{archive}]{Logger.RESET}'
sys.stdout.write(f'{out}\n') sys.stdout.write(f'{out}\n')
@ -98,52 +96,52 @@ class Logger:
@classmethod @classmethod
def out(cls, string): def out(cls, string):
"""to stdout no color""" """to stdout no color"""
string = Logger.fix_badchars(string) string = fix_badchars(string)
sys.stdout.write(f'{string}\n') sys.stdout.write(f'{string}\n')
@classmethod @classmethod
def out_err(cls, string): def out_err(cls, string):
"""to stderr no color""" """to stderr no color"""
string = Logger.fix_badchars(string) string = fix_badchars(string)
sys.stderr.write(f'{string}\n') sys.stderr.write(f'{string}\n')
@classmethod @classmethod
def debug(cls, string): def debug(cls, string):
"""to stderr no color""" """to stderr no color"""
string = Logger.fix_badchars(string) string = fix_badchars(string)
sys.stderr.write(f'[DBG] {string}\n') sys.stderr.write(f'[DBG] {string}\n')
@classmethod @classmethod
def info(cls, string): def info(cls, string):
"""to stdout in color""" """to stdout in color"""
string = Logger.fix_badchars(string) string = fix_badchars(string)
out = f'{Logger.MAGENTA}{string}{Logger.RESET}' out = f'{Logger.MAGENTA}{string}{Logger.RESET}'
sys.stdout.write(f'{out}\n') sys.stdout.write(f'{out}\n')
@classmethod @classmethod
def err(cls, string): def err(cls, string):
"""to stderr in RED""" """to stderr in RED"""
string = Logger.fix_badchars(string) string = fix_badchars(string)
out = f'{Logger.RED}{string}{Logger.RESET}' out = f'{Logger.RED}{string}{Logger.RESET}'
sys.stderr.write(f'{out}\n') sys.stderr.write(f'{out}\n')
@classmethod @classmethod
def progr(cls, string): def progr(cls, string):
"""print progress""" """print progress"""
string = Logger.fix_badchars(string) string = fix_badchars(string)
sys.stderr.write(f'{string}\r') sys.stderr.write(f'{string}\r')
sys.stderr.flush() sys.stderr.flush()
@classmethod @classmethod
def bold(cls, string): def bold(cls, string):
"""make it bold""" """make it bold"""
string = Logger.fix_badchars(string) string = fix_badchars(string)
return f'{Logger.BOLD}{string}{Logger.RESET}' return f'{Logger.BOLD}{string}{Logger.RESET}'
@classmethod @classmethod
def flog(cls, path, string, append=True): def flog(cls, path, string, append=True):
"""log and fix bad chars""" """log and fix bad chars"""
string = Logger.fix_badchars(string) string = fix_badchars(string)
mode = 'w' mode = 'w'
if append: if append:
mode = 'a' mode = 'a'

@ -12,10 +12,11 @@ import anytree
from pyfzf.pyfzf import FzfPrompt from pyfzf.pyfzf import FzfPrompt
# local imports # local imports
from catcli.utils import size_to_str, epoch_to_str, md5sum from catcli.utils import size_to_str, epoch_to_str, md5sum, fix_badchars
from catcli.logger import Logger from catcli.logger import Logger
from catcli.decomp import Decomp from catcli.decomp import Decomp
from catcli.version import __version__ as VERSION from catcli.version import __version__ as VERSION
from catcli.exceptions import CatcliException
class Noder: class Noder:
@ -115,7 +116,7 @@ class Noder:
# test hash # test hash
if self.hash and node.md5: if self.hash and node.md5:
md5 = self._get_hash(path) md5 = self._get_hash(path)
if md5 != node.md5: if md5 and md5 != node.md5:
msg = f'\tchange: checksum changed for \"{path}\"' msg = f'\tchange: checksum changed for \"{path}\"'
self._debug(msg) self._debug(msg)
return node, True return node, True
@ -523,6 +524,7 @@ class Noder:
@parentfromtree: get path from parent instead of stored relpath @parentfromtree: get path from parent instead of stored relpath
@fmt: output format @fmt: output format
@raw: raw size output @raw: raw size output
returns the found nodes
""" """
self._debug(f'searching for \"{key}\"') self._debug(f'searching for \"{key}\"')
@ -576,6 +578,8 @@ class Noder:
cmd = f'op=file; source=/media/mnt; $op {tmpstr}' cmd = f'op=file; source=/media/mnt; $op {tmpstr}'
Logger.info(cmd) Logger.info(cmd)
return list(paths.values())
def _callback_find_name(self, term, directory): def _callback_find_name(self, term, directory):
"""callback for finding files""" """callback for finding files"""
def find_name(node): def find_name(node):
@ -734,14 +738,16 @@ class Noder:
def _get_hash(self, path): def _get_hash(self, path):
"""return md5 hash of node""" """return md5 hash of node"""
return md5sum(path) try:
return md5sum(path)
except CatcliException as exc:
Logger.err(str(exc))
return None
def _sanitize(self, node): def _sanitize(self, node):
"""sanitize node string""" """sanitize node string"""
node.name = node.name.encode('utf-8', node.name = fix_badchars(node.name)
errors='ignore').decode('utf-8') node.relpath = fix_badchars(node.relpath)
node.relpath = node.relpath.encode('utf-8',
errors='ignore').decode('utf-8')
return node return node
def _debug(self, string): def _debug(self, string):

@ -12,15 +12,17 @@ import subprocess
import datetime import datetime
# local imports # local imports
from catcli.logger import Logger from catcli.exceptions import CatcliException
def md5sum(path): def md5sum(path):
"""calculate md5 sum of a file""" """
calculate md5 sum of a file
may raise exception
"""
rpath = os.path.realpath(path) rpath = os.path.realpath(path)
if not os.path.exists(rpath): if not os.path.exists(rpath):
Logger.err(f'\nmd5sum - file does not exist: {rpath}') raise CatcliException(f'md5sum - file does not exist: {rpath}')
return None
try: try:
with open(rpath, mode='rb') as file: with open(rpath, mode='rb') as file:
hashv = hashlib.md5() hashv = hashlib.md5()
@ -33,7 +35,7 @@ def md5sum(path):
except PermissionError: except PermissionError:
pass pass
except OSError as exc: except OSError as exc:
Logger.err(f'md5sum error: {exc}') raise CatcliException(f'md5sum error: {exc}') from exc
return None return None
@ -77,3 +79,8 @@ def edit(string):
file.seek(0) file.seek(0)
new = file.read() new = file.read()
return new.decode('utf-8') return new.decode('utf-8')
def fix_badchars(string):
"""fix none utf-8 chars in string"""
return string.encode('utf-8', 'ignore').decode('utf-8')

@ -123,7 +123,7 @@ def create_rnd_file(path, filename, content=None):
def write_to_file(path, content): def write_to_file(path, content):
"""write content to file""" """write content to file"""
with open(path, 'w', encoding='UTF-8') as file: with open(path, 'w', encoding='utf-8') as file:
file.write(content) file.write(content)
return path return path
@ -132,7 +132,7 @@ def read_from_file(path):
"""read file content""" """read file content"""
if not os.path.exists(path): if not os.path.exists(path):
return '' return ''
with open(path, 'r', encoding='UTF-8') as file: with open(path, 'r', encoding='utf-8') as file:
content = file.read() content = file.read()
return content return content

Loading…
Cancel
Save