# Copyright (c) 2017 Civic Knowledge. This file is licensed under the terms of the
# Revised BSD License, included in this distribution as LICENSE
"""
Metapack CLI program for exditing metapack metadata in place
"""
from metapack.cli.core import MetapackCliMemo as _MetapackCliMemo
from metapack.cli.core import err, warn, write_doc
from metapack.package import Downloader
downloader = Downloader.get_instance()
[docs]def edit_args(subparsers):
parser = subparsers.add_parser(
'edit',
help='Edit a metatab file'
)
cmds = parser.add_subparsers(help='Commands')
parser.set_defaults(run_command=edit_cmd)
parser.add_argument('-e', '--echo', help="Print the file after editing", action='store_true', required=False)
cp_add = cmds.add_parser('add', help='Add a term')
cp_add.set_defaults(edit_command=add_cmd)
cp_add.add_argument('term', nargs=1,
help="A fully qualified term. Prefix with a third component to set the section "
"( 'Resources.Root.Datafile' ) ")
cp_add.add_argument('value', nargs=1, help="The term value, for add and edit ")
cp_add.add_argument('-a', '--arg', help="Add an additional argument: 'Name=value' ",
action='append', required=False)
cp_add.add_argument('metatabfile', nargs='?',
help="Path or URL to a metatab file. If not provided, defaults to 'metadata.csv' ")
cp_edit = cmds.add_parser('change', help='Edit a term')
cp_edit.set_defaults(edit_command=change_cmd)
cp_edit.add_argument('term', nargs=1,
help="A fully qualified term. Prefix with a third component to set the section"
" ( 'Resources.Root.Datafile' ) ")
cp_edit.add_argument('value', nargs=1, help="The term value, for add and edit ")
cp_edit.add_argument('metatabfile', nargs='?',
help="Path or URL to a metatab file. If not provided, defaults to 'metadata.csv' ")
cp_delete = cmds.add_parser('delete', help='Delete a term')
cp_delete.set_defaults(edit_command=delete_cmd)
cp_delete.add_argument('term', nargs=1,
help="A fully qualified term. Prefix with a third component to set the section "
"( 'Resources.Root.Datafile' ) ")
cp_delete.add_argument('metatabfile', nargs='?',
help="Path or URL to a metatab file. If not provided, defaults to 'metadata.csv' ")
[docs]def edit_cmd(args):
m = MetapackCliMemo(args, downloader)
m.args.edit_command(m)
[docs]def add_cmd(m):
doc = m.doc
args = dict(a.split('=', 1) for a in m.args.arg)
doc[m.section].new_term(m.term, m.value, **args)
if m.args.echo:
print(doc.as_csv())
write_doc(doc)
[docs]def change_cmd(m):
doc = m.doc
t = doc.find_first(m.term, section=m.section)
t.value = m.value
if m.args.echo:
print(doc.as_csv())
write_doc(doc)
[docs]def delete_cmd(m):
doc = m.doc
terms = doc.find(m.term, section=m.section)
if not terms:
warn("No terms found for: '{}' in section '{}' ".format(m.term, m.section))
for term in terms:
doc.remove_term(term)
terms = doc.find(m.term, section=m.section)
if terms:
warn("Delete failed")
if m.args.echo:
print(doc.as_csv())
write_doc(doc)