python-bibtexparser
python-bibtexparser copied to clipboard
Adding bibtex entry to BibDatabase
Hello,
I would like to load 3 bib files: all.bib, topic1.bib, topic2.bib. Based on certain criteria (set by Jabref in the bib file) I would like to add an entry from all.bib to either topic1.bib or topic2.bib. I would like to create script that does this automatically.
I was thinking that this would be like to searching in the dictionary that holds the database and if its already there skip it. If not check for the condition and add the entry to the appropriate bibfile. I don't know whether this functionality is already present or not. Its not there in BibDatabase, also the entries is a list, so can't be searched easily and the entry_dict gets only built when called and does not update.
There is an _add_entry in bparser but I'm not sure how that coordinates with BibDatabase. Furthermore, I would like to some handy methods to get the size of the BibDatabase maybe by adding a len method. I am willing to work on this. I just wanted to check if what I want to do is already possible. If not, I can add this functionality with some help.
Thanks.
Hi @sudarshan85. The short answer is that you can do everything by manipulating the BibDatabase entries attribute that is meant to be public. You should not call the _add_entry from the parser, this is a private method specific for the parser.
More precisely, if b is your BibDatabase object you can:
– look for an entry in: b.get_entry_dict()['your-citekey'],
– get the number of entries in: len(b.entries),
– add one as: b.entries.append(the_entry_from_the_other_bibdatabase).
When adding entries, you are however responsible for it to be well formatted (which is the case if you take it from another database).
Beyond that, one might consider improving the BibDatabase object for easier manipulation. It would be adding a bit of complication to the code, so I don't know how much this should be considered. Similarly it would probably make more sense to have entries be a dictionary but, once again, does it worth breaking the API?
Any thoughts? anybody?
my issue here -- https://github.com/sciunto-org/python-bibtexparser/issues/364 -- may be a dup of this...