2 Commits

Author SHA1 Message Date
  John-Mark Gurney 71128c1767 fix two bugs, first is deleting tag that didn't exist was an error 6 months ago
  John-Mark Gurney cf0fddb0ad add enable/disable for debug prints.. 6 months ago
3 changed files with 80 additions and 25 deletions
Split View
  1. +12
    -0
      ui/fixtures/cmd.auto.json
  2. +53
    -25
      ui/medashare/cli.py
  3. +15
    -0
      ui/medashare/utils.py

+ 12
- 0
ui/fixtures/cmd.auto.json View File

@@ -27,5 +27,17 @@
"cmd": [ "list", "z.jpg" ],
"stdin": "y\n",
"stdout_re": "mimetype:\timage/jpeg"
},
{
"title": "Test auto detection of two files",
"cmd": [ "auto", "z.jpg", "test.txt" ],
"stdin": "y\ny\n",
"stdout": "Set:\n\tmimetype:\timage/jpeg\n\nApply (y/N)?\nSet:\n\tmimetype:\ttext/plain; charset=us-ascii\n\nApply (y/N)?\n"
},
{
"title": "Verify modification",
"cmd": [ "list", "z.jpg", "test.txt" ],
"stdin": "y\n",
"stdout_re": "hashes:.*\nmimetype:\timage/jpeg.*\nsig:.*\nhashes:.*\nmimetype:\ttext/plain.*\nsig:.*\n"
}
]

+ 53
- 25
ui/medashare/cli.py View File

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

@@ -1029,7 +1030,10 @@ def cmd_modify(options, persona, objstr, cache):
try:
key, v = k
except ValueError:
del obj[k[0]]
try:
del obj[k[0]]
except KeyError:
pass
else:
obj[key].remove(v)

@@ -1206,6 +1210,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 +1235,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')
@@ -1356,7 +1375,7 @@ def cmd_dump(options, persona, objstr, cache):
print(i.encode('json'))

def cmd_auto(options):
for i in options.files:
for i in options.files[:]:
mf = detect_from_filename(i)

primary = mf[0].split('/', 1)[0]
@@ -1372,7 +1391,8 @@ def cmd_auto(options):
inp = sys.stdin.readline()

if inp.strip().lower() in ('y', 'yes'):
options.modtagvalues = [ '+mimetype=%s' % mt ]
options.modtagvalues = [ '-mimetype', '+mimetype=%s' % mt ]
options.files = [ i ]
cmd_modify(options)

@init_datastructs
@@ -1668,6 +1688,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 +1785,11 @@ def main():

options = parser.parse_args()

if options.debug:
enable_debug()
else:
disable_debug()

try:
fun = options.func
except AttributeError:


+ 15
- 0
ui/medashare/utils.py View File

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



Loading…
Cancel
Save