Skip to content

main.python_writers

Classes

PythonWriters

PythonWriters(options)

Bases: BasicInput

Python writers for generating BibTeX files with various formatting options.

Parameters:

Name Type Description Default
options dict[str, Any]

Configuration options for BibTeX generation. - is_sort_entry_fields (bool): Whether to sort entry fields (default: True) - is_sort_blocks (bool): Whether to sort bibliography blocks (default: True) - sort_entries_by_field_keys_reverse (bool): Reverse sort order for entries (default: True) - choose_abbr_zotero_save (str): Source selection - "abbr", "zotero", or "save" (default: "save")

required

Attributes:

Name Type Description
choose_abbr_zotero_save str

Selected bibliography purpose ("abbr", "zotero", or "save")

bib_name_for_abbr str

Filename for abbreviated bibliography (default: "abbr.bib")

bib_name_for_zotero str

Filename for Zotero bibliography (default: "zotero.bib")

bib_name_for_save str

Filename for saved bibliography (default: "save.bib")

join_flag_in_http str

The join flag for HTTP-related formatting (default: " | " or " |\n")

display_www_google_connected_scite list[str]

Display options selection from ["www", "google", "connected", "scite"]

bibtex_format_indent str

Indentation string for BibTeX formatting (default: " ")

bibtex_format_trailing_comma bool

Whether to include trailing commas in BibTeX entries (default: True)

bibtex_format_block_separator str

Separator between BibTeX blocks (default: "")

Source code in pybibtexer/main/python_writers.py
def __init__(self, options: dict[str, Any]) -> None:
    # Set default options if not provided
    options["choose_abbr_zotero_save"] = options.get("choose_abbr_zotero_save", "save")
    options["is_sort_entry_fields"] = options.get("is_sort_entry_fields", True)
    options["is_sort_blocks"] = options.get("is_sort_blocks", True)
    options["sort_entries_by_field_keys_reverse"] = options.get("sort_entries_by_field_keys_reverse", True)
    super().__init__(options)

    # Initialize bibliography source filenames
    self.bib_name_for_abbr = options.get("bib_name_for_abbr", "abbr.bib")
    self.bib_name_for_zotero = options.get("bib_name_for_zotero", "zotero.bib")
    self.bib_name_for_save = options.get("bib_name_for_save", "save.bib")

    self.join_flag_in_http = options.get("join_flag_in_http", " | ")  # " |\n"

    # Initialize display options
    self.display_www_google_connected_scite = options.get(
        "display_www_google_connected_scite", ["www", "google", "connected", "scite"]
    )

    # Initialize BibTeX formatting options
    self.bibtex_format_indent = options.get("bibtex_format_indent", "  ")
    self.bibtex_format_trailing_comma = options.get("bibtex_format_trailing_comma", True)
    self.bibtex_format_block_separator = options.get("bibtex_format_block_separator", "")

    # Create and configure BibtexFormat object
    bibtex_format = BibtexFormat()
    bibtex_format.indent = self.bibtex_format_indent
    bibtex_format.block_separator = self.bibtex_format_block_separator
    bibtex_format.trailing_comma = self.bibtex_format_trailing_comma
    self._bibtex_format: BibtexFormat = bibtex_format

Functions

write_to_file
write_to_file(
    original_data,
    file_name,
    write_flag="w",
    path_storage=None,
    check=True,
    delete_first_empty=True,
    delete_last_empty=True,
    compulsory=False,
    delete_original_file=False,
)

Write.

Parameters:

Name Type Description Default
original_data Union[Library, list[Block], list[str]]

data

required
file_name str

file name

required
write_flag str = "w"

write flag

'w'
path_storage Optional[str] = None

path storage

None
check bool = True

check

True
delete_first_empty bool = True

delete first empty

True
delete_last_empty bool = True

delete last empty

True
compulsory bool = False

compulsory

False
delete_original_file bool = False

delete original file

False
Source code in pybibtexer/main/python_writers.py
def write_to_file(
    self,
    original_data: Library | list[Block] | list[str],
    file_name: str,
    write_flag: str = "w",
    path_storage: str | None = None,
    check: bool = True,
    delete_first_empty: bool = True,
    delete_last_empty: bool = True,
    compulsory: bool = False,
    delete_original_file: bool = False,
) -> None:
    """Write.

    Args:
        original_data (Union[Library, list[Block], list[str]]): data
        file_name (str): file name
        write_flag (str = "w"): write flag
        path_storage (Optional[str] = None): path storage
        check (bool = True): check
        delete_first_empty (bool = True): delete first empty
        delete_last_empty (bool = True): delete last empty
        compulsory (bool = False): compulsory
        delete_original_file (bool = False): delete original file

    """
    _options = {}
    _options.update(self.options)
    _library_str = ConvertLibrayToStr(_options)

    if isinstance(original_data, Library):
        data_list = _library_str.generate_str(original_data, self._bibtex_format)
    elif isinstance(original_data, list):
        if all(isinstance(line, str) for line in original_data):
            data_list = [line for line in original_data if isinstance(line, str)]
        else:
            data_list = [line for line in original_data if isinstance(line, Block)]
            data_list = _library_str.generate_str(data_list, self._bibtex_format)

    write_list(
        data_list,
        file_name,
        write_flag,
        path_storage,
        check,
        delete_first_empty,
        delete_last_empty,
        compulsory,
        delete_original_file,
    )
    return None
write_to_str
write_to_str(library)

Serialize a BibTeX database to a string.

Parameters:

Name Type Description Default
library Library | list[Block]

BibTeX database to serialize.

required
Source code in pybibtexer/main/python_writers.py
def write_to_str(self, library: Library | list[Block]) -> list[str]:
    """Serialize a BibTeX database to a string.

    Args:
        library (Library | list[Block]): BibTeX database to serialize.

    """
    return ConvertLibrayToStr(self.options).generate_str(library, self._bibtex_format)