def expand(file, entry): items = entry[4] for k in items.keys(): items[k] = _bibtex.expand(file, items[k], typemap.get(k, -1)) items['key'] = entry[0] items['type'] = entry[1] return items
def open(self, filename): """Read and parse bibtex file using python-bibtex.""" # figure out what the second argument means file = _bibtex.open_file(filename, 1) while 1: entry = _bibtex.next(file) if entry == None: break eprops = {} for k, v in entry[4].iteritems(): # figure out what the last argument really does # leaving in -1 seems to be save value = _bibtex.expand(file, v, 0)[2] try: value = unicode(value, 'utf-8') except UnicodeDecodeError, e: print "ERROR: Failed to decode string '%s'" % value raise if k.lower() == 'author': value = value.split(' and ') if k.lower() == 'pages': value = tuple(value.replace('-', ' ').split()) eprops[k] = value # bibtex key is dict key self[entry[0]] = (entry[1], eprops)
def expand(file, entry, type=-1): """Inline the expanded respresentation of each field.""" bibkey, bibtype, a, b, items = entry results = [] for k in sorted(items): results.append((k, _bibtex.expand(file, items[k], typemap.get(k, -1)))) return (bibkey, bibtype, a, b, results)
def __init__(self, context, t): """Constructs a BibtexRecord using the _bibtex mystery tuple.""" ## A hack to deal with _bibtex' incomplete handling of the "month" field self.date = [0,0] # Month, Year ## self.handle = t[0] self.entry_type = t[1] t[2] #Mystery! t[3] #Mystery! self.typemap = {} #If we know that some fields should be a particular type. self.data = {} items = t[4] for k in items.keys(): ty = self.typemap.get(k, -1) x = _bibtex.expand(context, items[k], ty) if k == "month": month = BIBTEX_MONTHS[_bibtex.get_native(items[k])] self.date[0] = month self.data["month"] = month continue if ty == -1: ty = x[0] self.data[k] = self.build(ty, x) if 'year' in self.data.keys(): self.date[1] = self.data['year'] else: self.date[1] = 1900
def field_and_loss(self, key): # look in the cache first if self.__text.has_key(key): return self.__text[key] obj = self.dict[key] # search its declared type fieldtype = Types.get_field(key) ret = _bibtex.expand(self.parser, obj, _fieldtype(fieldtype)) fieldtype = fieldtype.type if fieldtype == AuthorGroup: # Author val = AuthorGroup() for aut in ret[3]: val.append(Author(aut)) elif fieldtype == Date: # Date val = Date((ret[3], None, None)) elif fieldtype == LongText: # Annotation text val = BibLongTextField(ret[2], self.get_latex(key)) elif fieldtype == Text: # Any other text val = BibTextField(ret[2], self.get_latex(key)) elif fieldtype == Reference: # a reference on the same database val = Reference(ret[2], self.key.base) else: # specific fields, like URL val = fieldtype(ret[2]) self.__text[key] = (val, ret[1]) return (val, ret[1])
def open(self, filename): """Read and parse bibtex file using python-bibtex.""" # figure out what the second argument means file = _bibtex.open_file(filename, 1) while 1: entry = _bibtex.next(file) if entry == None: break eprops = {} for k,v in entry[4].iteritems(): # figure out what the last argument really does # leaving in -1 seems to be save value = _bibtex.expand(file, v, 0)[2] try: value = unicode(value, 'utf-8') except UnicodeDecodeError, e: print "ERROR: Failed to decode string '%s'" % value raise if k.lower() == 'author': value = value.split(' and ') if k.lower() == 'pages': value = tuple(value.replace('-', ' ').split()) eprops[k] = value # bibtex key is dict key self[entry[0]] = (entry[1],eprops)
def __init__(self, context, t): """Constructs a BibtexRecord using the _bibtex mystery tuple.""" ## A hack to deal with _bibtex' incomplete handling of the "month" field self.date = [0, 0] # Month, Year ## self.handle = t[0] self.entry_type = t[1] t[2] #Mystery! t[3] #Mystery! self.typemap = { } #If we know that some fields should be a particular type. self.data = {} items = t[4] for k in items.keys(): ty = self.typemap.get(k, -1) x = _bibtex.expand(context, items[k], ty) if k == "month": month = BIBTEX_MONTHS[self._strip_braces( _bibtex.get_native(items[k]))] self.date[0] = month self.data["month"] = month continue if ty == -1: ty = x[0] self.data[k] = self.build(ty, x) self.date[1] = self.data["year"]
def field_and_loss(self, key): # look in the cache first if self.__text.has_key(key): return self.__text[key] obj = self.dict[key] # search its declared type fieldtype = Types.get_field(key) ret = _bibtex.expand(self.parser, obj, _fieldtype(fieldtype)) fieldtype = fieldtype.type if fieldtype == AuthorGroup: # Author val = AuthorGroup () for aut in ret[3]: val.append (Author(aut)) elif fieldtype == Date: # Date val = Date((ret[3], None, None)) elif fieldtype == LongText: # Annotation text val = BibLongTextField(ret[2], self.get_latex(key)) elif fieldtype == Text: # Any other text val = BibTextField(ret[2], self.get_latex(key)) elif fieldtype == Reference: # a reference on the same database val = Reference(ret[2], self.key.base) else: # specific fields, like URL val = fieldtype(ret[2]) self.__text[key] = (val, ret[1]) return (val, ret[1])
def expand(file, entry): if entry[0] in ("preamble", "string"): return entry bibkind, (bibkey, bibtype, a, b, items) = entry results = [(k, _bibtex.expand(file, items[k], -1)) for k in sorted(items)] return (bibkind, (bibkey, bibtype, a, b, results))
def expand(file, entry, type=-1): """Inline the expanded respresentation of each field.""" bibkey, bibtype, a, b, items = entry results = [] for k in sorted(items): results.append( (k, _bibtex.expand(file, items[k], typemap.get(k, -1)))) return (bibkey, bibtype, a, b, results)
def expand (f, entry, typemap = -1): items = entry [4] for k in items.keys (): items [k] = _bibtex.expand (f, items [k], typemap) if k!='year': items[k]=items[k][-1] items['type']=entry[1] items['bibTex']=entry[0] return items
def expand(file, entry): if entry[0] in ('preamble', 'string'): return entry bibkind, (bibkey, bibtype, a, b, items) = entry results = [(k, _bibtex.expand(file, items[k], -1)) for k in sorted(items)] return (bibkind, (bibkey, bibtype, a, b, results))
def readBib(filename): ## _bibtex has horrible interface bibfile=bib.open_file(filename,1) # 2nd argument: strict mode db={} #rq=bibRecode.request('latex..latin1') while True: entry=bib.next(bibfile) if entry is None: break key,type,dta=entry[0],entry[1],entry[4] item={'type':type} for field in dta.keys(): expanded=bib.expand(bibfile,dta[field],-1) #conv=bibRecode.recode(rq,expanded[2]) item[field]=expanded[2].strip() db[key]=item ## now we don't need _bibtex anymore, everything is in our dicts return db
def readBib(filename): ## _bibtex has horrible interface bibfile = bib.open_file(filename, 1) # 2nd argument: strict mode db = {} #rq=bibRecode.request('latex..latin1') while True: entry = bib.next(bibfile) if entry is None: break key, type, dta = entry[0], entry[1], entry[4] item = {'type': type} for field in dta.keys(): expanded = bib.expand(bibfile, dta[field], -1) #conv=bibRecode.recode(rq,expanded[2]) item[field] = expanded[2].strip() db[key] = item ## now we don't need _bibtex anymore, everything is in our dicts return db
def parse(bibfilename, strict=0): print "opening bibtex file ", bibfilename file = _bibtex.open_file(bibfilename, strict) entry = _bibtex.next(file) counter = 0 answer = {} while (entry <> None): counter += 1 key = entry[0] # such as lemire-kaser2003 type = entry[1] #article, proc... items = entry[4] myentry = {} myentry['type'] = type for itemkey in items.keys(): myentry[itemkey.lower()] = cgi.escape( _bibtex.expand(file, items[itemkey], -1)[2]) answer[key] = myentry entry = _bibtex.next(file) print "parsed ", counter, " entries" return answer
if False: print importer("http://portal.acm.org/citation.cfm?id=277650.277719").load_bibtex() print importer("http://portal.acm.org/citation.cfm?id=324550").load_bibtex() print importer("http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.23.414").load_bibtex() test = importer("http://eprint.iacr.org/2009/137").load_bibtex() print test else: test = """@misc{cryptoeprint:2009:137, author = {Nicolas T. Courtois}, title = {The Dark Side of Security by Obscurity and Cloning MiFare Classic Rail and Building Passes Anywhere, Anytime}, howpublished = {Cryptology ePrint Archive, Report 2009/137}, year = {2009}, note = {\url{http://eprint.iacr.org/}}, }""" if "_bibtex" in sys.modules: b = _bibtex.open_string("foo", test, True) print b _bibtex.first(b) i = _bibtex.next(b) while i: print i items = i[4] for k in items.keys(): items[k] = _bibtex.expand(b, items[k], -1) print items i = _bibtex.next(b)
for m in matches: for k in m.split(','): start_keys.add(k) keys_to_papers = {} while True: be = _bibtex.next(b) if not be: break key = be[0] print >> sys.stderr, "===" + str(key) bh = be[4] h = {} for k in be[4].keys (): h[k] = _bibtex.expand(b,bh[k],-1) if ("title" in h) and ("year" in h): t = h["title"] y = h["year"] p = Paper(key,t[2],y[2]) bibtex_papers.add(p) keys_to_papers[key] = p paper_pdf_file = os.path.join(pdf_directory,key+".pdf") if os.path.exists(paper_pdf_file): papers_with_pdf_versions.add(p) else: print >> sys.stderr, "Warning: no PDF file "+paper_pdf_file if not tex_file: start_keys.add(key) earliest_year = 10000
for m in matches: for k in m.split(','): start_keys.add(k) keys_to_papers = {} while True: be = _bibtex.next(b) if not be: break key = be[0] print >> sys.stderr, "===" + str(key) bh = be[4] h = {} for k in be[4].keys(): h[k] = _bibtex.expand(b, bh[k], -1) if ("title" in h) and ("year" in h): t = h["title"] y = h["year"] p = Paper(key, t[2], y[2]) bibtex_papers.add(p) keys_to_papers[key] = p paper_pdf_file = os.path.join(pdf_directory, key + ".pdf") if os.path.exists(paper_pdf_file): papers_with_pdf_versions.add(p) else: print >> sys.stderr, "Warning: no PDF file " + paper_pdf_file if not tex_file: start_keys.add(key) earliest_year = 10000