From cd9159ddf307e4a23db9b804a034917357dbf74f Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Fri, 26 Aug 2022 15:08:45 -0700 Subject: [PATCH] add auto command to populate some standard fields, e.g. mimetype --- ui/fixtures/cmd.auto.json | 31 +++++++++++++++++++++++++++++ ui/fixtures/testfiles/z.jpg | Bin 0 -> 332 bytes ui/medashare/cli.py | 38 ++++++++++++++++++++++++++++++++---- ui/setup.py | 1 + 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 ui/fixtures/cmd.auto.json create mode 100644 ui/fixtures/testfiles/z.jpg 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 0000000000000000000000000000000000000000..0a64329646789b69f1205f9d722d3850b07ec17b GIT binary patch literal 332 zcmex=^(PF6}rMnOeST|r4lSw=>~TvNxu(8R<)Zf(-wU zFvtT9X9aSAfB^~^nV4Bv+1NQaxwwG}whAyXF)}kVu`si;vakSE*8=4kSOi&x6b&8O zgaZ@Vl?p|S8YeE~Pwh=DOELf4NWZ*Q!{f5ODks=S2uSL zPp{yR(6I1`$f)F$)U@=B%&g*)(z5c3%Btp;*0%PJ&aO$5r%atTea6gLixw|gx@`H1 zm8&*w-m-Pu_8mKS9XfpE=&|D`PM*4S`O4L6*Kgds_3+W-Cr_U}fAR9w$4{TXeEs(Q U$IoB?Z!vIy{A17Xnd|>e0JL6d;Q#;t literal 0 HcmV?d00001 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 = {