From cf0fddb0ad23a361665b6efbb482bf74e4526b2f Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 30 May 2024 18:03:56 -0700 Subject: [PATCH] add enable/disable for debug prints.. --- ui/medashare/cli.py | 68 +++++++++++++++++++++++++++++-------------- ui/medashare/utils.py | 15 ++++++++++ 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/ui/medashare/cli.py b/ui/medashare/cli.py index e710fc2..dea2760 100644 --- a/ui/medashare/cli.py +++ b/ui/medashare/cli.py @@ -19,7 +19,7 @@ if False: logging.getLogger('sqlalchemy').addHandler(_handler) logging.getLogger('sqlalchemy.engine').setLevel(lvl) -from .utils import _debprint +from .utils import _debprint, enable_debug, disable_debug def _getquery(q, objstr): return repr(str(q.compile(objstr._engine, @@ -841,6 +841,7 @@ class FileObject(MDBase): #print(repr(self), repr(s), s.st_mtime, file=_real_stderr) if self.mtime.timestamp() != mtimets or \ self.size != s.st_size: + _debprint('times:', self.mtime.timestamp(), mtimets) raise ValueError('file %s has changed' % repr(self.filename)) @@ -1206,6 +1207,18 @@ def checkforfile(objstr, curfile, ask=False): return fobj +from contextlib import contextmanager +import time + +@contextmanager +def timeblock(tag): + s = time.time() + try: + yield + finally: + e = time.time() + _debprint(tag, e - s) + @init_datastructs def cmd_interactive(options, persona, objstr, cache): files = [ pathlib.Path(x) for x in options.files ] @@ -1219,33 +1232,36 @@ def cmd_interactive(options, persona, objstr, cache): files = sorted(pathlib.Path('.').iterdir()) while True: - curfile = files[idx] + with timeblock('checkfile'): + curfile = files[idx] - fobj = checkforfile(objstr, curfile, not autoskip) + fobj = checkforfile(objstr, curfile, not autoskip) + #_debprint(repr(fobj)) - if fobj is None and autoskip and idx > 0 and idx < len(files) - 1: - # if we are auto skipping, and within range, continue - if inp == '1': - idx = max(0, idx - 1) - continue - if inp == '2': - idx = min(len(files) - 1, idx + 1) - continue + if fobj is None and autoskip and idx > 0 and idx < len(files) - 1: + # if we are auto skipping, and within range, continue + if inp == '1': + idx = max(0, idx - 1) + continue + if inp == '2': + idx = min(len(files) - 1, idx + 1) + continue print('Current: %s' % repr(str(curfile))) - if fobj is None: - print('No file object for this file.') - else: - try: - objs = objstr.by_file(curfile) - except KeyError: - print('No tags or metadata object for this file.') + with timeblock('byfile'): + if fobj is None: + print('No file object for this file.') else: - for k, v in _iterdictlist(objs[0]): - if k in { 'sig', 'hashes' }: - continue - print('%s:\t%s' % (k, v)) + try: + objs = objstr.by_file(curfile) + except KeyError: + print('No tags or metadata object for this file.') + else: + for k, v in _iterdictlist(objs[0]): + if k in { 'sig', 'hashes' }: + continue + print('%s:\t%s' % (k, v)) if idx == 0: print('1) No previous file') @@ -1668,6 +1684,9 @@ def main(): parser = argparse.ArgumentParser() + parser.add_argument('--debug', action='store_true', + default=False, help='enable debug output') + parser.add_argument('--db', '-d', type=str, help='base name for storage') @@ -1762,6 +1781,11 @@ def main(): options = parser.parse_args() + if options.debug: + enable_debug() + else: + disable_debug() + try: fun = options.func except AttributeError: diff --git a/ui/medashare/utils.py b/ui/medashare/utils.py index 7832385..e1fca56 100644 --- a/ui/medashare/utils.py +++ b/ui/medashare/utils.py @@ -6,7 +6,22 @@ import uuid _real_stderr = sys.stderr +DEBUG = True + +def enable_debug(): + global DEBUG + + DEBUG = True + +def disable_debug(): + global DEBUG + + DEBUG = False + def _debprint(*args): # pragma: no cover + if not DEBUG: + return + import traceback, sys, os.path st = traceback.extract_stack(limit=2)[0]