Support for many references and refactor.
- Moves shared command code to helpers module. - Implements reference(s) argument for commands: + a helper to add single or multi-reference argument to parser, + two functions to transform this argument into a list of citekeys.
This commit is contained in:
parent
281d945f08
commit
105ae292b9
@ -2,28 +2,7 @@ from .. import repo
|
|||||||
from .. import files
|
from .. import files
|
||||||
from ..paper import Paper, NoDocumentFile, get_bibentry_from_string
|
from ..paper import Paper, NoDocumentFile, get_bibentry_from_string
|
||||||
from .. import configs
|
from .. import configs
|
||||||
|
from .helpers import add_paper_with_docfile, extract_doc_path_from_bibdata
|
||||||
|
|
||||||
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):
|
def parser(subparsers, config):
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
from ..files import editor_input
|
from ..files import editor_input
|
||||||
from .. import repo
|
from .. import repo
|
||||||
from ..paper import get_bibentry_from_string, get_safe_metadata_from_content
|
from ..paper import get_bibentry_from_string, get_safe_metadata_from_content
|
||||||
|
from .helpers import add_references_argument, parse_reference
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
parser = subparsers.add_parser('edit',
|
parser = subparsers.add_parser('edit',
|
||||||
help='open the paper bibliographic file in an editor')
|
help='open the paper bibliographic file in an editor')
|
||||||
parser.add_argument('reference',
|
|
||||||
help='reference to the paper (citekey or number)')
|
|
||||||
parser.add_argument('-m', '--meta', action='store_true', default=False,
|
parser.add_argument('-m', '--meta', action='store_true', default=False,
|
||||||
help='edit metadata')
|
help='edit metadata')
|
||||||
|
add_references_argument(parser, single=True)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def command(config, ui, reference, meta):
|
def command(config, ui, meta, reference):
|
||||||
rp = repo.Repository.from_directory(config)
|
rp = repo.Repository.from_directory(config)
|
||||||
key = rp.citekey_from_ref(reference, fatal=True)
|
key = parse_reference(ui, rp, reference)
|
||||||
paper = rp.paper_from_citekey(key)
|
paper = rp.paper_from_citekey(key)
|
||||||
to_edit = 'bib'
|
to_edit = 'bib'
|
||||||
if meta:
|
if meta:
|
||||||
|
@ -4,6 +4,7 @@ from pybtex.database import BibliographyData
|
|||||||
|
|
||||||
from .. import repo
|
from .. import repo
|
||||||
from .. import files
|
from .. import files
|
||||||
|
from .helpers import parse_references, add_references_argument
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
@ -11,16 +12,21 @@ def parser(subparsers, config):
|
|||||||
help='export bibliography')
|
help='export bibliography')
|
||||||
parser.add_argument('-f', '--bib-format', default='bibtex',
|
parser.add_argument('-f', '--bib-format', default='bibtex',
|
||||||
help="export format")
|
help="export format")
|
||||||
|
add_references_argument(parser)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def command(config, ui, bib_format):
|
def command(config, ui, bib_format, references):
|
||||||
"""
|
"""
|
||||||
:param bib_format (in 'bibtex', 'yaml')
|
:param bib_format (in 'bibtex', 'yaml')
|
||||||
"""
|
"""
|
||||||
rp = repo.Repository.from_directory(config)
|
rp = repo.Repository.from_directory(config)
|
||||||
|
papers = [rp.paper_from_citekey(c)
|
||||||
|
for c in parse_references(ui, rp, references)]
|
||||||
|
if len(papers) == 0:
|
||||||
|
papers = rp.all_papers()
|
||||||
bib = BibliographyData()
|
bib = BibliographyData()
|
||||||
for p in rp.all_papers():
|
for p in papers:
|
||||||
bib.add_entry(p.citekey, p.bibentry)
|
bib.add_entry(p.citekey, p.bibentry)
|
||||||
try:
|
try:
|
||||||
files.write_bibdata(bib, sys.stdout, bib_format)
|
files.write_bibdata(bib, sys.stdout, bib_format)
|
||||||
|
48
papers/commands/helpers.py
Normal file
48
papers/commands/helpers.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
from .. import files
|
||||||
|
from .. import color
|
||||||
|
from ..repo import InvalidReference
|
||||||
|
|
||||||
|
|
||||||
|
def add_references_argument(parser, single=False):
|
||||||
|
if single:
|
||||||
|
parser.add_argument('reference',
|
||||||
|
help='reference to the paper (citekey or number)')
|
||||||
|
else:
|
||||||
|
parser.add_argument('references', nargs='*',
|
||||||
|
help="one or several reference to export (citekeysor numbers)")
|
||||||
|
|
||||||
|
|
||||||
|
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 parse_reference(ui, rp, ref):
|
||||||
|
try:
|
||||||
|
return rp.citekey_from_ref(ref)
|
||||||
|
except InvalidReference:
|
||||||
|
ui.error("no paper with reference: %s."
|
||||||
|
% color.dye(ref, color.citekey))
|
||||||
|
ui.exit(-1)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_references(ui, rp, refs):
|
||||||
|
citekeys = [parse_reference(ui, rp, ref) for ref in refs]
|
||||||
|
return citekeys
|
@ -1,6 +1,6 @@
|
|||||||
from .. import repo
|
from .. import repo
|
||||||
from ..paper import Paper
|
from ..paper import Paper
|
||||||
from add_cmd import add_paper_with_docfile, extract_doc_path_from_bibdata
|
from .helpers import add_paper_with_docfile, extract_doc_path_from_bibdata
|
||||||
from .. import configs
|
from .. import configs
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ from .. import repo
|
|||||||
from ..paper import NoDocumentFile
|
from ..paper import NoDocumentFile
|
||||||
from .. import configs
|
from .. import configs
|
||||||
from .. import color
|
from .. import color
|
||||||
|
from .helpers import add_references_argument, parse_reference
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
@ -11,14 +12,14 @@ def parser(subparsers, config):
|
|||||||
help='open the paper in a pdf viewer')
|
help='open the paper in a pdf viewer')
|
||||||
parser.add_argument('-w', '--with', dest='with_command', default=None,
|
parser.add_argument('-w', '--with', dest='with_command', default=None,
|
||||||
help='command to use to open the document file')
|
help='command to use to open the document file')
|
||||||
parser.add_argument('citekey',
|
add_references_argument(parser, single=True)
|
||||||
help='the paper associated citekey')
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def command(config, ui, with_command, citekey):
|
def command(config, ui, with_command, reference):
|
||||||
rp = repo.Repository.from_directory(config)
|
rp = repo.Repository.from_directory(config)
|
||||||
paper = rp.paper_from_ref(citekey, fatal=True)
|
key = parse_reference(ui, rp, reference)
|
||||||
|
paper = rp.paper_from_citekey(key)
|
||||||
if with_command is None:
|
if with_command is None:
|
||||||
with_command = config.get(configs.MAIN_SECTION, 'open-cmd')
|
with_command = config.get(configs.MAIN_SECTION, 'open-cmd')
|
||||||
try:
|
try:
|
||||||
@ -27,7 +28,7 @@ def command(config, ui, with_command, citekey):
|
|||||||
ui.print_('{} opened.'.format(color.dye(filepath, color.filepath)))
|
ui.print_('{} opened.'.format(color.dye(filepath, color.filepath)))
|
||||||
except NoDocumentFile:
|
except NoDocumentFile:
|
||||||
ui.error('No document associated with the entry {}.'.format(
|
ui.error('No document associated with the entry {}.'.format(
|
||||||
color.dye(citekey, color.citekey)))
|
color.dye(key, color.citekey)))
|
||||||
ui.exit()
|
ui.exit()
|
||||||
except OSError:
|
except OSError:
|
||||||
ui.error("Command does not exist: %s." % with_command)
|
ui.error("Command does not exist: %s." % with_command)
|
||||||
|
@ -1,20 +1,21 @@
|
|||||||
from .. import repo
|
from .. import repo
|
||||||
from .. import color
|
from .. import color
|
||||||
|
from .helpers import add_references_argument, parse_references
|
||||||
|
|
||||||
|
|
||||||
def parser(subparsers, config):
|
def parser(subparsers, config):
|
||||||
parser = subparsers.add_parser('remove', help='removes a paper')
|
parser = subparsers.add_parser('remove', help='removes a paper')
|
||||||
parser.add_argument('reference',
|
add_references_argument(parser)
|
||||||
help='reference to the paper (citekey or number)')
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
|
||||||
def command(config, ui, reference):
|
def command(config, ui, references):
|
||||||
rp = repo.Repository.from_directory(config)
|
rp = repo.Repository.from_directory(config)
|
||||||
key = rp.citekey_from_ref(reference, fatal=True)
|
citekeys = parse_references(ui, rp, references)
|
||||||
paper = rp.paper_from_citekey(key)
|
are_you_sure = ("Are you sure you want to delete paper(s) [%s]"
|
||||||
are_you_sure = ("Are you sure you want to delete paper [%s]"
|
|
||||||
" (this will also delete associated documents)?"
|
" (this will also delete associated documents)?"
|
||||||
% color.dye(paper.citekey, color.citekey))
|
% ', '.join([color.dye(c, color.citekey) for c in citekeys]))
|
||||||
sure = ui.input_yn(question=are_you_sure, default='n')
|
sure = ui.input_yn(question=are_you_sure, default='n')
|
||||||
if sure:
|
if sure:
|
||||||
rp.remove(paper.citekey)
|
for c in citekeys:
|
||||||
|
rp.remove(c)
|
||||||
|
@ -4,7 +4,6 @@ import glob
|
|||||||
|
|
||||||
from . import files
|
from . import files
|
||||||
from .paper import PaperInRepo, NoDocumentFile
|
from .paper import PaperInRepo, NoDocumentFile
|
||||||
from . import color
|
|
||||||
from . import configs
|
from . import configs
|
||||||
|
|
||||||
|
|
||||||
@ -19,6 +18,10 @@ class CiteKeyAlreadyExists(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidReference(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Repository(object):
|
class Repository(object):
|
||||||
|
|
||||||
def __init__(self, config=None):
|
def __init__(self, config=None):
|
||||||
@ -37,7 +40,7 @@ class Repository(object):
|
|||||||
self, self.path_to_paper_file(citekey, 'bib'),
|
self, self.path_to_paper_file(citekey, 'bib'),
|
||||||
metapath=self.path_to_paper_file(citekey, 'meta'))
|
metapath=self.path_to_paper_file(citekey, 'meta'))
|
||||||
|
|
||||||
def citekey_from_ref(self, ref, fatal=True):
|
def citekey_from_ref(self, ref):
|
||||||
"""Tries to get citekey from given ref.
|
"""Tries to get citekey from given ref.
|
||||||
Ref can be a citekey or a number.
|
Ref can be a citekey or a number.
|
||||||
"""
|
"""
|
||||||
@ -47,15 +50,10 @@ class Repository(object):
|
|||||||
try:
|
try:
|
||||||
return self.citekeys[int(ref)]
|
return self.citekeys[int(ref)]
|
||||||
except (IndexError, ValueError):
|
except (IndexError, ValueError):
|
||||||
if fatal:
|
raise(InvalidReference)
|
||||||
print('{}: no paper with reference {}'.format(
|
|
||||||
color.dye('error', color.error),
|
|
||||||
color.dye(ref, color.citekey)))
|
|
||||||
exit(-1)
|
|
||||||
raise(IOError('file not found'))
|
|
||||||
|
|
||||||
def paper_from_ref(self, ref, fatal=True):
|
def paper_from_ref(self, ref):
|
||||||
key = self.citekey_from_ref(ref, fatal=fatal)
|
key = self.citekey_from_ref(ref)
|
||||||
return self.paper_from_citekey(key)
|
return self.paper_from_citekey(key)
|
||||||
|
|
||||||
# creating new papers
|
# creating new papers
|
||||||
|
Loading…
x
Reference in New Issue
Block a user