From 245ab0ea4d22426f07ab2aaa396bcf581f3c4b85 Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Mon, 10 Jun 2013 15:04:04 +0200 Subject: [PATCH] Integrates devadd as add. --- papers/commands/__init__.py | 1 - papers/commands/add_cmd.py | 36 ++++++++++++++--- papers/commands/devadd_cmd.py | 76 ----------------------------------- papers/commands/edit_cmd.py | 4 +- papers/files.py | 9 ++++- papers/papers | 1 - papers/ui.py | 5 +++ 7 files changed, 44 insertions(+), 88 deletions(-) delete mode 100644 papers/commands/devadd_cmd.py diff --git a/papers/commands/__init__.py b/papers/commands/__init__.py index 8d013b5..180e7bb 100644 --- a/papers/commands/__init__.py +++ b/papers/commands/__init__.py @@ -9,5 +9,4 @@ import edit_cmd import remove_cmd import websearch_cmd -import devadd_cmd import devlist_cmd diff --git a/papers/commands/add_cmd.py b/papers/commands/add_cmd.py index 0319dc0..4256121 100644 --- a/papers/commands/add_cmd.py +++ b/papers/commands/add_cmd.py @@ -1,6 +1,6 @@ from .. import repo from .. import files -from ..paper import Paper, NoDocumentFile +from ..paper import Paper, NoDocumentFile, get_bibentry_from_string from .. import configs @@ -28,8 +28,11 @@ def extract_doc_path_from_bibdata(paper, ui): def parser(subparsers, config): parser = subparsers.add_parser('add', help='add a paper to the repository') - parser.add_argument('bibfile', help='bibtex, bibtexml or bibyaml file') + parser.add_argument('-b', '--bibfile', + help='bibtex, bibtexml or bibyaml file', default=None) parser.add_argument('-d', '--docfile', help='pdf or ps file', default=None) + parser.add_argument('-l', '--label', help='label associated to the paper', + 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', @@ -37,7 +40,7 @@ def parser(subparsers, config): return parser -def command(config, ui, bibfile, docfile, copy): +def command(config, ui, bibfile, docfile, label, copy): """ :param bibfile: bibtex file (in .bib, .bibml or .yaml format. :param docfile: path (no url yet) to a pdf or ps file @@ -45,7 +48,25 @@ def command(config, ui, bibfile, docfile, copy): if copy is None: copy = config.get(configs.MAIN_SECTION, 'import-copy') rp = repo.Repository.from_directory(config) - p = Paper.load(bibfile) + if bibfile is None: + cont = True + bibstr = '' + while cont: + try: + bibstr = files.editor_input(config, bibstr, suffix='.yaml') + key, bib = get_bibentry_from_string(bibstr) + cont = False + except Exception: + cont = ui.input_yn( + question='Invalid bibfile. Edit again or abort?', + default='y') + if not cont: + ui.exit() + p = Paper(bibentry=bib, citekey=key) + else: + p = Paper.load(bibfile) + if label is not None: + p.metadata['labels'] = label.split() # Check if another doc file is specified in bibtex docfile2 = extract_doc_path_from_bibdata(p, ui) if docfile is None: @@ -54,4 +75,9 @@ def command(config, ui, bibfile, docfile, copy): ui.warning( "Skipping document file from bib file: %s, using %s instead." % (docfile2, docfile)) - add_paper_with_docfile(rp, p, docfile=docfile, copy=copy) + try: + add_paper_with_docfile(rp, p, docfile=docfile, copy=copy) + except ValueError, v: + ui.error(v.message) + ui.exit(1) +# TODO handle case where citekey exists diff --git a/papers/commands/devadd_cmd.py b/papers/commands/devadd_cmd.py deleted file mode 100644 index 39b84df..0000000 --- a/papers/commands/devadd_cmd.py +++ /dev/null @@ -1,76 +0,0 @@ -import os -import subprocess - -from .. import repo -from .. import files -from ..paper import Paper, NoDocumentFile -from .. import configs - -TMP_BIB_FILE = '/tmp/ref.bib' - -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('devadd', help='devadd a paper to the repository') - parser.add_argument('-b', '--bibfile', help='bibtex, bibtexml or bibyaml file', default=None) - parser.add_argument('-d', '--docfile', help='pdf or ps file', default=None) - parser.add_argument('-l', '--label', help='label associated to the paper', 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, bibfile, docfile, label, copy): - """ - :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(configs.MAIN_SECTION, 'import-copy') - rp = repo.Repository.from_directory(config) - if bibfile is None: - bibfile = fill_bib(config) - p = Paper.load(bibfile) - if label is not None: - p.metadata['labels'] = label.split() - # 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) - -def fill_bib(config): - if os.path.exists(TMP_BIB_FILE): - os.remove(TMP_BIB_FILE) - print "Fill the reference as a .bib and close the editor" - proc = subprocess.Popen([config.get(configs.MAIN_SECTION, 'edit-cmd'), TMP_BIB_FILE]) - proc.wait() - return TMP_BIB_FILE - - diff --git a/papers/commands/edit_cmd.py b/papers/commands/edit_cmd.py index 5f942f8..7aec8d5 100644 --- a/papers/commands/edit_cmd.py +++ b/papers/commands/edit_cmd.py @@ -1,7 +1,6 @@ from ..files import editor_input from .. import repo from ..paper import get_bibentry_from_string, get_safe_metadata_from_content -from .. import configs def parser(subparsers, config): @@ -22,12 +21,11 @@ def command(config, ui, reference, meta): if meta: to_edit = 'meta' filepath = rp.path_to_paper_file(key, to_edit) - editor = config.get(configs.MAIN_SECTION, 'edit-cmd') with open(filepath) as f: content = f.read() while True: # Get new content from user - content = editor_input(editor, content) + content = editor_input(config, content) new_key = key bib = None metadata = None diff --git a/papers/files.py b/papers/files.py index 6fa87a1..87a1007 100644 --- a/papers/files.py +++ b/papers/files.py @@ -5,6 +5,7 @@ import tempfile import yaml from .color import colored +from . import configs try: import pybtex @@ -106,6 +107,7 @@ def save_bibdata(bib_data, filepath): def save_meta(meta_data, filepath): write_yamlfile(filepath, meta_data) + # is this function ever used? 08/06/2013 def load_meta(filepath): return read_yamlfile(filepath) @@ -136,9 +138,12 @@ def parse_bibdata(content, format_): return parser.parse_stream(content) -def editor_input(editor, initial=""): +def editor_input(config, initial="", suffix=None): """Use an editor to get input""" - with tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as temp_file: + if suffix is None: + suffix = '.tmp' + editor = config.get(configs.MAIN_SECTION, 'edit-cmd') + with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as temp_file: tfile_name = temp_file.name temp_file.write(initial) temp_file.flush() diff --git a/papers/papers b/papers/papers index d7c08a5..5756f08 100755 --- a/papers/papers +++ b/papers/papers @@ -22,7 +22,6 @@ cmds = collections.OrderedDict([ ('websearch', commands.websearch_cmd) ]) -cmds.update(collections.OrderedDict([('devadd', commands.devadd_cmd)])) cmds.update(collections.OrderedDict([('devlist', commands.devlist_cmd)])) config = configs.read_config() diff --git a/papers/ui.py b/papers/ui.py index cb920bd..17fee8c 100644 --- a/papers/ui.py +++ b/papers/ui.py @@ -1,3 +1,5 @@ +import sys + from .beets_ui import _encoding, input_ from .color import colored @@ -65,6 +67,9 @@ class UI: return (True, False)[self.input_choice(['yes', 'no'], ['y', 'n'], default=d, question=question)] + def exit(self, error_code=1): + sys.exit(error_code) + def error(self, message): self.print_("%s: %s" % (colored('error', 'red'), message))