This involved many changes, some side effects of the change include: - remove of all `u"abc"` forms, in favor of `from __future__ import unicode_literals`. Their usage was inconsistent anyway, leading to problems when mixing with unicode content. - improve the tests, to allow printing for usecase even when crashing. Should make future test easier. This is done with a rather hacky `StdIO` class in `p3`, but it works. - for some reason, the skipped test for Python 2 seems to work now. While the previous point might seem related, it is not clear that this is actually the case.
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
from __future__ import unicode_literals
|
|
|
|
import argparse
|
|
|
|
from .. import repo
|
|
from ..uis import get_ui
|
|
from .. import endecoder
|
|
from ..utils import resolve_citekey_list
|
|
from ..endecoder import BIBFIELD_ORDER
|
|
from ..completion import CiteKeyCompletion, CommaSeparatedListCompletion
|
|
|
|
|
|
class CommaSeparatedList(argparse.Action):
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
setattr(namespace, self.dest, [s for s in values.split(',') if s])
|
|
|
|
|
|
class FieldCommaSeparatedListCompletion(CommaSeparatedListCompletion):
|
|
|
|
values = BIBFIELD_ORDER
|
|
|
|
|
|
def parser(subparsers, conf):
|
|
parser = subparsers.add_parser('export', help='export bibliography')
|
|
parser.add_argument(
|
|
'--ignore-fields', default=[], action=CommaSeparatedList,
|
|
help='exclude field(s) from output (comma separated if multiple)'
|
|
).completer = FieldCommaSeparatedListCompletion(conf)
|
|
# parser.add_argument('-f', '--bib-format', default='bibtex',
|
|
# help='export format')
|
|
parser.add_argument('citekeys', nargs='*', help='one or several citekeys'
|
|
).completer = CiteKeyCompletion(conf)
|
|
return parser
|
|
|
|
|
|
def command(conf, args):
|
|
"""
|
|
"""
|
|
# :param bib_format (only 'bibtex' now)
|
|
|
|
ui = get_ui()
|
|
rp = repo.Repository(conf)
|
|
|
|
papers = []
|
|
if len(args.citekeys) < 1:
|
|
papers = rp.all_papers()
|
|
else:
|
|
for key in resolve_citekey_list(repo=rp, citekeys=args.citekeys, ui=ui, exit_on_fail=True):
|
|
papers.append(rp.pull_paper(key))
|
|
|
|
bib = {}
|
|
for p in papers:
|
|
bib[p.citekey] = p.bibdata
|
|
|
|
exporter = endecoder.EnDecoder()
|
|
bibdata_raw = exporter.encode_bibdata(bib, args.ignore_fields)
|
|
ui.message(bibdata_raw)
|
|
|
|
rp.close()
|