def set_ref_from_entry(key, bib_data, ref_doc): stream = StringIO.StringIO() writer = bib_out.Writer(encoding='ascii') writer.write_entry(key, bib_data.entries[key], stream) ref_doc['bibtex'] = stream.getvalue() try: for field in bib_data.entries[key].fields: value = bib_data.entries[key].fields[field] fieldname = field.strip().lower() rawvalue = value value = bibtex_purify(value) if fieldname == 'year': ref_doc['year'] = int(value) elif fieldname == 'doi': doiPrepender = 'http://dx.doi.org/' if not rawvalue.startswith("http://"): rawvalue = doiPrepender + rawvalue ref_doc['url'] = rawvalue elif fieldname == 'url': ref_doc['url'] = rawvalue else: ref_doc[fieldname] = value authorsAsText = "" firstAuthorSortKey = "" count = 0 if bib_data.entries[key].persons: numberOfAuthors = len(bib_data.entries[key].persons['author']) for person in bib_data.entries[key].persons['author']: first = person.get_part_as_text('first') last = person.get_part_as_text('last') simpleAuthor = bibtex_purify(first + ' ' + last) if 'emph' in simpleAuthor: simpleAuthor = simpleAuthor.replace('emph', '') authorsAsText += simpleAuthor if numberOfAuthors > 1: if count != (numberOfAuthors - 1): authorsAsText += ', ' if count == (numberOfAuthors - 2): authorsAsText += ' and ' count += 1 firstAuthor = bib_data.entries[key].persons['author'][0] firstAuthorSortKey = firstAuthor.get_part_as_text( 'last') + firstAuthor.get_part_as_text('first') ref_doc['authorsAsText'] = authorsAsText ref_doc['firstAuthorSortKey'] = firstAuthorSortKey except Exception, e: print "**** BIBTEX PARSING SCREWED UP ****" print e
def as_string(self): """Return entry as formatted bibtex string.""" writer = outparser.Writer() with io.StringIO() as stream: writer.write_stream(self._entry2db(), stream) string = stream.getvalue() string = string.strip() return string
def bibtex_edit(filename, tax_id, ref_doc): parser = bib_in.Parser() bib_data = parser.parse_file(filename) writer = bib_out.Writer(encoding='ascii') assert len(bib_data.entries.keys()) == 1 firstEntry = bib_data.entries.keys()[0] ref_doc = set_ref_from_entry(firstEntry, bib_data, ref_doc) ReferenceFamily(tax_id).save_reference(ref_doc)
def bibtex_import(filename): parser = bib_in.Parser() bib_data = parser.parse_file(filename) writer = bib_out.Writer(encoding='ascii') for key in bib_data.entries.keys(): print 'Key ' + key stream = StringIO.StringIO() writer.write_entry(key, bib_data.entries[key], stream) ref_obj = get_ref(key, stream.getvalue()) ref_obj.save() for field in bib_data.entries[key].fields: value = bib_data.entries[key].fields[field] col = get_column(field) if 'title' in field.lower(): ref_obj.title = value elif 'journal' in field.lower(): ref_obj.journal = value elif 'year' in field.lower(): ref_obj.year = int(value) attr = ReferenceAttribute(column=col, value=value) attr.save() ref_obj.referenceAttributes.add(attr) if bib_data.entries[key].persons: for person in bib_data.entries[key].persons['author']: first = person.get_part_as_text('first') middle = person.get_part_as_text('middle') prelast = person.get_part_as_text('prelast') last = person.get_part_as_text('last') lineage = person.get_part_as_text('lineage') auth = ReferenceAuthor(first_name=first, middle_name=middle, prelast_name=prelast, last_name=last, lineage=lineage) auth.save() ref_obj.authors.add(auth) ref_obj.save() print "Imported %i BibTeX references." % len(bib_data.entries)
from __future__ import print_function, unicode_literals, with_statement import re import sys from os import path from shutil import rmtree from subprocess import PIPE, Popen from tempfile import mkdtemp from pybtex.database import BibliographyData, Entry, Person from pybtex.database.output import bibtex from pybtex.errors import report_error from pybtex.exceptions import PybtexError writer = bibtex.Writer(encoding='ascii') def write_aux(filename, citations): with open(filename, 'w') as aux_file: for citation in citations: aux_file.write('\\citation{%s}\n' % citation) aux_file.write('\\bibdata{test}\n') aux_file.write('\\bibstyle{test}\n') def write_bib(filename, database): writer.write_file(database, filename) def write_bst(filename, style):
def bibtex_import(filename, taxonomyItem): parser = bib_in.Parser() bib_data = parser.parse_file(filename) writer = bib_out.Writer(encoding='ascii') for key in bib_data.entries.keys(): stream = StringIO.StringIO() writer.write_entry(key, bib_data.entries[key], stream) title = getTitle(bib_data.entries[key].fields) ref_obj = get_ref(key, title, stream.getvalue()) try: ref_obj.save() for field in bib_data.entries[key].fields: value = bib_data.entries[key].fields[field] col = get_column(field) if 'title' in field.lower(): ref_obj.title = title elif 'journal' in field.lower(): ref_obj.journal = bibtex_purify(value) elif 'year' in field.lower(): ref_obj.year = int(value) elif 'url' in field.lower(): ref_obj.url = value elif 'abstract' in field.lower(): ref_obj.abstract = value elif 'doi' in field.lower(): doiPrepender = 'http://dx.doi.org/' if not value.startswith(doiPrepender): value = doiPrepender + value ref_obj.url = value attr = ReferenceAttribute(column=col, value=value) attr.save() ref_obj.referenceAttributes.add(attr) authorsAsText = "" count = 0 if bib_data.entries[key].persons: numberOfAuthors = len(bib_data.entries[key].persons['author']) for person in bib_data.entries[key].persons['author']: first = person.get_part_as_text('first') middle = person.get_part_as_text('middle') prelast = person.get_part_as_text('prelast') last = person.get_part_as_text('last') lineage = person.get_part_as_text('lineage') simpleAuthor = bibtex_purify(first + ' ' + last) if 'emph' in simpleAuthor: simpleAuthor = simpleAuthor.replace('emph', '') authorsAsText += simpleAuthor if numberOfAuthors > 1: if count != (numberOfAuthors -1): authorsAsText += ', ' if count == (numberOfAuthors - 2): authorsAsText += ' and ' count += 1 try: author, created = ReferenceAuthor.objects.get_or_create(first_name=first, last_name=last) except Exception, e: logger.debug( 'Author ' + first + ' ' + last + ' exists in multiple places. Error is ' + e.message) author = ReferenceAuthor.objects.filter(first_name=first).filter(last_name=last).__getitem__(0) created = False if created: author.middle_name = middle author.prelast_name = prelast author.lineage = lineage author.save() if not author in ref_obj.authors.all(): ref_obj.authors.add(author) ref_obj.authorsAsText = authorsAsText ref_obj.save() if not ref_obj in taxonomyItem.references.all(): taxonomyItem.references.add(ref_obj) except Exception, e: logger.log(e.message) connection._rollback()
def to_file(self, path): """Write entry bibtex to file.""" writer = outparser.Writer(encoding='utf-8') writer.write_file(self._entry2db(), path)
def write_output(args, bibs): writer = bibtex_output.Writer() writer.write_file(bibs, args.output_bibtex)