Commit aa384e83 authored by Ophélie Gagnard's avatar Ophélie Gagnard Committed by Julien Muchembled

upload: Support metadata in json file

parent 5c59ccb1
...@@ -502,8 +502,10 @@ def cmd_upload(*args): ...@@ -502,8 +502,10 @@ def cmd_upload(*args):
help="Upload the contents of this file, overriding --url") help="Upload the contents of this file, overriding --url")
parser.add_argument('--id', parser.add_argument('--id',
help="Identifier used for the shadir URL. Overriding --prefix-key, --suffix-key and --url") help="Identifier used for the shadir URL. Overriding --prefix-key, --suffix-key and --url")
parser.add_argument('--metadata',
help="Take a file containing a json-serializable dictionary with shadir metadata.")
parser.add_argument('meta', nargs='*', metavar='KEY=VALUE', parser.add_argument('meta', nargs='*', metavar='KEY=VALUE',
help="Extra metadata.") help="Extra metadata. Warning: interpreted as string.")
args = parser.parse_args(args or sys.argv[1:]) args = parser.parse_args(args or sys.argv[1:])
nc = NetworkcacheClient(args.config) nc = NetworkcacheClient(args.config)
f = None f = None
...@@ -517,15 +519,25 @@ def cmd_upload(*args): ...@@ -517,15 +519,25 @@ def cmd_upload(*args):
f = (nc.archive if os.path.isdir(args.url) else urlopen)(args.url) f = (nc.archive if os.path.isdir(args.url) else urlopen)(args.url)
else: else:
parser.error('either --file or --url is required') parser.error('either --file or --url is required')
kw = dict(x.split('=', 1) for x in args.meta) if args.metadata:
with open(args.metadata) as g:
try:
metadata_dict = json.loads(g.read())
if type(metadata_dict) != dict:
raise NetworkcacheException("Not a json-serializable dictionary: %s" % args.metadata)
except json.decoder.JSONDecodeError:
raise NetworkcacheException("Invalid json in %s" % args.metadata)
else:
metadata_dict = dict()
metadata_dict.update(dict(x.split('=', 1) for x in args.meta))
if args.id: if args.id:
kw.setdefault('id', args.id) metadata_dict.setdefault('id', args.id)
key = args.id key = args.id
else: else:
kw.setdefault('url', args.url) metadata_dict.setdefault('url', args.url)
urlmd5 = hashlib.md5(args.url.encode()).hexdigest() urlmd5 = hashlib.md5(args.url.encode()).hexdigest()
key = args.prefix_key + urlmd5 + args.suffix_key key = args.prefix_key + urlmd5 + args.suffix_key
nc.upload(f, key, **kw) nc.upload(f, key, **metadata_dict)
finally: finally:
f is None or f.close() f is None or f.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment