<s> """<STR_LIT>""" <EOL> __docformat__ = "<STR_LIT>" <EOL> __authors__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> __version__ = '<STR_LIT>' <EOL> __needs__ = '<STR_LIT>' <EOL> __strict__ = False <EOL> import re <EOL> import sys <EOL> from simpleparse . dispatchprocessor import dispatch , DispatchProcessor , getString , lines <EOL> from bibstuff import bibgrammar <EOL> from bibstuff . bibstyles . shared import reformat_para <EOL> import logging <EOL> bibfile_logger = logging . getLogger ( '<STR_LIT>' ) <EOL> months_en = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> monthslower_en = [ m . lower ( ) for m in months_en ] <EOL> monthmacros_en = [ m [ : <NUM_LIT:3> ] for m in monthslower_en ] <EOL> MONTH_DICT = dict ( zip ( monthmacros_en , months_en ) ) <EOL> class BibEntry ( dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> dict . __init__ ( self , * args , ** kwargs ) <EOL> self . _fields = [ ] <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> stringrep = '<STR_LIT>' % ( self . entry_type . upper ( ) , self . citekey ) <EOL> try : <EOL> mlen = max ( len ( key_str ) for key_str in self . _fields ) <EOL> except ValueError : <EOL> mlen = <NUM_LIT:0> <EOL> bibfile_logger . warn ( "<STR_LIT>" ) <EOL> field_list = [ ] <EOL> for key in self . _fields : <EOL> addbraces = True <EOL> addquotes = False <EOL> spacer = '<STR_LIT:U+0020>' * ( mlen - len ( key ) ) <EOL> val = self [ key ] <EOL> if key == '<STR_LIT>' : <EOL> try : val = val [ '<STR_LIT>' ] <EOL> except TypeError : pass <EOL> elif key == '<STR_LIT>' : <EOL> if val . isalpha ( ) and val . islower ( ) : <EOL> addbraces = False <EOL> elif key == '<STR_LIT>' : <EOL> if val . lower ( ) in monthslower_en + monthmacros_en : <EOL> val = val [ : <NUM_LIT:3> ] . lower ( ) <EOL> addbraces = False <EOL> elif key in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> try : <EOL> addbraces = not int ( val ) <EOL> except : <EOL> pass <EOL> if '<STR_LIT:@>' in val : <EOL> addquotes = True <EOL> if addquotes : <EOL> val = '<STR_LIT:">' + val + '<STR_LIT:">' <EOL> elif addbraces : <EOL> val = "<STR_LIT:{>" + val + "<STR_LIT:}>" <EOL> field_list . append ( "<STR_LIT>" % ( mlen , key , val ) ) <EOL> stringrep += "<STR_LIT>" . join ( field_list ) <EOL> stringrep += '<STR_LIT>' <EOL> return stringrep <EOL> def __setitem__ ( self , key , val ) : <EOL> key = key . lower ( ) <EOL> dict . __setitem__ ( self , key , val ) <EOL> if key == "<STR_LIT:key>" : <EOL> bibfile_logger . info ( <EOL> "<STR_LIT>" ) <EOL> if key not in self . _fields and key not in [ "<STR_LIT>" , "<STR_LIT>" ] and val : <EOL> self . _fields . append ( key ) <EOL> def __getitem__ ( self , field ) : <EOL> field = field . lower ( ) <EOL> if field == "<STR_LIT:key>" : <EOL> bibfile_logger . info ( <EOL> "<STR_LIT>" ) <EOL> try : <EOL> result = dict . __getitem__ ( self , field ) <EOL> except KeyError : <EOL> crossref = self . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if isinstance ( crossref , self . __class__ ) : <EOL> result = crossref [ field ] <EOL> else : <EOL> result = '<STR_LIT>' <EOL> if field == '<STR_LIT>' and result in monthmacros_en : <EOL> result = MONTH_DICT [ result ] <EOL> return result <EOL> def __delitem__ ( self , key ) : <EOL> key = key . lower ( ) <EOL> try : <EOL> dict . __delitem__ ( self , key ) <EOL> except KeyError : <EOL> pass <EOL> try : <EOL> self . _fields . remove ( key ) <EOL> except ValueError : <EOL> pass <EOL> def set_entry_type ( self , val ) : <EOL> self [ "<STR_LIT>" ] = val . lower ( ) <EOL> def get_entry_type ( self ) : <EOL> return self [ "<STR_LIT>" ] <EOL> entry_type = property ( get_entry_type , set_entry_type , None , "<STR_LIT>" ) <EOL> def set_citekey ( self , val ) : <EOL> self [ "<STR_LIT>" ] = val <EOL> def get_citekey ( self ) : <EOL> return self [ "<STR_LIT>" ] <EOL> citekey = property ( get_citekey , set_citekey , None , "<STR_LIT>" ) <EOL> def get_fields ( self ) : <EOL> return self . _fields <EOL> def set_fields ( self , lst ) : <EOL> self . _fields = lst <EOL> fields = property ( get_fields , set_fields , None , "<STR_LIT>" ) <EOL> def search_fields ( self , string_or_compiled , field = '<STR_LIT>' , ignore_case = True ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( string_or_compiled , str ) : <EOL> if ignore_case : <EOL> reo = re . compile ( string_or_compiled , re . MULTILINE | re . IGNORECASE ) <EOL> else : <EOL> reo = re . compile ( string_or_compiled , re . MULTILINE ) <EOL> else : <EOL> reo = string_or_compiled <EOL> if not field : <EOL> for f in self . get_fields ( ) : <EOL> found = reo . search ( self [ f ] ) <EOL> if found : break <EOL> elif self [ field ] : <EOL> found = reo . search ( self [ field ] ) <EOL> else : <EOL> if field in self : <EOL> bibfile_logger . info ( "<STR_LIT>" % ( self , field ) ) <EOL> found = None <EOL> return found <EOL> def format_names ( self , names_formatter ) : <EOL> """<STR_LIT>""" <EOL> bibfile_logger . debug ( "<STR_LIT>" + str ( names_formatter ) ) <EOL> names = self . get_names ( ) <EOL> if isinstance ( names , str ) : <EOL> result = names <EOL> else : <EOL> result = names . format ( names_formatter ) <EOL> bibfile_logger . debug ( "<STR_LIT>" + str ( result ) ) <EOL> return result <EOL> def get_names ( self , entry_formatter = None , try_fields = None ) : <EOL> """<STR_LIT>""" <EOL> if entry_formatter is None : <EOL> if not try_fields : <EOL> try_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> return self . make_names ( entry_formatter , try_fields = try_fields ) <EOL> def make_names ( self , entry_formatter = None , try_fields = None ) : <EOL> """<STR_LIT>""" <EOL> from bibstuff import bibname <EOL> if entry_formatter is None : <EOL> for field in try_fields : <EOL> raw_names = self [ field ] <EOL> if raw_names : <EOL> break <EOL> else : <EOL> raw_names , field = entry_formatter . pick_raw_names ( self , try_fields ) <EOL> return bibname . BibName ( raw_names , from_field = field ) <EOL> def format_with ( self , entry_formatter ) : <EOL> bibfile_logger . debug ( "<STR_LIT>" + str ( entry_formatter ) ) <EOL> return entry_formatter . format_entry ( self ) <EOL> class BibFile ( DispatchProcessor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . entries = [ ] <EOL> self . _macroMap = { } <EOL> def get_entrylist ( self , citekeys , discard = True ) : <EOL> """<STR_LIT>""" <EOL> if not citekeys : <EOL> bibfile_logger . warning ( "<STR_LIT>" ) <EOL> return [ ] <EOL> temp = [ ( key , self . get_entry_by_citekey ( key ) ) for key in citekeys ] <EOL> bad_keys = [ pair [ <NUM_LIT:0> ] for pair in temp if not pair [ <NUM_LIT:1> ] ] <EOL> if bad_keys and discard : <EOL> bibfile_logger . warning ( "<STR_LIT>" + "<STR_LIT:\n>" . join ( bad_keys ) ) <EOL> if discard : <EOL> result = [ pair [ <NUM_LIT:1> ] for pair in temp if pair [ <NUM_LIT:1> ] ] <EOL> else : <EOL> result = [ pair [ <NUM_LIT:1> ] for pair in temp ] <EOL> for entry in result : <EOL> if entry : <EOL> crossref = entry . get ( '<STR_LIT>' , None ) <EOL> if isinstance ( crossref , str ) : <EOL> crossref = self . get_entry_by_citekey ( crossref ) <EOL> if crossref : <EOL> entry [ '<STR_LIT>' ] = crossref <EOL> return result <EOL> def get_entry_by_citekey ( self , citekey ) : <EOL> """<STR_LIT>""" <EOL> for entry in self . entries : <EOL> if entry . citekey == citekey : <EOL> return entry <EOL> """<STR_LIT>""" <EOL> def string ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> return buffer [ start + <NUM_LIT:1> : stop - <NUM_LIT:1> ] <EOL> def number ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> return buffer [ start : stop ] <EOL> def entry_type ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> return getString ( ( tag , start , stop , subtags ) , buffer ) <EOL> def citekey ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> return getString ( ( tag , start , stop , subtags ) , buffer ) <EOL> def name ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> return self . _macroMap . get ( buffer [ start : stop ] , buffer [ start : stop ] ) <EOL> def field ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> str = '<STR_LIT>' <EOL> for t in subtags [ <NUM_LIT:1> ] [ <NUM_LIT:3> ] : <EOL> if ( t ) : <EOL> str += dispatch ( self , t , buffer ) <EOL> return ( dispatch ( self , subtags [ <NUM_LIT:0> ] , buffer ) , str ) <EOL> def entry ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> entry = BibEntry ( ) <EOL> entry . entry_type = dispatch ( self , subtags [ <NUM_LIT:0> ] , buffer ) <EOL> entry . citekey = dispatch ( self , subtags [ <NUM_LIT:1> ] , buffer ) <EOL> for field in subtags [ <NUM_LIT:2> ] [ <NUM_LIT:3> ] : <EOL> k , v = dispatch ( self , field , buffer ) <EOL> entry [ k ] = v <EOL> self . entries . append ( entry ) <EOL> def macro ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> name , str = dispatch ( self , subtags [ <NUM_LIT:0> ] , buffer ) <EOL> """<STR_LIT>""" <EOL> self . _macroMap [ name ] = str <EOL> def preamble ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> the_type = getString ( subtags [ <NUM_LIT:0> ] , buffer ) <EOL> lineno = lines ( <NUM_LIT:0> , start , buffer ) + <NUM_LIT:1> <EOL> if the_type . upper ( ) != '<STR_LIT>' : <EOL> bibfile_logger . warning ( "<STR_LIT>" % ( lineno , the_type ) ) <EOL> else : <EOL> bibfile_logger . warning ( "<STR_LIT>" % lineno + "<STR_LIT:\n>" + buffer [ start : stop ] ) <EOL> def comment_entry ( self , ( tag , start , stop , subtags ) , buffer ) : <EOL> """<STR_LIT>""" <EOL> the_type = getString ( subtags [ <NUM_LIT:0> ] , buffer ) <EOL> lineno = lines ( <NUM_LIT:0> , start , buffer ) + <NUM_LIT:1> <EOL> if the_type . upper ( ) != '<STR_LIT>' : <EOL> bibfile_logger . warning ( "<STR_LIT>" % ( lineno , the_type ) ) <EOL> else : <EOL> bibfile_logger . info ( "<STR_LIT>" % lineno + "<STR_LIT:U+0020>" + getString ( subtags [ <NUM_LIT:1> ] , buffer ) ) <EOL> def search_entries ( self , string_or_compiled , field = '<STR_LIT>' , ignore_case = True ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( string_or_compiled , str ) : <EOL> if ignore_case : <EOL> reo = re . compile ( string_or_compiled , re . MULTILINE | re . IGNORECASE ) <EOL> else : <EOL> reo = re . compile ( string_or_compiled , re . MULTILINE ) <EOL> else : <EOL> reo = string_or_compiled <EOL> """<STR_LIT>""" <EOL> ls = [ entry for entry in self . entries <EOL> if entry . search_fields ( string_or_compiled = reo , field = field , ignore_case = ignore_case ) ] <EOL> return ls <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> src = open ( sys . argv [ <NUM_LIT:1> ] ) . read ( ) <EOL> bfile = BibFile ( ) <EOL> bibgrammar . Parse ( src , bfile ) <EOL> for entry in bfile . entries : <EOL> print entry <EOL> else : <EOL> print "<STR_LIT>" </s>
