def get_pybtex_object(obj): '''convert from PieObject to a pybtex Entry''' if not obj.BibData_Type: raise KeyError, 'Necessary fields missing - BibTeX type' pybtex_entry = Entry(obj.BibData.Type.lower()) for btkey, objfield in bibtexmap: if btkey == 'author': if obj.AuthorIsCorporate(): pybtex_entry.add_person(Person('{%s}' % obj.Author()), btkey) else: for name in split_name_list(obj.Author()): pybtex_entry.add_person(Person(name), btkey) elif btkey == 'editor': for name in split_name_list(obj.Author()): pybtex_entry.add_person(Person(name), btkey) elif btkey == 'title': pybtex_entry.fields[btkey] = obj.Title() else: pybtex_entry.fields[btkey] = getattr(obj, bibtexmap[btkey]) pybtex_entry.fields['month'] = obj.ReferDate().strftime('%B') pybtex_entry.fields['year'] = obj.ReferDate.strftime('%Y') return pybtex_entry
def process_entry(self, entry_type, key, fields): entry = Entry(entry_type) if key is None: key = 'unnamed-%i' % self.unnamed_entry_counter self.unnamed_entry_counter += 1 for field_name, field_value_list in fields: field_value = textutils.normalize_whitespace(self.flatten_value_list(field_value_list)) if field_name in self.person_fields: for name in split_name_list(field_value): entry.add_person(Person(name), field_name) else: entry.fields[field_name] = field_value self.data.add_entry(key, entry)
def process_entry(self, entry_type, key, fields): entry = Entry(entry_type) if key is None: key = 'unnamed-%i' % self.unnamed_entry_counter self.unnamed_entry_counter += 1 seen_fields = set() for field_name, field_value_list in fields: if field_name.lower() in seen_fields: self.handle_error(DuplicateField(key, field_name)) continue field_value = textutils.normalize_whitespace(self.flatten_value_list(field_value_list)) if field_name in self.person_fields: for name in split_name_list(field_value): entry.add_person(Person(name), field_name) else: entry.fields[field_name] = field_value seen_fields.add(field_name.lower()) self.data.add_entry(key, entry)
def data2bib(data, key, type='article'): """Convert a python dict into a Bibentry object.""" if not data: return # need to remove authors field from data authors = None if 'authors' in data: authors = data['authors'] if isinstance(authors, str): authors = split_name_list(authors) if len(authors) == 1: authors = authors[0].split(',') del data['authors'] entry = Entry(type, fields=data) if authors: for p in authors: entry.add_person(Person(p), 'author') return Bibentry(key, entry)
def data2bib(data, key, type='article'): """Convert a python dict into a Bibentry object.""" if not data: return # need to remove authors field from data authors = None if 'authors' in data: authors = data['authors'] if isinstance(authors, str): authors = split_name_list(authors) if len(authors) == 1: authors = authors[0].split(',') del data['authors'] entry = Entry(type, fields=data) if authors: for p in authors: entry.add_person(Person(p), 'author') return Bibentry(key, entry).as_string()
def process_entry(self, entry_type, key, fields): entry = Entry(entry_type) if key is None: key = 'unnamed-%i' % self.unnamed_entry_counter self.unnamed_entry_counter += 1 already_handled_person_fields = set() for field_name, field_value_list in fields: field_value = textutils.normalize_whitespace( self.flatten_value_list(field_value_list)) if field_name in self.person_fields: if field_name in already_handled_person_fields: error_message = 'entry with key {} has a duplicate {} field'.format( key, field_name) self.handle_error(DuplicatePersonField(error_message)) continue for name in split_name_list(field_value): entry.add_person(Person(name), field_name) already_handled_person_fields.add(field_name) else: entry.fields[field_name] = field_value self.data.add_entry(key, entry)
def names(s): for name in split_name_list(s): try: yield Name.from_string(name) except PybtexError as e: # pragma: no cover print(repr(e))
def num_names(i): names = i.pop() i.push(len(utils.split_name_list(names)))
def _split_names(names): return utils.split_name_list(names)
def names(s): for name in split_name_list(s): try: yield Name.from_string(name) except PybtexError as e: print(repr(e))
def get_pybtex_object(obj, texify=True): '''convert from PieObject to a pybtex Entry''' def f_(text, protectcaps=False): if type(text) in (str, unicode): if protectcaps: text = protect_caps( text, PIE_CONFIG.getboolean( 'Format', 'protect_all_caps_in_citations') ) if texify: text = eblc(text) return text else: #this ain't text, don't touch it return text if not obj.BibData_Type: raise KeyError, 'Necessary fields missing - BibTeX type' pybtex_entry = Entry(obj.BibData_Type.lower()) key_set = False for btkey, objfield in bibtexmap.items(): # if btkey == 'publisher': # print 'yes publisher:', getattr(obj, objfield) if btkey == 'author': if obj.AuthorIsCorporate(): pybtex_entry.add_person(Person('{%s}' % obj.Author()), btkey) else: for name in split_name_list(obj.Author()): pybtex_entry.add_person(Person(name), btkey) continue elif btkey == 'editor': if not getattr(obj, objfield): continue for name in split_name_list(getattr(obj, objfield)): pybtex_entry.add_person(Person(name), btkey) continue elif btkey == 'title': pybtex_entry.fields[btkey] = f_(obj.Title(texstuff=True), protectcaps=False) continue elif type(getattr(obj, objfield)) not in (str, unicode): continue elif len(getattr(obj, objfield)) == 0: continue elif btkey == 'url': pybtex_entry.fields[btkey] = f_(obj.Url()) continue elif btkey == 'pie_bibdatakey': pybtex_entry.key = getattr(obj, objfield) key_set = True elif btkey in ('bttype', 'pie_corpauthor', 'pie_datepublished'): continue # elif type(getattr(obj, objfield) == datetime.datetime): # continue else: # if not getattr(obj, objfield): continue pybtex_entry.fields[btkey] = f_(getattr(obj, bibtexmap[btkey])) # if btkey == 'publisher': print 'PUBLISHER SET' if not (obj.ReferDate().day == 1 and obj.ReferDate().month == 1): # hacky hack - if publication date is supposedly 1 January, # then we disbelieve it and assume that only the year has been # set. pybtex_entry.fields['month'] = obj.ReferDate().strftime('%B') if not key_set: pybtex_entry.key = 'nominal_key' pybtex_entry.fields['year'] = obj.ReferDate().strftime('%Y') # pprint(pybtex_entry.fields) return pybtex_entry