Skip to content

stepup.reprep.bibsane

Sanitize BibTeX files.

BibsaneConfig

The configuration object controlling 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 controlling 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."""

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

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

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

    strip_braces: bool = attrs.field(default=False)
    """Set to `True` to remove unwarranted use of braces in field values."""

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

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

    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_journals: bool = attrs.field(default=False)
    """Set to `True` to abbreviate journal names using ISO4."""

    custom_abbreviations: dict[str, str] = attrs.field(factory=dict)
    """Custom journal abbreviations.

    By default, pyiso4 is used to abbreviate journal names.
    The custom abbreviations can override those provided by pyiso4.
    """

    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):
        """Instantiate a configuration from a YAML config file."""
        if fn_yaml is None:
            config = cls(os.getcwd())
        else:
            with open(fn_yaml) as f:
                data = yaml.safe_load(f)
                if data is None:
                    data = {}
                elif not isinstance(data, dict):
                    raise ValueError(
                        f"Invalid BibSane config file: expected a mapping at top level.({fn_yaml})"
                    )
                data.setdefault("root", os.path.dirname(fn_yaml))
                config = cattrs.structure(data, cls)
        return config

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

Set to True to abbreviate journal names using ISO4.

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

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

custom_abbreviations = attrs.field(factory=dict) class-attribute instance-attribute

Custom journal abbreviations.

By default, pyiso4 is used to abbreviate journal names. The custom abbreviations can override those provided by pyiso4.

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_key = attrs.field(default=(DuplicatePolicy.IGNORE)) class-attribute instance-attribute

The policy for duplicate BibTeX keys: 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_whitespace = attrs.field(default=False) class-attribute instance-attribute

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

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}.

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

Set to True to remove unwarranted use of braces in field values.

from_file(fn_yaml) classmethod

Instantiate a configuration from a YAML config file.

Source code in stepup/reprep/bibsane.py
@classmethod
def from_file(cls, fn_yaml: str):
    """Instantiate a configuration from a YAML config file."""
    if fn_yaml is None:
        config = cls(os.getcwd())
    else:
        with open(fn_yaml) as f:
            data = yaml.safe_load(f)
            if data is None:
                data = {}
            elif not isinstance(data, dict):
                raise ValueError(
                    f"Invalid BibSane config file: expected a mapping at top level.({fn_yaml})"
                )
            data.setdefault("root", os.path.dirname(fn_yaml))
            config = cattrs.structure(data, cls)
    return config