diff --git a/ui/fixtures/cmd.auto.json b/ui/fixtures/cmd.auto.json new file mode 100644 index 0000000..b34a966 --- /dev/null +++ b/ui/fixtures/cmd.auto.json @@ -0,0 +1,31 @@ +[ +{ + "title": "gen ident", + "cmd": [ "genident", "name=A Test User" ], + "exit": 0 +}, +{ + "title": "Test auto detection of file", + "cmd": [ "auto", "test.txt" ], + "stdin": "y\n", + "stdout": "Set:\n\tmimetype:\ttext/plain; charset=us-ascii\n\nApply (y/N)?\n" +}, +{ + "title": "Verify modification", + "cmd": [ "list", "test.txt" ], + "stdin": "y\n", + "stdout_re": ".*mimetype:\ttext/plain; charset=us-ascii.*" +}, +{ + "title": "Test auto detection of jpeg file", + "cmd": [ "auto", "z.jpg" ], + "stdin": "y\n", + "stdout": "Set:\n\tmimetype:\timage/jpeg\n\nApply (y/N)?\n" +}, +{ + "title": "Verify modification", + "cmd": [ "list", "z.jpg" ], + "stdin": "y\n", + "stdout_re": ".*mimetype:\timage/jpeg" +} +] diff --git a/ui/fixtures/testfiles/z.jpg b/ui/fixtures/testfiles/z.jpg new file mode 100644 index 0000000..0a64329 Binary files /dev/null and b/ui/fixtures/testfiles/z.jpg differ diff --git a/ui/medashare/cli.py b/ui/medashare/cli.py index 0210b7b..0abe77d 100644 --- a/ui/medashare/cli.py +++ b/ui/medashare/cli.py @@ -20,6 +20,7 @@ import hashlib import io import itertools import json +import magic import os.path import pathlib import pasn1 @@ -670,7 +671,7 @@ def enumeratedir(_dir, created_by_ref): Returned is a list of FileObjects.''' return [FileObject.from_file(os.path.join(_dir, x), - created_by_ref) for x in os.listdir(_dir) if not + created_by_ref) for x in sorted(os.listdir(_dir)) if not os.path.isdir(os.path.join(_dir, x)) ] def get_objstore(options): @@ -820,6 +821,26 @@ def cmd_dump(options): for i in objstr: print(i.encode('json')) +def cmd_auto(options): + for i in options.files: + mf = magic.detect_from_filename(i) + + primary = mf[0].split('/', 1)[0] + mt = mf[0] + if primary == 'text': + mt += '; charset=%s' % mf[1] + + print('Set:') + print('\tmimetype:\t%s' % mt) + print() + print('Apply (y/N)?') + + inp = sys.stdin.readline() + + if inp.strip().lower() in ('y', 'yes'): + options.modtagvalues = [ '+mimetype=%s' % mt ] + cmd_modify(options) + def cmd_list(options): persona, objstr = get_objstore(options) @@ -837,7 +858,6 @@ def cmd_list(options): except (FileNotFoundError, KeyError) as e: print('ERROR: file not found: %s' % repr(i), file=sys.stderr) sys.exit(1) - except FileNotFoundError: # XXX - tell the difference? print('ERROR: file not found: %s' % repr(i), @@ -912,6 +932,11 @@ def main(): help='files to modify') parser_mod.set_defaults(func=cmd_modify) + parser_auto = subparsers.add_parser('auto', help='automatic detection of file properties') + parser_auto.add_argument('files', nargs='+', + help='files to modify') + parser_auto.set_defaults(func=cmd_auto) + parser_list = subparsers.add_parser('list', help='list tags on file(s)') parser_list.add_argument('files', nargs='+', help='files to modify') @@ -1141,7 +1166,7 @@ class _TestCases(unittest.TestCase): def test_enumeratedir(self): files = enumeratedir(self.tempdir, self.created_by_ref) - ftest = files[1] + ftest = [ x for x in files if x.filename == 'test.txt' ][0] fname = 'test.txt' # make sure that they are of type MDBase @@ -1459,6 +1484,7 @@ class _TestCases(unittest.TestCase): self.assertRegex(stdout.getvalue(), outre) else: self.assertEqual(stdout.getvalue(), cmd.get('stdout', '')) + self.assertEqual(stderr.getvalue(), cmd.get('stderr', '')) self.assertEqual(cm.exception.code, cmd.get('exit', 0)) @@ -1468,9 +1494,13 @@ class _TestCases(unittest.TestCase): i.stop() def test_cmds(self): - cmds = self.fixtures.glob('cmd.*.json') + cmds = sorted(self.fixtures.glob('cmd.*.json')) for i in cmds: + # make sure each file starts with a clean slate + self.tearDown() + self.setUp() + os.chdir(self.tempdir) self.run_command_file(i) diff --git a/ui/setup.py b/ui/setup.py index dd5590f..4d069ae 100644 --- a/ui/setup.py +++ b/ui/setup.py @@ -28,6 +28,7 @@ setup( 'hypercorn', # option, for server only? 'orm', 'pasn1 @ git+https://www.funkthat.com/gitea/jmg/pasn1.git@c6c64510b42292557ace2b77272eb32cb647399d#egg=pasn1', + 'file-magic @ git+https://github.com/file/file.git#egg=file-magic&subdirectory=python', 'pydantic[dotenv]', ], extras_require = {