From caebd209e05d828a55d7e64b1ab9aadb1bcb3890 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Sat, 22 Oct 2022 11:16:26 +0200 Subject: [PATCH] refactoring --- catcli/catcli.py | 7 +++---- catcli/logger.py | 32 +++++++++++++++----------------- catcli/noder.py | 20 +++++++++++++------- catcli/utils.py | 17 ++++++++++++----- tests/helpers.py | 4 ++-- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/catcli/catcli.py b/catcli/catcli.py index e3077a3..e6a3e7b 100755 --- a/catcli/catcli.py +++ b/catcli/catcli.py @@ -34,7 +34,6 @@ BANNER = f""" +-+-+-+-+-+-+ |c|a|t|c|l|i| +-+-+-+-+-+-+ v{VERSION}""" -# TODO add grep format for output USAGE = f""" {BANNER} @@ -190,9 +189,9 @@ def cmd_find(args, noder, top): raw = args['--raw-size'] script = args['--script'] search_for = args[''] - noder.find_name(top, search_for, script=script, - startpath=startpath, directory=directory, - parentfromtree=fromtree, fmt=fmt, raw=raw) + return noder.find_name(top, search_for, script=script, + startpath=startpath, directory=directory, + parentfromtree=fromtree, fmt=fmt, raw=raw) def cmd_tree(args, noder, top): diff --git a/catcli/logger.py b/catcli/logger.py index e33b0fc..59d1ae5 100644 --- a/catcli/logger.py +++ b/catcli/logger.py @@ -7,6 +7,9 @@ Logging helper import sys +# local imports +from catcli.utils import fix_badchars + class Logger: """log to stdout/stderr""" @@ -42,11 +45,6 @@ class Logger: Logger.BOLD = '' 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 ###################################################################### @@ -57,7 +55,7 @@ class Logger: if attr: end = f' {Logger.GRAY}({attr}){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' out += f' {Logger.GRAY}{args}{Logger.RESET}' sys.stdout.write(f'{out}\n') @@ -65,7 +63,7 @@ class Logger: @classmethod def file(cls, pre, name, attr): """print a file node""" - nobad = Logger.fix_badchars(name) + nobad = fix_badchars(name) out = f'{pre}{nobad}' out += f' {Logger.GRAY}[{attr}]{Logger.RESET}' sys.stdout.write(f'{out}\n') @@ -81,14 +79,14 @@ class Logger: if end: endstring = ', '.join(end) 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}' sys.stdout.write(f'{out}\n') @classmethod def arc(cls, pre, name, archive): """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}' sys.stdout.write(f'{out}\n') @@ -98,52 +96,52 @@ class Logger: @classmethod def out(cls, string): """to stdout no color""" - string = Logger.fix_badchars(string) + string = fix_badchars(string) sys.stdout.write(f'{string}\n') @classmethod def out_err(cls, string): """to stderr no color""" - string = Logger.fix_badchars(string) + string = fix_badchars(string) sys.stderr.write(f'{string}\n') @classmethod def debug(cls, string): """to stderr no color""" - string = Logger.fix_badchars(string) + string = fix_badchars(string) sys.stderr.write(f'[DBG] {string}\n') @classmethod def info(cls, string): """to stdout in color""" - string = Logger.fix_badchars(string) + string = fix_badchars(string) out = f'{Logger.MAGENTA}{string}{Logger.RESET}' sys.stdout.write(f'{out}\n') @classmethod def err(cls, string): """to stderr in RED""" - string = Logger.fix_badchars(string) + string = fix_badchars(string) out = f'{Logger.RED}{string}{Logger.RESET}' sys.stderr.write(f'{out}\n') @classmethod def progr(cls, string): """print progress""" - string = Logger.fix_badchars(string) + string = fix_badchars(string) sys.stderr.write(f'{string}\r') sys.stderr.flush() @classmethod def bold(cls, string): """make it bold""" - string = Logger.fix_badchars(string) + string = fix_badchars(string) return f'{Logger.BOLD}{string}{Logger.RESET}' @classmethod def flog(cls, path, string, append=True): """log and fix bad chars""" - string = Logger.fix_badchars(string) + string = fix_badchars(string) mode = 'w' if append: mode = 'a' diff --git a/catcli/noder.py b/catcli/noder.py index 3a7004f..86da245 100644 --- a/catcli/noder.py +++ b/catcli/noder.py @@ -12,10 +12,11 @@ import anytree from pyfzf.pyfzf import FzfPrompt # 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.decomp import Decomp from catcli.version import __version__ as VERSION +from catcli.exceptions import CatcliException class Noder: @@ -115,7 +116,7 @@ class Noder: # test hash if self.hash and node.md5: md5 = self._get_hash(path) - if md5 != node.md5: + if md5 and md5 != node.md5: msg = f'\tchange: checksum changed for \"{path}\"' self._debug(msg) return node, True @@ -523,6 +524,7 @@ class Noder: @parentfromtree: get path from parent instead of stored relpath @fmt: output format @raw: raw size output + returns the found nodes """ self._debug(f'searching for \"{key}\"') @@ -576,6 +578,8 @@ class Noder: cmd = f'op=file; source=/media/mnt; $op {tmpstr}' Logger.info(cmd) + return list(paths.values()) + def _callback_find_name(self, term, directory): """callback for finding files""" def find_name(node): @@ -734,14 +738,16 @@ class Noder: def _get_hash(self, path): """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): """sanitize node string""" - node.name = node.name.encode('utf-8', - errors='ignore').decode('utf-8') - node.relpath = node.relpath.encode('utf-8', - errors='ignore').decode('utf-8') + node.name = fix_badchars(node.name) + node.relpath = fix_badchars(node.relpath) return node def _debug(self, string): diff --git a/catcli/utils.py b/catcli/utils.py index 08e6f8f..6bc197d 100644 --- a/catcli/utils.py +++ b/catcli/utils.py @@ -12,15 +12,17 @@ import subprocess import datetime # local imports -from catcli.logger import Logger +from catcli.exceptions import CatcliException def md5sum(path): - """calculate md5 sum of a file""" + """ + calculate md5 sum of a file + may raise exception + """ rpath = os.path.realpath(path) if not os.path.exists(rpath): - Logger.err(f'\nmd5sum - file does not exist: {rpath}') - return None + raise CatcliException(f'md5sum - file does not exist: {rpath}') try: with open(rpath, mode='rb') as file: hashv = hashlib.md5() @@ -33,7 +35,7 @@ def md5sum(path): except PermissionError: pass except OSError as exc: - Logger.err(f'md5sum error: {exc}') + raise CatcliException(f'md5sum error: {exc}') from exc return None @@ -77,3 +79,8 @@ def edit(string): file.seek(0) new = file.read() 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') diff --git a/tests/helpers.py b/tests/helpers.py index 22242b0..a6da349 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -123,7 +123,7 @@ def create_rnd_file(path, filename, content=None): def write_to_file(path, content): """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) return path @@ -132,7 +132,7 @@ def read_from_file(path): """read file content""" if not os.path.exists(path): return '' - with open(path, 'r', encoding='UTF-8') as file: + with open(path, 'r', encoding='utf-8') as file: content = file.read() return content