- Moves shared command code to helpers module. - Implements reference(s) argument for commands: + a helper to add single or multi-reference argument to parser, + two functions to transform this argument into a list of citekeys.main
parent
281d945f08
commit
105ae292b9
@ -0,0 +1,48 @@
|
|||||||
|
from .. import files
|
||||||
|
from .. import color
|
||||||
|
from ..repo import InvalidReference
|
||||||
|
|
||||||
|
|
||||||
|
def add_references_argument(parser, single=False):
|
||||||
|
if single:
|
||||||
|
parser.add_argument('reference',
|
||||||
|
help='reference to the paper (citekey or number)')
|
||||||
|
else:
|
||||||
|
parser.add_argument('references', nargs='*',
|
||||||
|
help="one or several reference to export (citekeysor numbers)")
|
||||||
|
|
||||||
|
|
||||||
|
def add_paper_with_docfile(repo, paper, docfile=None, copy=False):
|
||||||
|
repo.add_paper(paper)
|
||||||
|
if docfile is not None:
|
||||||
|
if copy:
|
||||||
|
repo.import_document(paper.citekey, docfile)
|
||||||
|
else:
|
||||||
|
paper.set_external_document(docfile)
|
||||||
|
repo.add_or_update(paper)
|
||||||
|
|
||||||
|
|
||||||
|
def extract_doc_path_from_bibdata(paper, ui):
|
||||||
|
try:
|
||||||
|
file_path = paper.get_document_file_from_bibdata(remove=True)
|
||||||
|
if files.check_file(file_path):
|
||||||
|
return file_path
|
||||||
|
else:
|
||||||
|
ui.warning("File does not exist for %s (%s)."
|
||||||
|
% (paper.citekey, file_path))
|
||||||
|
except NoDocumentFile:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def parse_reference(ui, rp, ref):
|
||||||
|
try:
|
||||||
|
return rp.citekey_from_ref(ref)
|
||||||
|
except InvalidReference:
|
||||||
|
ui.error("no paper with reference: %s."
|
||||||
|
% color.dye(ref, color.citekey))
|
||||||
|
ui.exit(-1)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_references(ui, rp, refs):
|
||||||
|
citekeys = [parse_reference(ui, rp, ref) for ref in refs]
|
||||||
|
return citekeys
|
@ -1,20 +1,21 @@
|
|||||||
from .. import repo
|
from .. import repo
|
||||||
from .. import color
|
from .. import color
|
||||||
|
from .helpers import add_references_argument, parse_references
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
parser = subparsers.add_parser('remove', help='removes a paper')
|
parser = subparsers.add_parser('remove', help='removes a paper')
|
||||||
parser.add_argument('reference',
|
add_references_argument(parser)
|
||||||
help='reference to the paper (citekey or number)')
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def command(config, ui, reference):
|
def command(config, ui, references):
|
||||||
rp = repo.Repository.from_directory(config)
|
rp = repo.Repository.from_directory(config)
|
||||||
key = rp.citekey_from_ref(reference, fatal=True)
|
citekeys = parse_references(ui, rp, references)
|
||||||
paper = rp.paper_from_citekey(key)
|
are_you_sure = ("Are you sure you want to delete paper(s) [%s]"
|
||||||
are_you_sure = ("Are you sure you want to delete paper [%s]"
|
|
||||||
" (this will also delete associated documents)?"
|
" (this will also delete associated documents)?"
|
||||||
% color.dye(paper.citekey, color.citekey))
|
% ', '.join([color.dye(c, color.citekey) for c in citekeys]))
|
||||||
sure = ui.input_yn(question=are_you_sure, default='n')
|
sure = ui.input_yn(question=are_you_sure, default='n')
|
||||||
if sure:
|
if sure:
|
||||||
rp.remove(paper.citekey)
|
for c in citekeys:
|
||||||
|
rp.remove(c)
|
||||||
|
Loading…
Reference in new issue