def test_comment_write(self):
        with open('bibtexparser/tests/data/comments_only.bib') as bibfile:
            bib = BibTexParser(bibfile.read())

        with open('bibtexparser/tests/data/comments_only_output.bib') as bibfile:
            expected = bibfile.read()
        result = to_bibtex(bib)
        self.assertEqual(result, expected)
Exemple #2
0
 def load_and_replace(bibtex_file):
     with open(os.path.join('publications', bibtex_file), 'r') as f:
         p = BibTexParser(f.read(), bc.author).get_entry_list()
     for pub in p:
         for field in pub:
             pub[field] = context.make_replacements(pub[field])
         pub['author'] = _format_author_list(pub['author'])
     return p
Exemple #3
0
 def test_43(self):
     comment = "@STRING{foo = \"bar\"}\n" \
               "This is a comment\n" \
               "This is a second comment."
     expected = "This is a comment\nThis is a second comment."
     bib = BibTexParser(comment)
     self.assertEqual(bib.comments, [expected])
     self.assertEqual(bib.strings, {'foo': 'bar'})
Exemple #4
0
 def test_comment_list(self):
     with open('bibtexparser/tests/data/features.bib') as bibfile:
         bib = BibTexParser(bibfile.read())
     expected = [
         "ignore this line!", "ignore this line too!",
         "and ignore this line too!"
     ]
     self.assertEqual(bib.comments, expected)
Exemple #5
0
def bib_parse(path):

    with open(path) as bibtex_file:
        parser = BibTexParser()
        parser.customization = custom_callback
        bib_database = bibtexparser.load(bibtex_file, parser=parser)
        input_data = bib_database.entries
    return input_data
Exemple #6
0
def load_bibtex_string(string):
    string_parser = BibTexParser(common_strings=True,
                                 ignore_nonstandard_types=True)
    string_parser.customization = customizations

    bib_database = bibtexparser.loads(string, parser=string_parser)

    return bib_database
Exemple #7
0
def load_bibtex_file(filepath):
    parser = BibTexParser(common_strings=True, ignore_nonstandard_types=True)
    parser.customization = customizations

    with open(filepath, "r") as bibtex:
        bib_database = bibtexparser.load(bibtex, parser=parser)

    return bib_database
def load_bib(filename):
    with open(filename) as bibtex_file:
        parser = BibTexParser()
        parser.customization = convert_to_unicode
        bib_database = bibtexparser.loads(bibtex_file.read().replace(
            "{{", "{").replace("}}", "}"),
                                          parser=parser)
        return bib_database
Exemple #9
0
 def deserialize_publications(self, base_path):
     # Scientific publications
     sci_pubs_file = os.path.join(base_path, 'sci_publications.bib')
     if os.path.exists(sci_pubs_file):
         parser = BibTexParser()
         parser.customization = homogenize_latex_encoding
         with open(sci_pubs_file, encoding='utf-8') as bibtex_file:
             bib_database = bibtexparser.load(bibtex_file, parser=parser)
             self.scientificPubs = bib_database.entries
     # Popular publications
     pop_pubs_file = os.path.join(base_path, 'pop_publications.bib')
     if os.path.exists(pop_pubs_file):
         parser = BibTexParser()
         parser.customization = homogenize_latex_encoding
         with open(pop_pubs_file, encoding='utf-8') as bibtex_file:
             bib_database = bibtexparser.load(bibtex_file, parser=parser)
             self.popularPubs = bib_database.entries
Exemple #10
0
def printable_bibtex_entry(entry):
    # converts a dictionary BibTeX entry to LaTeX format

    entry_str = bibtex_entry_str(entry)
    parser = BibTexParser()
    parser.customization = homogeneize_latex_encoding
    bib_database = bibtexparser.loads(entry_str, parser = parser)
    return(bib_database.entries[0])
Exemple #11
0
 def test_wrong(self):
     with open('bibtexparser/tests/data/wrong.bib', 'r') as bibfile:
         bib = BibTexParser(bibfile.read())
         res = bib.get_entry_list()
         expected = [{'author': 'correct',
                      'ID': 'bar',
                      'ENTRYTYPE': 'article'}]
     self.assertEqual(res, expected)
Exemple #12
0
def updatestatistics():
    articlesparser = BibTexParser(common_strings=False)
    articlesparser.ignore_nonstandard_types = False
    with open('/home/limingtao/ircre-bibtex/ircreupdate/articles.bib', encoding='utf8') as articlesfile:
        articles_database = bibtexparser.load(articlesfile, articlesparser)

    articleentries = articles_database.entries
    totalcitations = 0
    totalif = 0.0
    citationlist = []
    jourallist = []
    hihonumber = 0
    totalpublications = len(articleentries) + 28
    totalarticles = len(articleentries)
    for i in range(len(articleentries)):

        if 'cited' in articleentries[i]:
            citednumber = int(articleentries[i]['cited'])
        else:
            citednumber = 0
        if 'impactfactor' in articleentries[i]:
            impactfactor = float(articleentries[i]['impactfactor'])
        else:
            impactfactor = 0.0

        if 'hihosubject' in articleentries[i]:
            hihonumber = hihonumber + 1

        citationlist.append(citednumber)
        jourallist.append(articleentries[i]['journal'])
        totalcitations = totalcitations + citednumber
        totalif = totalif + impactfactor
    hindex = Hindex(citationlist)
    i10index = I10index(citationlist)
    totalcitations = totalcitations + 19
    citationperpaper = totalcitations / len(articleentries)
    journalnumber = len(set(jourallist))
    averageif = totalif / len(articleentries)
    # print(totalcitations)
    # print(hindex)
    # print(i10index)
    # print(citationperpaper)
    # print(journalnumber)
    # print(averageif)
    # print(hihonumber)
    # print(totalpublications)

    with open('/home/limingtao/ircre-bibtex/ircreupdate/newstatistics.js', 'w', encoding='utf8') as statisticsjsfile:
        statisticsjsfile.write('totalpublications = "%d";\n' % totalpublications)
        statisticsjsfile.write('totalarticles = "%d";\n' % totalarticles)
        statisticsjsfile.write('totalcitations = "%d";\n' % totalcitations)
        statisticsjsfile.write('hindex = "%d";\n' % hindex)
        statisticsjsfile.write('i10index = "%d";\n' % i10index)
        statisticsjsfile.write('numberjournals = "%d";\n' % journalnumber)
        statisticsjsfile.write('numberesihighlycited = "%d";\n' % hihonumber)
        statisticsjsfile.write('citationperpaper = "%.2f";\n' % citationperpaper)
        statisticsjsfile.write('averageif = "%.3f";\n' % averageif)
    return 0
Exemple #13
0
def get_bibentries(bibfile):

    print("Getting bibentries...")
    with open(bibfile) as bibtexfile:
        parser = BibTexParser(common_strings=True)
        bibdatabase = bibtexparser.load(bibtex_file=bibtexfile, parser=parser)

    bibentries = pd.DataFrame(bibdatabase.entries)
    return bibentries
Exemple #14
0
    def test_book(self):
        with open('bibtexparser/tests/data/book.bib', 'r') as bibfile:
            bib = BibTexParser(bibfile.read())

        with open('bibtexparser/tests/data/book_output.bib', 'r') as bibfile:
            expected = bibfile.read()
        result = to_bibtex(bib)
        self.maxDiff = None
        self.assertEqual(expected, result)
Exemple #15
0
 def __init__(self, bibtex_filename):
     '''
     Constructor
     '''
     self.filename = bibtex_filename
     self.entry_list = []
     with open(self.filename) as bibfile:
         bp = BibTexParser(bibfile)
         self.entry_list = bp.get_entry_list()
Exemple #16
0
def load_bibtex(f):
    parser = BibTexParser()
    parser.alt_dict = {}

    with open(f, 'r') as fd:
        txt = re.sub('^\s*%.*$', '', fd.read(), flags=re.MULTILINE)
        bib = bibtexparser.loads(txt, parser=parser)

    return bib.entries
Exemple #17
0
    def database(self) -> BibDatabase:
        """Return the BibTex Python object representation of master file.

        """
        logger.info(f'parsing master bibtex file: {self.master_bib}')
        parser = BibTexParser()
        parser.ignore_nonstandard_types = False
        with open(self.master_bib) as f:
            return bibtexparser.load(f, parser)
    def load_records(self, bibtex_filename=None):
        """Load all bibtex items as valid records"""

        with open(bibtex_filename) as bibtex_file:
            # Parse BibTex file
            parser = BibTexParser()
            parser.customization = td_biblio_customization
            bp = bibtexparser.load(bibtex_file, parser=parser)
            self.records = [self.to_record(r) for r in bp.get_entry_list()]
Exemple #19
0
    def test_book(self):
        with io.open(_data_path('book.bib'), 'r') as bibfile:
            bib = BibTexParser(bibfile.read())

        with io.open(_data_path('book_output.bib'), 'r') as bibfile:
            expected = bibfile.read()
        result = to_bibtex(bib)
        self.maxDiff = None
        self.assertEqual(expected, result)
def get_bibtex_data(filename):
    parser = BibTexParser()
    parser.ignore_nonstandard_types = False
    with open(filename) as f:
        bib_database = bibtexparser.loads(f.read(), parser)
    sources_dict_lst = []
    for entry in bib_database.entries:
        sources_dict_lst.append(entry)
    return sources_dict_lst
Exemple #21
0
def load_bibtex_file(filepath):
    """Parse BibTeX file and return entry list"""
    with open(filepath, 'rU') as bibfile:
        bp = BibTexParser(bibfile)
        entries = bp.get_entry_list()

    entries = list(map(_capitalize_entry_title, entries))
    entries = list(map(_format_entry_authors, entries))
    return entries
Exemple #22
0
 def test_43_bis(self):
     comment = "@STRING{foo = \"bar\"}\n" \
               "This is a comment\n" \
               "STRING{Baz = \"This should be interpreted as comment.\"}"
     expected = "This is a comment\n" \
                "STRING{Baz = \"This should be interpreted as comment.\"}"
     bib = BibTexParser(comment)
     self.assertEqual(bib.comments, [expected])
     self.assertEqual(bib.strings, {'foo': 'bar'})
Exemple #23
0
def read_bibtex(bibtex_str):
    parser = BibTexParser(common_strings=True)
    parser.ignore_nonstandard_types = False
    parser.homogenize_fields = True
    bib_database = parser.parse(bibtex_str)
    keyworded = map(bibtexparser.customization.keyword, bib_database.entries)
    converted = list(map(bibtexparser.customization.convert_to_unicode, keyworded))
    authored = map(bibtexparser.customization.author, converted)
    return list(authored)
Exemple #24
0
def generateFromBib(bibfile):
    with open(bibfile) as bibtex_file:
        bibtex_str = bibtex_file.read()
        parser = BibTexParser()
        parser.customization = customizations
        bib_database = bibtexparser.loads(bibtex_str, parser=parser)
        for entry in bib_database.entries:
            if entry["ID"] in pubList:
                prettyPrintEntry(entry)
Exemple #25
0
def parse_bibtex(bibtex):
    parser = BibTexParser()
    parser.customization = convert_to_unicode
    try:
        bib_database = bibtexparser.loads(bibtex, parser=parser)
    except Exception as e:
        return bibtex

    entries = decorate_entries(bib_database.entries)
    return entries
Exemple #26
0
 def __init__(self, bib_file_location=None):
     if bib_file_location is None:
         bib_file_location = gflags.FLAGS.bib_file
     bib_file_location = re.sub('~', os.environ['HOME'], bib_file_location)
     with open(bib_file_location) as bibfile:
         content = bibfile.read()
         bp = BibTexParser(content)
     self.entry_map = {}
     for ent in bp.get_entry_list():
         self.entry_map[ent['id']] = ent
Exemple #27
0
def parse_bibtex(reference, bibtex_parser=None):
    if bibtex_parser is None:
        bibtex_parser = BibTexParser()
    try:
        result = bibtex_parser.parse(reference).get_entry_list()[-1]
    except IndexError:
        #unable to parse
        result = None

    return result
Exemple #28
0
 def test_multiple_entries(self):
     with open('bibtexparser/tests/data/multiple_entries_and_comments.bib'
               ) as bibfile:
         bib = BibTexParser(bibfile.read())
     with open(
             'bibtexparser/tests/data/multiple_entries_and_comments_output.bib'
     ) as bibfile:
         expected = bibfile.read()
     result = to_bibtex(bib)
     self.assertEqual(result, expected)
Exemple #29
0
 def bibtex(self):
     """Return entry formatted in BibTeX style as dictionary or 'None'."""
     if self.entry:
         entry_str = _entry_to_str(self.entry)
         parser = BibTexParser()
         parser.customization = homogenize_latex_encoding
         bib_database = bibtexparser.loads(entry_str, parser = parser)
         return bib_database.entries[0]
     else:
         return None
Exemple #30
0
def load_bibtex(filename):

    if not (os.path.isfile(filename)):
        print("input file not found" + filename, file=sys.stderr)
        exit(0)

    parser = BibTexParser(common_strings=True)
    with open(filename, encoding='utf-8') as bibtex_file:
        database = bibtexparser.load(bibtex_file, parser)
    return database