def _populate_from_src(self, src): bib_data = None is_url = False if (re.match('^[A-Za-z0-9+_-]+://.*', src)): is_url = True if (not is_url): src = self.resolveSourcePath(src) # read data, decode it in the right charset data = None if is_url: logger.debug("Opening URL %r", src) try: f = urllib.urlopen(src) if (f is None): return None data = butils.guess_encoding_decode(f.read()) logger.longdebug(" ... successfully read %d chars from URL resouce." % len(data)) f.close() except IOError: # ignore source, will have to try next in list return None else: logger.debug("Opening file %r", src) try: with open(src, 'r') as f: data = butils.guess_encoding_decode(f.read()) except IOError: # ignore source, will have to try next in list return None logger.info("Found Source: %s" %(src)) try: # parse bibtex parser = inputbibtex.Parser() bib_data = None with io.StringIO(data) as stream: try: bib_data = parser.parse_stream(stream) except Exception as e: # We don't skip to next source, because we've encountered an error in the # BibTeX data itself: the file itself was properly found. So raise an error. raise BibolamaziBibtexSourceError(unicode(e), fname=src) if (self._bibliographydata is None): # initialize bibliography data self._bibliographydata = pybtex.database.BibliographyData() for key, entry in bib_data.entries.iteritems(): if (key in self._bibliographydata.entries): logger.warn('Repeated bibliography entry in other file: %s. Keeping first encountered entry.', key) continue # TODO: Do this cleverly? # #if (key in self._bibliographydata.entries): # oldkey = key # duplcounter = 1 # while key in self._bibliographydata.entries: # key = _rx_repl_key_duplsuffix.sub(_key_duplsuffix+str(duplcounter), key) # duplcounter += 1 # # logger.warn('Repeated bibliography entry in other file: %s. '+ # 'Renamed duplicate occurence to %s.', oldkey, key) self._bibliographydata.add_entry(key, entry) except pybtex.database.BibliographyDataError as e: # We don't skip to next source, because we've encountered an error in the # BibTeX data itself: the file itself was properly found. So raise an error. raise BibolamaziBibtexSourceError(unicode(e), fname=src) return True
is_url = False if (re.match(ur'^[A-Za-z0-9+_-]+://.*', src)): is_url = True if (not is_url): src = self.resolveSourcePath(src) # read data, decode it in the right charset data = None if is_url: logger.debug("Opening URL %r", src) try: f = urllib.urlopen(src) if (f is None): return None data = butils.guess_encoding_decode(f.read()) logger.longdebug(" ... successfully read %d chars from URL resouce." % len(data)) f.close() except IOError: # ignore source, will have to try next in list return None else: logger.debug("Opening file %r", src) try: with open(src, 'r') as f: data = butils.guess_encoding_decode(f.read()) except IOError: # ignore source, will have to try next in list return None logger.info("Found Source: %s" %(src))