update attach and export cmds
This commit is contained in:
parent
24df1b36ae
commit
bc82d0de8c
@ -5,8 +5,8 @@ import open_cmd
|
||||
import websearch_cmd
|
||||
import remove_cmd
|
||||
import tag_cmd
|
||||
import attach_cmd
|
||||
import export_cmd
|
||||
# import import_cmd
|
||||
# import export_cmd
|
||||
# import edit_cmd
|
||||
# import attach_cmd
|
||||
# import update_cmd
|
||||
|
@ -1,9 +1,6 @@
|
||||
from .. import repo
|
||||
from ..configs import config
|
||||
from ..uis import get_ui
|
||||
from .helpers import (add_references_argument, parse_reference,
|
||||
add_docfile_to_paper)
|
||||
|
||||
|
||||
def parser(subparsers):
|
||||
parser = subparsers.add_parser('attach',
|
||||
@ -12,8 +9,10 @@ def parser(subparsers):
|
||||
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)")
|
||||
add_references_argument(parser, single=True)
|
||||
parser.add_argument('document', help='pdf or ps file')
|
||||
parser.add_argument('citekey',
|
||||
help='citekey of the paper')
|
||||
parser.add_argument('document',
|
||||
help='document file')
|
||||
return parser
|
||||
|
||||
|
||||
@ -24,19 +23,24 @@ def command(args):
|
||||
"""
|
||||
|
||||
ui = get_ui()
|
||||
|
||||
rp = repo.Repository(config())
|
||||
paper = rp.pull_paper(args.citekey)
|
||||
|
||||
copy = args.copy
|
||||
reference = args.reference
|
||||
document = args.document
|
||||
|
||||
|
||||
if copy is None:
|
||||
copy = config().import_copy
|
||||
rp = repo.Repository(config())
|
||||
key = parse_reference(rp, reference)
|
||||
paper = rp.get_paper(key)
|
||||
|
||||
try:
|
||||
add_docfile_to_paper(rp, paper, docfile=document, copy=copy)
|
||||
document = args.document
|
||||
if copy:
|
||||
document = rp.databroker.copy_doc(paper.citekey, document)
|
||||
else:
|
||||
pass # TODO warn if file does not exists
|
||||
paper.docpath = document
|
||||
except ValueError, v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
||||
# TODO handle case where citekey exists
|
||||
except IOError, v:
|
||||
ui.error(v.message)
|
||||
ui.exit(1)
|
||||
|
@ -1,19 +1,20 @@
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
|
||||
from pybtex.database import BibliographyData
|
||||
|
||||
from .. import repo
|
||||
from .. import files
|
||||
from .helpers import parse_references, add_references_argument
|
||||
from ..configs import config
|
||||
from ..uis import get_ui
|
||||
from .. import endecoder
|
||||
|
||||
def parser(subparsers):
|
||||
parser = subparsers.add_parser('export',
|
||||
help='export bibliography')
|
||||
parser.add_argument('-f', '--bib-format', default='bibtex',
|
||||
help="export format")
|
||||
add_references_argument(parser)
|
||||
help='export format')
|
||||
parser.add_argument('citekeys', nargs='*',
|
||||
help='one or several citekeys')
|
||||
return parser
|
||||
|
||||
|
||||
@ -24,17 +25,23 @@ def command(args):
|
||||
|
||||
ui = get_ui()
|
||||
bib_format = args.bib_format
|
||||
references = args.references
|
||||
|
||||
rp = repo.Repository(config())
|
||||
papers = [rp.get_paper(c)
|
||||
for c in parse_references(rp, references)]
|
||||
|
||||
try:
|
||||
papers = [rp.pull_paper(c) for c in args.citekeys]
|
||||
except repo.InvalidReference, v:
|
||||
ui.error(v)
|
||||
ui.exit(1)
|
||||
|
||||
if len(papers) == 0:
|
||||
papers = rp.all_papers()
|
||||
bib = BibliographyData()
|
||||
for p in papers:
|
||||
bib.add_entry(p.citekey, p.bibentry)
|
||||
try:
|
||||
files.write_bibdata(bib, sys.stdout, bib_format)
|
||||
exporter = endecoder.EnDecoder()
|
||||
bibdata_raw = exporter.encode_bibdata(bib, fmt=bib_format)
|
||||
print(bibdata_raw, end='')
|
||||
except KeyError:
|
||||
ui.error("Invalid output format: %s." % bib_format)
|
||||
|
@ -10,6 +10,8 @@ try:
|
||||
import pybtex.database.input.bibtex
|
||||
import pybtex.database.input.bibtexml
|
||||
import pybtex.database.input.bibyaml
|
||||
import pybtex.database.output.bibtex
|
||||
import pybtex.database.output.bibtexml
|
||||
import pybtex.database.output.bibyaml
|
||||
|
||||
except ImportError:
|
||||
@ -30,9 +32,13 @@ class EnDecoder(object):
|
||||
* encode_bibdata will try to recognize exceptions
|
||||
"""
|
||||
|
||||
decode_fmt = (pybtex.database.input.bibyaml,
|
||||
pybtex.database.input.bibtex,
|
||||
pybtex.database.input.bibtexml)
|
||||
decode_fmt = {'bibyaml' : pybtex.database.input.bibyaml,
|
||||
'bibtex' : pybtex.database.input.bibtex,
|
||||
'bibtexml': pybtex.database.input.bibtexml}
|
||||
|
||||
encode_fmt = {'bibyaml' : pybtex.database.output.bibyaml,
|
||||
'bibtex' : pybtex.database.output.bibtex,
|
||||
'bibtexml': pybtex.database.output.bibtexml}
|
||||
|
||||
def encode_metadata(self, metadata):
|
||||
return yaml.safe_dump(metadata, allow_unicode=True, encoding='UTF-8', indent = 4)
|
||||
@ -40,16 +46,16 @@ class EnDecoder(object):
|
||||
def decode_metadata(self, metadata_raw):
|
||||
return yaml.safe_load(metadata_raw)
|
||||
|
||||
def encode_bibdata(self, bibdata):
|
||||
def encode_bibdata(self, bibdata, fmt='bibyaml'):
|
||||
"""Encode bibdata """
|
||||
s = StringIO.StringIO()
|
||||
pybtex.database.output.bibyaml.Writer().write_stream(bibdata, s)
|
||||
EnDecoder.encode_fmt[fmt].Writer().write_stream(bibdata, s)
|
||||
return s.getvalue()
|
||||
|
||||
def decode_bibdata(self, bibdata_raw):
|
||||
""""""
|
||||
bibdata_rawutf8 = unicode(bibdata_raw)
|
||||
for fmt in EnDecoder.decode_fmt:
|
||||
for fmt in EnDecoder.decode_fmt.values():
|
||||
try:
|
||||
bibdata_stream = StringIO.StringIO(bibdata_rawutf8)
|
||||
return self._decode_bibdata(bibdata_stream, fmt.Parser())
|
||||
|
@ -20,10 +20,10 @@ CORE_CMDS = collections.OrderedDict([
|
||||
('websearch', commands.websearch_cmd),
|
||||
('remove', commands.remove_cmd),
|
||||
('tag', commands.tag_cmd),
|
||||
('attach', commands.attach_cmd),
|
||||
('export', commands.export_cmd),
|
||||
# ('import', commands.import_cmd),
|
||||
# ('export', commands.export_cmd),
|
||||
# ('edit', commands.edit_cmd),
|
||||
# ('attach', commands.attach_cmd),
|
||||
# ('update', commands.update_cmd),
|
||||
])
|
||||
|
||||
|
@ -55,7 +55,7 @@ class Repository(object):
|
||||
citekey=citekey,
|
||||
metadata=self.databroker.pull_metadata(citekey))
|
||||
else:
|
||||
raise InvalidReference
|
||||
raise InvalidReference('{} citekey not found'.format(citekey))
|
||||
|
||||
def push_paper(self, paper, overwrite=False, event=True):
|
||||
""" Push a paper to disk
|
||||
|
Loading…
x
Reference in New Issue
Block a user