You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.9 KiB
96 lines
2.9 KiB
import os
|
|
import datetime
|
|
|
|
from .. import repo
|
|
from .. import endecoder
|
|
from .. import bibstruct
|
|
from .. import color
|
|
from ..paper import Paper
|
|
from ..configs import config
|
|
from ..uis import get_ui
|
|
from ..content import system_path, read_file
|
|
|
|
|
|
def parser(subparsers):
|
|
parser = subparsers.add_parser('import',
|
|
help='import paper(s) to the repository')
|
|
parser.add_argument('bibpath',
|
|
help='path to bibtex, bibtexml or bibyaml file (or directory)')
|
|
parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True,
|
|
help="don't copy document files, just create a link.")
|
|
parser.add_argument('keys', nargs='*',
|
|
help="one or several keys to import from the file")
|
|
return parser
|
|
|
|
|
|
def many_from_path(bibpath):
|
|
"""Extract list of papers found in bibliographic files in path.
|
|
|
|
The behavior is to:
|
|
- ignore wrong entries,
|
|
- overwrite duplicated entries.
|
|
:returns: dictionary of (key, paper | exception)
|
|
if loading of entry failed, the excpetion is returned in the
|
|
dictionary in place of the paper
|
|
"""
|
|
coder = endecoder.EnDecoder()
|
|
|
|
bibpath = system_path(bibpath)
|
|
if os.path.isdir(bibpath):
|
|
print([os.path.splitext(f)[-1][1:] for f in os.listdir(bibpath)])
|
|
all_files = [os.path.join(bibpath, f) for f in os.listdir(bibpath)
|
|
if os.path.splitext(f)[-1][1:] == 'bib']
|
|
else:
|
|
all_files = [bibpath]
|
|
|
|
biblist = []
|
|
for filepath in all_files:
|
|
biblist.append(coder.decode_bibdata(read_file(filepath)))
|
|
|
|
papers = {}
|
|
for b in biblist:
|
|
for k in b.keys():
|
|
try:
|
|
bibdata = {}
|
|
bibdata[k] = b[k]
|
|
|
|
papers[k] = Paper(bibdata, citekey=k)
|
|
papers[k].added = datetime.datetime.now()
|
|
except ValueError as e:
|
|
papers[k] = e
|
|
return papers
|
|
|
|
|
|
def command(args):
|
|
"""
|
|
:param bibpath: path (no url yet) to a bibliography file
|
|
"""
|
|
|
|
ui = get_ui()
|
|
bibpath = args.bibpath
|
|
copy = args.copy
|
|
|
|
if copy is None:
|
|
copy = config().import_copy
|
|
rp = repo.Repository(config())
|
|
# Extract papers from bib
|
|
papers = many_from_path(bibpath)
|
|
keys = args.keys or papers.keys()
|
|
for k in keys:
|
|
try:
|
|
p = papers[k]
|
|
if isinstance(p, Exception):
|
|
ui.error('could not load entry for citekey {}.'.format(k))
|
|
else:
|
|
rp.push_paper(p)
|
|
ui.print_('{} imported'.format(color.dye(p.citekey, color.cyan)))
|
|
docfile = bibstruct.extract_docfile(p.bibdata)
|
|
if docfile is None:
|
|
ui.warning("no file for {}.".format(p.citekey))
|
|
else:
|
|
rp.push_doc(p.citekey, docfile, copy=args.copy)
|
|
except KeyError:
|
|
ui.error('no entry found for citekey {}.'.format(k))
|
|
except IOError as e:
|
|
ui.error(e.message)
|