bibpy package

bibpy: Bib(la)tex parser and tools.

bibpy.read_string(string, format='relaxed', postprocess=False, remove_braces=False, ignore_comments=True, split_names=False)[source]

Read a string containing references in a given format.

The function returns an Entries object containing parsed entries and comments.

Valid formats are ‘bibtex’, ‘biblatex’, ‘mixed’ or ‘relaxed’:
  • bibtex : Parse as bibtex, raise error on non-conformity
  • biblatex: Parse as biblatex, raise error on non-conformity
  • mixed : Parse as a mix of bibtex and biblatex, raise error on non-
    conformity
  • relaxed : Allow any type of entries or fields

The postprocess kwarg can either be a list of fields to convert (‘year’ to int for example) or a bool. If True, then all viable entry fields are converted to appropriate types. Not all fields can be converted, e.g. the ‘title’ field remains unchanged.

If remove_braces is True, remove braces from field values e.g. ‘A {and} B’ becomes ‘A and B’.

If ignore_comments is True, do not include non-entry comments (comment entries are still included).

If split_names is True, split names into four components: first, prefix, last and suffix. This is only done for fields that are selected for postprocessing.

bibpy.read_file(source, format='relaxed', encoding='utf-8', postprocess=False, remove_braces=False, ignore_comments=True, split_names=False)[source]

Read a file containing references in a given format.

The source kwarg can either be a file handle or a filename. Files are treated as utf-8 encoded by default. The function returns an Entries object containing parsed entries and comments.

Valid formats are ‘bibtex’, ‘biblatex’, ‘mixed’ or ‘relaxed’:
  • bibtex: Parse as bibtex, raise error on non-conformity
  • biblatex: Parse as biblatex, raise error on non-conformity
  • mixed: Parse as a mix of bibtex and biblatex, raise error on non-
    conformity
  • relaxed: Allow any type of entries or fields

The postprocess kwarg can either be a list of fields to convert (‘year’ to int for example) or a bool. If True, then all viable entry fields are converted to appropriate types. Not all fields can be converted, e.g. the ‘title’ field remains unchanged.

If remove_braces is True, remove braces from field values e.g. ‘A {and} B’ becomes ‘A and B’.

If ignore_comments is True, do not include non-entry comments (comment entries are still included).

If split_names is True, split names into four components: first, prefix, last and suffix. This is only done for fields that are selected for postprocessing.

bibpy.write_string(entries, **format_options)[source]

Write a list of entries as a string.

Accepts either a bibpy.Entries object or a list of bibpy.Entry objects. The list of formatting options are the same as those for Entry’s format.

bibpy.write_file(source, entries, encoding='utf-8', **format_options)[source]

Write a list of entries to a file given by a filename or file descriptor.

The encoding refers to the file’s encoding and defaults to utf-8.

The list of formatting options are the same as those for Entry’s format.

bibpy.string_is_format(string, format)[source]

Check whether the string conforms to the given reference format.

bibpy.file_is_format(file, format)[source]

Check whether the file conforms to the given reference format.

bibpy.expand_strings(entries, strings, ignore_duplicates=False)[source]

Expand all string variables found in all entries.

The operation is done in-place. If multiple string variables have the same name, only one of them is arbitrarily used unless ignore_duplicates is True in which case an exception is thrown.

bibpy.unexpand_strings(entries, strings, ignore_duplicates=False)[source]

Unexpand all string variables in all entries where possible.

The operation is done in-place. If multiple string variables have the same name, only one of them is arbitrarily used unless ignore_duplicates is True in which case an exception is thrown.

bibpy.inherit_crossrefs(entries, inherit=True, override=False, exceptions={})[source]

Expand the crossreferences in the given entries.

The expansion is done according to biber (see section 2.4.1 of the biblatex manual).

Inheritance modes are either True for inheriting or False for no inheritance. Likewise fields can either be overwritten or not. Note that overriding fields is a destructive process, they cannot be recreated by uninherit_crossrefs.

Exceptions to both rules can be defined using the exceptions option which is expected to be a tuple of (source, target, options), where the source is the crossreferenced entry and target is the entry containing the crossref, as per biblatex nomenclature. The last field is a dict of the options (inherit and override) for this pair of source and target.

bibpy.uninherit_crossrefs(entries, inherit=True, override=False, exceptions={})[source]

Unexpand or collapse the crossreferences in the given entries.

The unexpansion is done according to biber (see section 2.4.1 of the biblatex manual). The ‘crossref’ fields of the entries are used if they refer to a valid key.

The options correspond to those given by the a call to inherit_crossrefs.

Inheritance modes are either ‘all’ (True) or ‘none’ (False). Fields can either be overwritten or not. Exceptions to both rules can be defined using the ‘exceptions’ option which is expected to be a dictionary mapping from one entry type to another and the value the ‘inherit’ and/or ‘override’ options. Both the source and the target can be ‘*’ to denote all entry types.

bibpy.inherit_xdata(entries)[source]

Expand the xdata fields in the given entries.

Inheritance is done according to biber (see section 3.11.6 of the biblatex manual).

bibpy.uninherit_xdata(entries)[source]

Unherit the xdata fields in the given entries.

Uninheritance is done according to biber (see section 3.11.6 of the biblatex manual).