<s> from StringIO import StringIO <EOL> import json , re <EOL> from pybtex . database . input import bibtex <EOL> from pybtex . errors import enable_strict_mode , format_error <EOL> from pybtex . scanner import PybtexSyntaxError , PybtexError <EOL> from totalimpact . providers import provider <EOL> from totalimpact . providers . provider import Provider , ProviderContentMalformedError , ProviderTimeout , ProviderServerError <EOL> import unicode_helpers <EOL> from totalimpact . providers import bibtex_lookup <EOL> import logging <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def build_bibtex_to_unicode ( unicode_to_bibtex ) : <EOL> bibtex_to_unicode = { } <EOL> for unicode_value in unicode_to_bibtex : <EOL> bibtex = unicode_to_bibtex [ unicode_value ] <EOL> bibtex = unicode ( bibtex , "<STR_LIT:utf-8>" ) <EOL> bibtex = bibtex . strip ( ) <EOL> bibtex = bibtex . replace ( "<STR_LIT:\\>" , "<STR_LIT>" ) <EOL> bibtex = bibtex . replace ( "<STR_LIT:{>" , "<STR_LIT>" ) <EOL> bibtex = bibtex . replace ( "<STR_LIT:}>" , "<STR_LIT>" ) <EOL> bibtex = "<STR_LIT:{>" + bibtex + "<STR_LIT:}>" <EOL> bibtex_to_unicode [ bibtex ] = unicode_value <EOL> return bibtex_to_unicode <EOL> class Bibtex ( Provider ) : <EOL> example_id = None <EOL> url = "<STR_LIT>" <EOL> descr = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> super ( Bibtex , self ) . __init__ ( ) <EOL> enable_strict_mode ( True ) <EOL> self . bibtex_to_unicode = build_bibtex_to_unicode ( bibtex_lookup . unicode_to_latex ) <EOL> def _to_unicode ( self , text ) : <EOL> text = unicode_helpers . to_unicode_or_bust ( text ) <EOL> if "<STR_LIT:{>" in text : <EOL> text = text . replace ( "<STR_LIT:\\>" , "<STR_LIT>" ) <EOL> for i , j in self . bibtex_to_unicode . iteritems ( ) : <EOL> text = text . replace ( i , j ) <EOL> return text <EOL> def _parse_bibtex_entries ( self , entries ) : <EOL> biblio_list = [ ] <EOL> for entry in entries : <EOL> stream = StringIO ( entry ) <EOL> parser = bibtex . Parser ( ) <EOL> try : <EOL> biblio = parser . parse_stream ( stream ) <EOL> biblio_list += [ biblio ] <EOL> except ( PybtexSyntaxError , PybtexError ) , error : <EOL> error = error <EOL> logger . error ( format_error ( error , prefix = '<STR_LIT>' ) ) <EOL> return biblio_list <EOL> def parse ( self , bibtex_contents ) : <EOL> ret = [ ] <EOL> cleaned_string = bibtex_contents . replace ( "<STR_LIT>" , "<STR_LIT>" ) . replace ( "<STR_LIT:%>" , "<STR_LIT>" ) . strip ( ) <EOL> entries = [ "<STR_LIT:@>" + entry for entry in cleaned_string . split ( "<STR_LIT:@>" ) if entry ] <EOL> biblio_list = self . _parse_bibtex_entries ( entries ) <EOL> for biblio in biblio_list : <EOL> parsed = { } <EOL> try : <EOL> mykey = biblio . entries . keys ( ) [ <NUM_LIT:0> ] <EOL> except AttributeError : <EOL> logger . info ( u"<STR_LIT>" % ( self . provider_name , biblio ) ) <EOL> continue <EOL> try : <EOL> parsed [ "<STR_LIT>" ] = self . _to_unicode ( biblio . entries [ mykey ] . fields [ "<STR_LIT>" ] ) <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> lnames = [ person . get_part_as_text ( "<STR_LIT>" ) for person in biblio . entries [ mykey ] . persons [ "<STR_LIT>" ] ] <EOL> parsed [ "<STR_LIT>" ] = self . _to_unicode ( lnames [ <NUM_LIT:0> ] ) <EOL> except ( KeyError , AttributeError ) : <EOL> try : <EOL> parsed [ "<STR_LIT>" ] = self . _to_unicode ( biblio . entries [ mykey ] . fields [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . split ( "<STR_LIT:U+002C>" ) [ <NUM_LIT:0> ] ) <EOL> except ( KeyError , AttributeError ) : <EOL> pass <EOL> try : <EOL> lnames = [ person . get_part_as_text ( "<STR_LIT>" ) for person in biblio . entries [ mykey ] . persons [ "<STR_LIT>" ] ] <EOL> parsed [ "<STR_LIT>" ] = self . _to_unicode ( "<STR_LIT:U+002CU+0020>" . join ( lnames ) ) <EOL> except ( KeyError , AttributeError ) : <EOL> pass <EOL> try : <EOL> parsed [ "<STR_LIT>" ] = biblio . entries [ mykey ] . fields [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> parsed [ "<STR_LIT>" ] = biblio . entries [ mykey ] . fields [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> pages = biblio . entries [ mykey ] . fields [ "<STR_LIT>" ] <EOL> parsed [ "<STR_LIT>" ] = pages . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> year_string = biblio . entries [ mykey ] . fields [ "<STR_LIT>" ] . replace ( "<STR_LIT:{}>" , "<STR_LIT>" ) <EOL> parsed [ "<STR_LIT>" ] = re . sub ( "<STR_LIT>" , "<STR_LIT>" , year_string ) <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> parsed [ "<STR_LIT:title>" ] = self . _to_unicode ( biblio . entries [ mykey ] . fields [ "<STR_LIT:title>" ] ) <EOL> except KeyError : <EOL> pass <EOL> ret . append ( parsed ) <EOL> return ret <EOL> def member_items ( self , bibtex_contents , cache_enabled = True ) : <EOL> logger . debug ( u"<STR_LIT>" % ( self . provider_name ) ) <EOL> parsed_bibtex = self . parse ( bibtex_contents ) <EOL> aliases = [ ] <EOL> for entry in parsed_bibtex : <EOL> if ( "<STR_LIT>" in entry ) and "<STR_LIT>" in entry [ "<STR_LIT>" ] : <EOL> arxiv_id = entry [ "<STR_LIT>" ] . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> arxiv_id = arxiv_id . replace ( "<STR_LIT>" , "<STR_LIT>" ) . strip ( ) <EOL> aliases += [ ( "<STR_LIT>" , arxiv_id ) ] <EOL> else : <EOL> aliases += [ ( "<STR_LIT>" , entry ) ] <EOL> return ( aliases ) </s>
