Skip to content

stepup.reprep.bibsane

Sanitize BibTeX files.

BibsaneConfig

The configuration object controling BibSane behavior.

Note that the settings default to the most permissive and least invasive ones. We recommend the opposite settings, but you have to switch knowingly in the config file.

Source code in stepup/reprep/bibsane.py
@attrs.define
class BibsaneConfig:
    """The configuration object controling BibSane behavior.

    Note that the settings default to the most permissive and least invasive ones.
    We recommend the opposite settings, but you have to switch knowingly in the config file.
    """

    root: Path = attrs.field()
    """The parent directory of the configuration file."""

    amend: bool = attrs.field(default=False)
    """Set to `True` to amend inputs and outputs the StepUp workflow."""

    drop_entry_types: list[str] = attrs.field(default=attrs.Factory(list))
    """The entry types to drop from the BibTeX database."""

    normalize_doi: bool = attrs.field(default=False)
    """Set to `True` to normalize the DOIs in the entries."""

    duplicate_id: DuplicatePolicy = attrs.field(default=DuplicatePolicy.IGNORE)
    """The policy for duplicate BibTeX IDs: fail, merge or ignore."""

    duplicate_doi: DuplicatePolicy = attrs.field(default=DuplicatePolicy.IGNORE)
    """The policy for duplicate DOIs: fail, merge or ignore."""

    preambles_allowed: bool = attrs.field(default=True)
    """Set to `False` to disallow @preamble entries in the BibTeX database."""

    normalize_whitespace: bool = attrs.field(default=False)
    """Set to `True` to normalize the whitespace in the field values."""

    normalize_names: bool = attrs.field(default=False)
    """Set to `True` to normalize the author and editor names.

    (This currently broken.)
    """

    fix_page_double_hyphen: bool = attrs.field(default=False)
    """Set to `True` to fix the page ranges for which no double hyphen is used."""

    abbreviate_journal: Path | None = attrs.field(default=None)
    """The path to the journal abbreviation cache file.

    This must be a path relative to the parent of the configuration file.
    If no configuration file is provided, it must be relative to the current working directory.

    If set, unabbreviated journal names will be substituted by their ISO abbreviation,
    which are generated with pyiso4 and cached in the given file.
    The main reason for the cache file is to provide a mechanism for customizing abbreviations
    if pyiso4 does not provide the desired abbreviation.
    """

    sort: bool = attrs.field(default=False)
    """Set to `True` to sort the entries by year and first author.

    The sort key is `{year}{first author lowercase last name}`.
    """

    citation_policies: dict[str, dict[str, FieldPolicy]] = attrs.field(default=attrs.Factory(dict))
    """The field policies (must or may) for each entry type."""

    @classmethod
    def from_file(cls, fn_yaml: str, amend: bool = False):
        """Instantiate a configuration from a YAML config file."""
        if fn_yaml is None:
            config = cls(os.getcwd(), amend)
        else:
            with open(fn_yaml) as f:
                data = yaml.safe_load(f)
                data.setdefault("root", os.path.dirname(fn_yaml))
                data.setdefault("amend", amend)
                config = cattrs.structure(data, cls)
        return config

abbreviate_journal = attrs.field(default=None) class-attribute instance-attribute

The path to the journal abbreviation cache file.

This must be a path relative to the parent of the configuration file. If no configuration file is provided, it must be relative to the current working directory.

If set, unabbreviated journal names will be substituted by their ISO abbreviation, which are generated with pyiso4 and cached in the given file. The main reason for the cache file is to provide a mechanism for customizing abbreviations if pyiso4 does not provide the desired abbreviation.

amend = attrs.field(default=False) class-attribute instance-attribute

Set to True to amend inputs and outputs the StepUp workflow.

citation_policies = attrs.field(default=attrs.Factory(dict)) class-attribute instance-attribute

The field policies (must or may) for each entry type.

drop_entry_types = attrs.field(default=attrs.Factory(list)) class-attribute instance-attribute

The entry types to drop from the BibTeX database.

duplicate_doi = attrs.field(default=DuplicatePolicy.IGNORE) class-attribute instance-attribute

The policy for duplicate DOIs: fail, merge or ignore.

duplicate_id = attrs.field(default=DuplicatePolicy.IGNORE) class-attribute instance-attribute

The policy for duplicate BibTeX IDs: fail, merge or ignore.

fix_page_double_hyphen = attrs.field(default=False) class-attribute instance-attribute

Set to True to fix the page ranges for which no double hyphen is used.

normalize_doi = attrs.field(default=False) class-attribute instance-attribute

Set to True to normalize the DOIs in the entries.

normalize_names = attrs.field(default=False) class-attribute instance-attribute

Set to True to normalize the author and editor names.

(This currently broken.)

normalize_whitespace = attrs.field(default=False) class-attribute instance-attribute

Set to True to normalize the whitespace in the field values.

preambles_allowed = attrs.field(default=True) class-attribute instance-attribute

Set to False to disallow @preamble entries in the BibTeX database.

root = attrs.field() class-attribute instance-attribute

The parent directory of the configuration file.

sort = attrs.field(default=False) class-attribute instance-attribute

Set to True to sort the entries by year and first author.

The sort key is {year}{first author lowercase last name}.

from_file(fn_yaml, amend=False) classmethod

Instantiate a configuration from a YAML config file.

Source code in stepup/reprep/bibsane.py
@classmethod
def from_file(cls, fn_yaml: str, amend: bool = False):
    """Instantiate a configuration from a YAML config file."""
    if fn_yaml is None:
        config = cls(os.getcwd(), amend)
    else:
        with open(fn_yaml) as f:
            data = yaml.safe_load(f)
            data.setdefault("root", os.path.dirname(fn_yaml))
            data.setdefault("amend", amend)
            config = cattrs.structure(data, cls)
    return config