<s> import os . path <EOL> import sys <EOL> from pybtex . database . input import bibtex <EOL> from citation_vim . item import Item <EOL> class bibtexParser ( object ) : <EOL> def load ( self , source_field , file_path ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> try : <EOL> bib_path = self . _check_path ( file_path ) <EOL> bib_data = self . _read_file ( bib_path ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" . format ( file_path ) ) <EOL> print ( "<STR_LIT>" . format ( str ( e ) ) ) <EOL> return [ ] <EOL> for key in bib_data . entries : <EOL> bib_entry = bib_data . entries [ key ] <EOL> if not source_field in [ '<STR_LIT>' , '<STR_LIT:key>' , '<STR_LIT>' ] and not source_field in bib_entry . fields : <EOL> continue <EOL> if source_field == '<STR_LIT>' : <EOL> try : <EOL> bib_entry . persons [ u'<STR_LIT>' ] <EOL> except : <EOL> continue <EOL> item = Item ( ) <EOL> item . abstract = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . author = self . format_author ( bib_entry ) <EOL> item . date = self . get_field ( bib_entry , "<STR_LIT>" ) + self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . doi = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . file = self . format_file ( bib_entry ) <EOL> item . isbn = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . journal = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . key = key <EOL> item . language = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . issue = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . notes = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . pages = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . publisher = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . tags = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . title = self . get_field ( bib_entry , "<STR_LIT:title>" ) <EOL> item . type = bib_entry . type <EOL> item . url = self . get_field ( bib_entry , "<STR_LIT:url>" ) <EOL> item . volume = self . get_field ( bib_entry , "<STR_LIT>" ) <EOL> item . combine ( ) <EOL> if not getattr ( item , source_field ) == "<STR_LIT>" : <EOL> items . append ( item ) <EOL> return items <EOL> def _read_file ( self , filename ) : <EOL> parser = bibtex . Parser ( ) <EOL> return parser . parse_file ( filename ) <EOL> def _check_path ( self , path ) : <EOL> path = os . path . abspath ( os . path . expanduser ( path ) ) <EOL> if not os . path . exists ( path ) : <EOL> raise RuntimeError ( "<STR_LIT>" % path ) <EOL> return path <EOL> def strip_chars ( self , string ) : <EOL> return string . replace ( "<STR_LIT:{>" , "<STR_LIT>" ) . replace ( "<STR_LIT:}>" , "<STR_LIT>" ) <EOL> def get_field ( self , entry , field ) : <EOL> output = entry . fields [ field ] if field in entry . fields else "<STR_LIT>" <EOL> output = self . strip_chars ( output ) <EOL> return output <EOL> def format_author ( self , entry ) : <EOL> try : <EOL> persons = entry . persons [ u"<STR_LIT>" ] <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> authors = [ unicode ( au ) for au in persons ] <EOL> elif sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> authors = [ str ( au ) for au in persons ] <EOL> except KeyError : <EOL> authors = [ "<STR_LIT>" ] <EOL> authors = self . strip_chars ( "<STR_LIT>" . join ( authors ) ) <EOL> return authors <EOL> def format_file ( self , entry ) : <EOL> output = "<STR_LIT>" <EOL> if u"<STR_LIT:file>" in entry . fields : <EOL> output = entry . fields [ u"<STR_LIT:file>" ] . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] <EOL> return output <EOL> def format_tags ( entry ) : <EOL> output = "<STR_LIT>" <EOL> if u"<STR_LIT>" in entry . fields : <EOL> output = "<STR_LIT:U+002CU+0020>" . join ( entry . fields [ u"<STR_LIT>" ] ) <EOL> return output </s>
