Uniformizes add and import commands. Still to be further tested.
This commit is contained in:
parent
a45051815d
commit
f8e370f288
@ -1,17 +1,56 @@
|
||||
from .. import repo
|
||||
from .. import files
|
||||
from ..paper import Paper, NoDocumentFile
|
||||
|
||||
|
||||
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 parser(subparsers, config):
|
||||
parser = subparsers.add_parser('add', help='add a paper to the repository')
|
||||
parser.add_argument('pdffile', help='pdf or ps file')
|
||||
parser.add_argument('bibfile', help='bibtex, bibtexml or bibyaml file')
|
||||
parser.add_argument('-d', '--docfile', help='pdf or ps file', default=None)
|
||||
parser.add_argument('-c', '--copy', action='store_true', default=None,
|
||||
help="copy document files into library directory (default)")
|
||||
parser.add_argument('-C', '--nocopy', action='store_false', dest='copy',
|
||||
help="don't copy document files (opposite of -c)")
|
||||
return parser
|
||||
|
||||
|
||||
def command(config, ui, pdffile, bibfile):
|
||||
def command(config, ui, bibfile, docfile, copy):
|
||||
"""
|
||||
:param pdffilepath path (no url yet) to a pdf or ps file
|
||||
:param bibtex bibtex file (in .bib, .bibml or .yaml format.
|
||||
:param bibfile: bibtex file (in .bib, .bibml or .yaml format.
|
||||
:param docfile: path (no url yet) to a pdf or ps file
|
||||
"""
|
||||
if copy is None:
|
||||
copy = config.get('papers', 'import-copy')
|
||||
rp = repo.Repository.from_directory()
|
||||
rp.add_paper_from_paths(pdffile, bibfile)
|
||||
p = Paper.load(bibfile)
|
||||
# Check if another doc file is specified in bibtex
|
||||
docfile2 = extract_doc_path_from_bibdata(p, ui)
|
||||
if docfile is None:
|
||||
docfile = docfile2
|
||||
elif docfile2 is not None:
|
||||
ui.warning(
|
||||
"Skipping document file from bib file: %s, using %s instead."
|
||||
% (docfile2, docfile))
|
||||
add_paper_with_docfile(rp, p, docfile=docfile, copy=copy)
|
||||
|
@ -1,6 +1,6 @@
|
||||
from .. import repo
|
||||
from ..paper import Paper, NoDocumentFile
|
||||
from .. import files
|
||||
from ..paper import Paper
|
||||
from add_cmd import add_paper_with_docfile, extract_doc_path_from_bibdata
|
||||
|
||||
|
||||
def parser(subparsers, config):
|
||||
@ -25,20 +25,7 @@ def command(config, ui, bibpath, copy):
|
||||
# Extract papers from bib
|
||||
papers = Paper.many_from_path(bibpath, fatal=False)
|
||||
for p in papers:
|
||||
doc_file = None
|
||||
try:
|
||||
file_path = p.get_document_file_from_bibdata(remove=True)
|
||||
if files.check_file(file_path):
|
||||
doc_file = file_path
|
||||
else:
|
||||
print("File does not exist for %s (%s)."
|
||||
% (p.citekey, file_path))
|
||||
except NoDocumentFile:
|
||||
print "No file for %s." % p.citekey
|
||||
rp.add_paper(p)
|
||||
if doc_file:
|
||||
if copy:
|
||||
rp.import_document(p.citekey, doc_file)
|
||||
else:
|
||||
p.set_external_document(doc_file)
|
||||
rp.add_or_update(p)
|
||||
doc_file = extract_doc_path_from_bibdata(p, ui)
|
||||
if doc_file is None:
|
||||
ui.warning("No file for %s." % p.citekey)
|
||||
add_paper_with_docfile(rp, p, docfile=doc_file, copy=copy)
|
||||
|
@ -77,7 +77,9 @@ def get_safe_metadata_from_content(content):
|
||||
|
||||
|
||||
def get_safe_metadata_from_path(metapath):
|
||||
if metapath is not None:
|
||||
if metapath is None:
|
||||
content = None
|
||||
else:
|
||||
content = files.read_yamlfile(metapath)
|
||||
return get_safe_metadata(content)
|
||||
|
||||
|
@ -3,7 +3,7 @@ import shutil
|
||||
import glob
|
||||
|
||||
import files
|
||||
from paper import Paper, PaperInRepo, NoDocumentFile
|
||||
from paper import PaperInRepo, NoDocumentFile
|
||||
from color import colored
|
||||
import configs
|
||||
|
||||
@ -60,13 +60,6 @@ class Repository(object):
|
||||
|
||||
# creating new papers
|
||||
|
||||
# Deprecated
|
||||
# TODO merge
|
||||
def add_paper_from_paths(self, docpath, bibpath):
|
||||
p = Paper.load(bibpath)
|
||||
p.set_external_document(docpath)
|
||||
self.add_paper(p)
|
||||
|
||||
def add_paper(self, p):
|
||||
if p.citekey is None: # TODO also test if citekey is valid
|
||||
raise(ValueError("Invalid citekey: %s." % p.citekey))
|
||||
|
@ -66,3 +66,6 @@ class UI:
|
||||
|
||||
def error(self, message):
|
||||
self.print_("%s: %s" % (colored('error', 'red'), message))
|
||||
|
||||
def warning(self, message):
|
||||
self.print_("%s: %s" % (colored('warning', 'yellow'), message))
|
||||
|
Loading…
x
Reference in New Issue
Block a user