def bib2zope(filename, app = None): import _bibtex file = _bibtex.open_file(filename, True) entry = _bibtex.next(file) while entry != None: keys = entry[4].keys() id = entry[0] pubtype = entry[1] print '-----' print 'id:', id print 'pubype:', pubtype print 'keys:', keys for key in keys: print key, ':', _bibtex.get_native(entry[4][key]).strip('{} ') if app != None: publications = app.infnine.publications print "Running in Zope, creating objects in ZODB..." if not publications.hasObject(id): print "Entry", id, "not yet in ZODB, creating" admin = app.acl_users.getUser('admin') import AccessControl AccessControl.SecurityManagement.newSecurityManager(None, admin) from Testing.makerequest import makerequest app = makerequest(app) publications.invokeFactory('Publication', id) pub = publications.__getitem__(id) print "Updating", id, keys from infnine.data.interfaces import IPublication for key in keys: if not key.replace('-', '_') in (IPublication._v_attrs.keys()): print "!!! Missing key:", key value = _bibtex.get_native(entry[4][key]).strip('{} "') if key in ['year']: value = int(value) elif key == 'bib2html_pubtype': value = unicode(filtered_pubtype(value)) else: value = unicode(value) if '-' in key: pub.__setattr__(key.replace('-', '_'), value) else: pub.__setattr__(key, value) pub.reindexObject() import transaction transaction.commit() entry = _bibtex.next(file)
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 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 _iterate_bibtexsource(bs): """Takes one of _bibtex' BibtexSouce objects and returns a list of BibtexRecord objects""" out = [] while True: e = _bibtex.next(bs) if not e: break out.append(e) return map(lambda x: BibtexRecord(bs, x), out)
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
def read_and_parse_file(filename,strict=1): fix_invalid_formats(filename,["author = ","editor = "]) f = _bibtex.open_file (filename, strict) l=[] while 1: try: entry = _bibtex.next (f) if entry is None: break l.append(bib_entry(expand (f, entry))) except IOError, msg: obtained = 'ParserError'
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 checkfile(filename, strict=1, typemap={}): 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) file = _bibtex.open_file(filename, strict) result = open(filename + '-ok', 'r') line = 1 failures = 0 checks = 0 while 1: try: entry = _bibtex.next(file) if entry is None: break obtained = ` expand(file, entry) ` except IOError, msg: obtained = 'ParserError' if _debug: print obtained valid = result.readline().strip() if not expected_result(obtained, valid): sys.stderr.write('error: %s: line %d: unexpected result:\n' % (filename, line)) sys.stderr.write('error: %s: line %d: obtained %s\n' % (filename, line, obtained)) sys.stderr.write('error: %s: line %d: expected %s\n' % (filename, line, valid)) failures = failures + 1 checks = checks + 1
def checkfile(filename, strict=1, typemap={}): 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) file = _bibtex.open_file(filename, strict) result = open(filename + "-ok", "r") line = 1 failures = 0 checks = 0 while 1: try: entry = _bibtex.next(file) if entry is None: break obtained = ` expand(file, entry) ` except IOError, msg: obtained = "ParserError" if _debug: print obtained valid = result.readline().strip() if not expected_result(obtained, valid): sys.stderr.write("error: %s: line %d: unexpected result:\n" % (filename, line)) sys.stderr.write("error: %s: line %d: obtained %s\n" % (filename, line, obtained)) sys.stderr.write("error: %s: line %d: expected %s\n" % (filename, line, valid)) failures = failures + 1 checks = checks + 1
def read_bibtex(content, typemap={}): import _bibtex 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 file = _bibtex.open_string("/dev/null", content, 0) entries = [] while True: try: entry = _bibtex.next(file) if entry is None: break e = expand(file, entry) entries.append(e) except IOError, msg: pass
def next(self): """ iterator interface: get next bibtex entry """ try: return BibTexEntry( _bibtex.next(self.fhandle), self.path ) except TypeError: raise StopIteration
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)
start_keys = set([]) papers_with_pdf_versions = set([]) if tex_file: fp = open(tex_file,"r") matches = re.findall('\\cite.{([^}]+)}',fp.read()) fp.close() 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")
start_keys = set([]) papers_with_pdf_versions = set([]) if tex_file: fp = open(tex_file, "r") matches = re.findall('\\cite.{([^}]+)}', fp.read()) fp.close() 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")
def setUp(self): from os.path import dirname, join as os_join self.bibtex_entry = _bibtex.next( _bibtex.open_file( os_join( dirname(__file__), "../test", BIBTEX_TEST_FILE ), 100 ) )