Integrates devadd as add.

main
Olivier Mangin 12 years ago
parent 74118924b8
commit 245ab0ea4d

@ -9,5 +9,4 @@ import edit_cmd
import remove_cmd
import websearch_cmd
import devadd_cmd
import devlist_cmd

@ -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

@ -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

@ -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

@ -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()

@ -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()

@ -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))

Loading…
Cancel
Save