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 __parsefile__(self): self.dict = {} # Ouvrir le fichier associe self.parser = _bibtex.open_file(Open.url_to_local(self.key), Config.get('bibtex/strict').data) # Incorporer les definitions de l'utilisateur if not Config.get('bibtex+/override').data: user = Config.get('bibtex/macros').data valid = re.compile('^\w+$') for k in user.keys(): if not valid.match(k): raise TypeError, _(u"key “%s” is malformed") % k _bibtex.set_string( self.parser, k, _bibtex.reverse(_base_fieldtype[Text], Config.get('bibtex+/braces').data, user[k][0])) errors = [] # Creer la base de cles iter = BibtexIterator(self, self.parser) try: entry = iter.first() if entry is not None: if entry.key is None: self.add(entry) else: if self.dict.has_key(entry.key): errors.append( _(u"%s:%d: key “%s” already defined") % (str(self.key), entry.line, entry.key.key)) else: self.dict[entry.key] = entry except Exceptions.ParserError, err: errors.append(str(err))
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 __parsefile__(self): self.dict = {} # Ouvrir le fichier associe self.parser = _bibtex.open_file(Open.url_to_local(self.key), Config.get ('bibtex/strict').data) # Incorporer les definitions de l'utilisateur if not Config.get('bibtex+/override').data: user = Config.get('bibtex/macros').data valid = re.compile('^\w+$') for k in user.keys(): if not valid.match(k): raise TypeError, _("key `%s' is malformed") % k _bibtex.set_string(self.parser, k, _bibtex.reverse(_base_fieldtype[Text], Config.get('bibtex+/braces').data, user [k][0])) finished = 0 errors = [] # Creer la base de cles iter = BibtexIterator(self, self.parser) try: entry = iter.first() if entry is not None: if entry.key is None: self.add(entry) else: if self.dict.has_key(entry.key): errors.append(_("%s:%d: key `%s' already defined") % ( str(self.key), entry.line, entry.key.key)) else: self.dict [entry.key] = entry except Exceptions.ParserError, err: errors.append (str (err))
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 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 iterator (url, check): ''' This methods returns an iterator that will parse the database on the fly (useful for merging or to parse broken databases ''' if check and url.url [2] [-4:] != '.bib': return None # Ouvrir le fichier associe parser = _bibtex.open_file (Open.url_to_local (url), Config.get ('bibtex/strict').data) # create a database to generate correct keys db = Base.DataBase (url) return BibtexIterator (db, parser)
def iterator(url, check): ''' This methods returns an iterator that will parse the database on the fly (useful for merging or to parse broken databases ''' if check and url.url[2][-4:] != '.bib': return None # Ouvrir le fichier associe parser = _bibtex.open_file(Open.url_to_local(url), Config.get('bibtex/strict').data) # create a database to generate correct keys db = Base.DataBase(url) return BibtexIterator(db, parser)
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 checkunfiltered(filename, strict=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)) file = _bibtex.open_file(filename, strict) result = open(filename + '-ok', 'r') line = 1 failures = 0 checks = 0 while 1: try: entry = _bibtex.next_unfiltered(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 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 checkunfiltered(filename, strict=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)) file = _bibtex.open_file(filename, strict) result = open(filename + "-ok", "r") line = 1 failures = 0 checks = 0 while 1: try: entry = _bibtex.next_unfiltered(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 __init__(self, path): self.path = path self.fhandle = _bibtex.open_file(path, 100)
pdf_directory = sys.argv[2] bibtex_file = sys.argv[1] tex_file = None if len(sys.argv) == 4: tex_file = sys.argv[3] if not os.path.exists(pdf_directory): usage_and_exit("The PDF directory '"+pdf_directory+"' doesn't exist.") if not os.path.isdir(pdf_directory): usage_and_exit("'"+pdf_directory+"' is not a directory.") if tex_file and not os.path.exists(tex_file): usage_and_exit("The TeX file '"+tex_file+"' does not exist.") b = _bibtex.open_file(bibtex_file,1) # The second parameter means "strict" class Paper: def __init__(self,key,title,year): self.key = key self.title = title self.year = year def __eq__(self,other): return self.key == other.key def __cmp__(self,other): return cmp(self.key,other.key) def __hash__(self): return self.key.__hash__() def pdf_filename(self): return os.path.join(pdf_directory,self.key+".pdf") def text_filename(self):
def read_file(path): """Takes a file path and returns a list containing objects of type BibtexRecord""" # Mystery arguments: strictness = False # Read the string: return _iterate_bibtexsource(_bibtex.open_file(path, strictness))
pdf_directory = sys.argv[2] bibtex_file = sys.argv[1] tex_file = None if len(sys.argv) == 4: tex_file = sys.argv[3] if not os.path.exists(pdf_directory): usage_and_exit("The PDF directory '" + pdf_directory + "' doesn't exist.") if not os.path.isdir(pdf_directory): usage_and_exit("'" + pdf_directory + "' is not a directory.") if tex_file and not os.path.exists(tex_file): usage_and_exit("The TeX file '" + tex_file + "' does not exist.") b = _bibtex.open_file(bibtex_file, 1) # The second parameter means "strict" class Paper: def __init__(self, key, title, year): self.key = key self.title = title self.year = year def __eq__(self, other): return self.key == other.key def __cmp__(self, other): return cmp(self.key, other.key) def __hash__(self):
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 return failures, checks failures = 0 checks = 0 parser = _bibtex.open_file('/dev/null', True) def convert(text, t): field = _bibtex.reverse(t, True, text) return _bibtex.get_latex(parser, field, t) text = 'éssai A {} toto~tutu' for t, r in ((0, r'\'essai A \{\} toto~tutu'), (2, r'\'essai {A} \{\} toto~tutu'), (4, text)): checks += 1 o = convert(text, t) if o != r: print "type %d convert: got %r instead of %r" % (t, o, r) failures += 1 for file in ('tests/preamble.bib', 'tests/string.bib',
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 return failures, checks failures = 0 checks = 0 parser = _bibtex.open_file("/dev/null", True) def convert(text, t): field = _bibtex.reverse(t, True, text) return _bibtex.get_latex(parser, field, t) text = "éssai A {} toto~tutu" for t, r in ((0, r"\'essai A \{\} toto~tutu"), (2, r"\'essai {A} \{\} toto~tutu"), (4, text)): checks += 1 o = convert(text, t) if o != r: print "type %d convert: got %r instead of %r" % (t, o, r) failures += 1 for file in ("tests/preamble.bib", "tests/string.bib", "tests/simple-2.bib"): f, c = checkunfiltered(file)
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 ) )