Storage module

Note

To access the classes in this module, you need to extend the lib by importing biblib.storage.

This module contains basic reader and writer classes, with the interfaces implemented as abstract base classes.

They will handle ids (cite keys), entries and dictonaries ({‘cite key’: entry}) as input and will return an entry or a dictonary ({‘cite key’: entry}).

The following classes are provided:

>>> from biblib.storage import FileStorage, DoiReadStorage
>>> # init storages
>>> doiStorage = DoiReadStorage()
>>> storage = FileStorage('/path/to/file.bib')
>>> # read entries from file
>>> entries = storage.readEntries()
{'CiteKey': <biblib._entry.Entry object at 0x808184390>}
>>> # fetch entry from doi and add it to the file storage
>>> entry = doiStorage.readEntry('10.1103/physrevlett.108.105901')
>>> storage.createEntry(entry, 'OtherCiteKey')
>>> entries = storage.readEntries()
{'CiteKey': <biblib._entry.Entry object at 0x808184390>, 'OtherCiteKey': <biblib._entry.Entry object at 0x8081eef90>}

Abstract Base Classes

class biblib.storage.ReadStorage(src)[source]

AbstractBaseClass ReadStorage

Parameters:src (str) – data source to act on
readEntry(id)[source]

Return Entry for given id. (abstract method, must be implemented in derived classes)

Parameters:id (str) – ID of Entry
Returns:Entry object for the given id
Return type:Entry or None
readEntries(ids=None)[source]

Return list of Entries for given ids. Returns all stored Entries if list ist empty.

Parameters:ids (list) – IDs of Entries
Returns:Entry objects for given ids
Return type:dict of Entries
class biblib.storage.WriteStorage(src)[source]

AbstractBaseClass WriteStorage

Parameters:src (str) – data source to act on
createEntry(entry, id=None)[source]

Add Entry to storage source

Parameters:
  • entry (Entry) – Entry to store
  • id (str) – id to store entry with
updateEntry(entry, id=None)[source]

update Entry in storage source

Parameters:
  • entry (Entry) – Entry to store
  • id (str) – id to store entry with
deleteEntry(entry, id=None)[source]

delete Entry from storage source

Parameters:
  • entry (Entry) – Entry to store
  • id (str or None) – id to store entry with
writeEntries(entries=None)[source]

Overwrite storage with given Entries. Existing entries will be purged.

Parameters:entries (list of Entries) – list of Entry objects

String Storage

class biblib.storage.StringReadStorage(bibStr)[source]

Bases: biblib.storage.ReadStorage

ReadStorage that stores the data in a string.

Parameters:bibStr (str) – BibTex string
classmethod entriesFromString(bibStr)[source]

Transform bibtex string into a dictonary of Entries.

Parameters:bibStr (unicode) – bibtex string
Returns:dictonary of Entries
Return type:dict of Entries
classmethod entryFromString(bibStr)[source]

Transform bibtex string into an Entry. If string contains more than one Entry the first one will be returned.

Parameters:bibStr (unicode) – bibtex string
Returns:Entry
Return type:Entry or None
str()[source]
class biblib.storage.StringStorage(src, encoding='ascii')[source]

Bases: biblib.storage.StringReadStorage, biblib.storage.WriteStorage

ReadWriteStorage that stores the data in a string.

Parameters:
  • bibStr (str) – BibTex string
  • encoding (str) – string encoding
classmethod entriesToString(entries, encoding='ascii')[source]
classmethod entryToString(entry, citeKey=None, encoding='ascii')[source]

Returns the BibTeX code of an Entry objects as a string. If citeKey is not defined, the object attribute Entry.citeKey will be used.

Parameters:
  • entryObj (Article | Book | …) – BibTeX Entry
  • citeKey (str) – citation-key
  • encoding (str) – string encoding
Returns:

BibTeX string

Return type:

str

File Storage

class biblib.storage.FileReadStorage(file)[source]

Bases: biblib.storage.StringReadStorage

ReadStorage that reads the data from a file.

Parameters:file (str) – path to file
class biblib.storage.FileStorage(file, encoding='ascii')[source]

Bases: biblib.storage.FileReadStorage, biblib.storage.StringStorage

ReadWriteStorage that stores the data in a file.

Parameters:
  • file (str) – path to file
  • encoding (str) – string encoding

Doi Storage

class biblib.storage.DoiReadStorage[source]

Bases: biblib.storage.ReadStorage

ReadStorage that reads the data from the doi database.

ISBN Storage

class biblib.storage.IsbnReadStorage[source]

Bases: biblib.storage.ReadStorage

ReadStorage that reads the data for a given ISBN.

Sqlite3 Storage

class biblib.storage.SqliteStorage(src)[source]

Bases: biblib.storage.ReadStorage, biblib.storage.WriteStorage

ReadWriteStorage that stores the data in SQLite3 database

Parameters:file (str) – path to db file