|
|
@ -1,10 +1,9 @@
|
|
|
|
from .. import repo
|
|
|
|
|
|
|
|
from .. import files
|
|
|
|
|
|
|
|
from ..paper import Paper, NoDocumentFile, get_bibentry_from_string
|
|
|
|
|
|
|
|
from ..configs import config
|
|
|
|
|
|
|
|
from ..uis import get_ui
|
|
|
|
from ..uis import get_ui
|
|
|
|
from .helpers import add_paper_with_docfile, extract_doc_path_from_bibdata
|
|
|
|
from ..configs import config
|
|
|
|
|
|
|
|
from .. import bibstruct
|
|
|
|
|
|
|
|
from .. import content
|
|
|
|
|
|
|
|
from .. import repo
|
|
|
|
|
|
|
|
from .. import paper
|
|
|
|
|
|
|
|
|
|
|
|
def parser(subparsers):
|
|
|
|
def parser(subparsers):
|
|
|
|
parser = subparsers.add_parser('add', help='add a paper to the repository')
|
|
|
|
parser = subparsers.add_parser('add', help='add a paper to the repository')
|
|
|
@ -13,6 +12,8 @@ def parser(subparsers):
|
|
|
|
parser.add_argument('-d', '--docfile', help='pdf or ps file', default=None)
|
|
|
|
parser.add_argument('-d', '--docfile', help='pdf or ps file', default=None)
|
|
|
|
parser.add_argument('-t', '--tags', help='tags associated to the paper, separated by commas',
|
|
|
|
parser.add_argument('-t', '--tags', help='tags associated to the paper, separated by commas',
|
|
|
|
default=None)
|
|
|
|
default=None)
|
|
|
|
|
|
|
|
parser.add_argument('-k', '--citekey', help='citekey associated with the paper;\nif not provided, one will be generated automatically.',
|
|
|
|
|
|
|
|
default=None)
|
|
|
|
parser.add_argument('-c', '--copy', action='store_true', default=None,
|
|
|
|
parser.add_argument('-c', '--copy', action='store_true', default=None,
|
|
|
|
help="copy document files into library directory (default)")
|
|
|
|
help="copy document files into library directory (default)")
|
|
|
|
parser.add_argument('-C', '--nocopy', action='store_false', dest='copy',
|
|
|
|
parser.add_argument('-C', '--nocopy', action='store_false', dest='copy',
|
|
|
@ -30,41 +31,72 @@ def command(args):
|
|
|
|
bibfile = args.bibfile
|
|
|
|
bibfile = args.bibfile
|
|
|
|
docfile = args.docfile
|
|
|
|
docfile = args.docfile
|
|
|
|
tags = args.tags
|
|
|
|
tags = args.tags
|
|
|
|
copy = args.copy
|
|
|
|
citekey = args.copy
|
|
|
|
|
|
|
|
|
|
|
|
if copy is None:
|
|
|
|
|
|
|
|
copy = config().import_copy
|
|
|
|
|
|
|
|
rp = repo.Repository(config())
|
|
|
|
rp = repo.Repository(config())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# get bibfile
|
|
|
|
|
|
|
|
|
|
|
|
if bibfile is None:
|
|
|
|
if bibfile is None:
|
|
|
|
cont = True
|
|
|
|
cont = True
|
|
|
|
bibstr = ''
|
|
|
|
bibstr = ''
|
|
|
|
while cont:
|
|
|
|
while cont:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
bibstr = files.editor_input(config().edit_cmd, bibstr, suffix='.yaml')
|
|
|
|
bibstr = content.editor_input(config().edit_cmd, bibstr, suffix='.yaml')
|
|
|
|
key, bib = get_bibentry_from_string(bibstr)
|
|
|
|
bibdata = rp.databroker.verify(bibstr)
|
|
|
|
|
|
|
|
bibstruct.verify_bibdata(bibdata)
|
|
|
|
|
|
|
|
# REFACTOR Generate citykey
|
|
|
|
cont = False
|
|
|
|
cont = False
|
|
|
|
except Exception:
|
|
|
|
except ValueError:
|
|
|
|
cont = ui.input_yn(
|
|
|
|
cont = ui.input_yn(
|
|
|
|
question='Invalid bibfile. Edit again ?',
|
|
|
|
question='Invalid bibfile. Edit again ?',
|
|
|
|
default='y')
|
|
|
|
default='y')
|
|
|
|
if not cont:
|
|
|
|
if not cont:
|
|
|
|
ui.exit(0)
|
|
|
|
ui.exit(0)
|
|
|
|
p = Paper(bibentry=bib, citekey=key)
|
|
|
|
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
p = Paper.load(bibfile)
|
|
|
|
bibdata_raw = content.get_content(bibfile)
|
|
|
|
|
|
|
|
bibdata = rp.databroker.verify(bibdata_raw)
|
|
|
|
|
|
|
|
if bibdata is None:
|
|
|
|
|
|
|
|
ui.error('invalid bibfile {}.'.format(bibfile))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print bibdata
|
|
|
|
|
|
|
|
# citekey
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
citekey = args.citekey
|
|
|
|
|
|
|
|
if citekey is None:
|
|
|
|
|
|
|
|
base_key = bibstruct.extract_citekey(bibdata)
|
|
|
|
|
|
|
|
citekey = rp.unique_citekey(base_key)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
rp.databroker.exists(citekey, both=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# tags
|
|
|
|
|
|
|
|
|
|
|
|
if tags is not None:
|
|
|
|
if tags is not None:
|
|
|
|
p.tags = set(tags.split(','))
|
|
|
|
p.tags = set(tags.split(','))
|
|
|
|
# Check if another doc file is specified in bibtex
|
|
|
|
|
|
|
|
docfile2 = extract_doc_path_from_bibdata(p)
|
|
|
|
p = paper.Paper(citekey=citekey, bibdata=bibdata)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# document file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bib_docfile = bibstruct.extract_docfile(bibdata)
|
|
|
|
if docfile is None:
|
|
|
|
if docfile is None:
|
|
|
|
docfile = docfile2
|
|
|
|
docfile = bib_docfile
|
|
|
|
elif docfile2 is not None:
|
|
|
|
elif bib_docfile is not None:
|
|
|
|
ui.warning(
|
|
|
|
ui.warning(('Skipping document file from bib file '
|
|
|
|
"Skipping document file from bib file: %s, using %s instead."
|
|
|
|
'{}, using {} instead.').format(bib_docfile, docfile))
|
|
|
|
% (docfile2, docfile))
|
|
|
|
|
|
|
|
|
|
|
|
if docfile is not None:
|
|
|
|
|
|
|
|
copy_doc = args.copy
|
|
|
|
|
|
|
|
if copy_doc is None:
|
|
|
|
|
|
|
|
copy_doc = config().import_copy
|
|
|
|
|
|
|
|
if copy_doc:
|
|
|
|
|
|
|
|
docfile = rp.databroker.copy_doc(citekey, docfile)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# create the paper
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
add_paper_with_docfile(rp, p, docfile=docfile, copy=copy)
|
|
|
|
p.docpath = docfile
|
|
|
|
|
|
|
|
rp.push_paper(p)
|
|
|
|
except ValueError, v:
|
|
|
|
except ValueError, v:
|
|
|
|
ui.error(v.message)
|
|
|
|
ui.error(v.message)
|
|
|
|
ui.exit(1)
|
|
|
|
ui.exit(1)
|
|
|
|
# TODO handle case where citekey exists
|
|
|
|
|
|
|
|