2
1
mirror of https://github.com/deadc0de6/catcli synced 2024-11-13 07:10:26 +00:00

refactoring

This commit is contained in:
deadc0de6 2022-10-22 11:16:26 +02:00
parent d8a360d3b5
commit caebd209e0
5 changed files with 45 additions and 35 deletions

View File

@ -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['<term>']
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):

View File

@ -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'

View File

@ -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):

View File

@ -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')

View File

@ -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