zensols.bibstract package

Submodules

zensols.bibstract.app module

This utility extracts Bib(La)Tex references (a.k.a markers) from a (La)TeX project.

class zensols.bibstract.app.Application(config_factory, converter_library, log_name)[source]

Bases: object

This utility extracts Bib(La)Tex references from a (La)Tex.

__init__(config_factory, converter_library, log_name)
config_factory: ConfigFactory

The configuration factory used to create this instance.

converter_library: ConverterLibrary

The converter library used to print what’s available.

converters()[source]

List converters and their information.

export(texpath, output=None)[source]

Export the derived BibTex file.

Parameters:
  • texpath (str) – a path separated (‘:’ on Linux) list of files or directories to export

  • output (Path) – the output path name, or standard out if not given

log_name: str

The name of the package logger.

package(texpath, libpath=None, package_regex=None, no_extension=False, inverse=False)[source]

Return a list of all packages.

Parameters:
  • texpath (str) – a path separated (‘:’ on Linux) list of files or directories to export

  • libpath (str) – a path separated (‘:’ on Linux) list of files or directories of libraries to not include in results

  • package_regex (str) – the regular expression used to filter packages

  • no_extension (bool) – do not add the .sty extension

print_bibtex_ids()[source]

Print BibTex citation keys.

print_entry(citation_key)[source]

Print a single BibTex entry as it would be given in the output.

Parameters:

citation_key (str) – the citation key of entry to print out

print_extracted_ids(texpath)[source]

Print BibTex export citation keys.

Parameters:

texpath (Path) – either a file or directory to recursively scan for files with LaTex citation references

print_texfile_refs(texpath)[source]

Print citation references.

Parameters:

texpath (Path) – either a file or directory to recursively scan for files with LaTex citation references

zensols.bibstract.cli module

Command line entry point to the application.

class zensols.bibstract.cli.ApplicationFactory(*args, **kwargs)[source]

Bases: ApplicationFactory

__init__(*args, **kwargs)[source]
zensols.bibstract.cli.main(args=['/Users/landes/opt/lib/pixi/envs/zensols_relpo/bin/sphinx-build', '-M', 'html', '/Users/landes/view/util/bibstract/target/doc/stage', '/Users/landes/view/util/bibstract/target/doc/build'], **kwargs)[source]
Return type:

ActionResult

zensols.bibstract.converter module

A library of built in converters.

class zensols.bibstract.converter.ConditionalConverter(name, config_factory, converters=<factory>, includes=<factory>, excludes=<factory>)[source]

Bases: Converter

A converter that invokes a list of other converters if a certain entry key/value pair matches.

NAME = 'conditional_converter'
__init__(name, config_factory, converters=<factory>, includes=<factory>, excludes=<factory>)
config_factory: ConfigFactory

The configuration factory used to create this converter and used to get referenced converters.

converters: List[str]

The list of converters to inovke if the predicate condition is satisfied.

excludes: Dict[str, str]

The key/values that can not match in the entry to invoke the converters referenced by converters.

includes: Dict[str, str]

The key/values that must match in the entry to invoke the converters referenced by converters.

class zensols.bibstract.converter.CopyOrMoveKeyConverter(name, destructive=False, fields=<factory>)[source]

Bases: DestructiveConverter

Copy or move one or more fields in the entry. This is useful when your bibliography style expects one key, but the output (i.e.BibLatex) outputs a different named field).

When :obj:destructive is set to True, this copy operation becomes a move.

NAME = 'copy'

The name of the converter.

__init__(name, destructive=False, fields=<factory>)
fields: Dict[str, str]

The source to target list of fields specifying which keys to keys get copied or moved.

class zensols.bibstract.converter.DateToYearConverter(name, destructive=False, source_field='date', update_fields=('year',), format='%Y')[source]

Bases: DestructiveConverter

Converts the year part of a date field to a year. This is useful when using Zotero’s Better Biblatex extension that produces BibLatex formats, but you need BibTex entries.

NAME = 'date_year'

The name of the converter.

__init__(name, destructive=False, source_field='date', update_fields=('year',), format='%Y')
format: str = '%Y'

The datetime.datetime.strftime() formatted time, which defaults to a four digit year.

source_field: str = 'date'
update_fields: Tuple[str] = ('year',)

The fields to update using the new date format.

class zensols.bibstract.converter.RemoveConverter(name, destructive=False, keys=())[source]

Bases: DestructiveConverter

Remove entries that match a regular expression.

NAME = 'remove'

The name of the converter.

__init__(name, destructive=False, keys=())
keys: Tuple[str] = ()

A list of regular expressions, that if match the entry key, will remove the entry.

class zensols.bibstract.converter.ReplaceValue(name, fields=<factory>)[source]

Bases: Converter

Replace values of entries by regular expression.

NAME = 'replace'
__init__(name, fields=<factory>)
fields: List[Tuple[str, str, str]]

A list of tuples, each tuple having the key of the entry to modify, a string regular expression of what to change, and the replacement string.

class zensols.bibstract.converter.UpdateOrAddValue(name, fields=<factory>)[source]

Bases: Converter

Update (clobber) or add a new mapping in an entry.

NAME = 'update'
__init__(name, fields=<factory>)
fields: List[Tuple[str, str]]

A list of tuples, each tuple having the key to add and the value to update or add using Python interpolation syntax from existing entry keys.

zensols.bibstract.domain module

Domain and utility classes.

exception zensols.bibstract.domain.BibstractError[source]

Bases: ApplicationError

Application level error.

__module__ = 'zensols.bibstract.domain'
class zensols.bibstract.domain.Converter(name)[source]

Bases: object

A base class to convert fields of a BibTex entry (which is of type dict) to another field.

Subclasses should override _convert().

ENTRY_TYPE = 'ENTRYTYPE'
__init__(name)
convert(entry)[source]

Convert and return a new entry.

Parameters:

entry (Dict[str, str]) – the source data to transform

Return type:

Dict[str, str]

Returns:

a new instance of a dict with the transformed data

name: str

The name of the converter.

class zensols.bibstract.domain.ConverterLibrary(config_factory, converter_class_names, converter_names=None)[source]

Bases: Writable

__init__(config_factory, converter_class_names, converter_names=None)
config_factory: ConfigFactory

The configuration factory used to create the converters.

converter_class_names: List[str]

The list of converter class names currently available.

converter_names: List[str] = None

A list of converter names used to convert to BibTex entries.

property converters: Tuple[Converter]
property converters_by_name: Dict[str, Converter]
write(depth=0, writer=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, markdown_depth=1)[source]

Write the contents of this instance to writer using indention depth.

Parameters:
  • depth (int) – the starting indentation depth

  • writer (TextIOBase) – the writer to dump the content of this writable

class zensols.bibstract.domain.DestructiveConverter(name, destructive=False)[source]

Bases: Converter

A converter that can optionally remove or modify entries.

__init__(name, destructive=False)
destructive: bool = False

If true, remove the original field if converting from one key to another in the Bibtex entry.

class zensols.bibstract.domain.RegexFileParser(pattern=re.compile('\\\\\\\\cite\\\\{(.+?)\\\\}|\\\\{([a-zA-Z0-9, -]+?)\\\\}'), collector=<factory>)[source]

Bases: object

Finds all instances of the citation references in a file.

MULTI_REF_REGEX = re.compile('[^,\\s]+')

The regular expression used to find comma separated lists of citations commands (i.e. \cite).

REF_REGEX = re.compile('\\\\cite\\{(.+?)\\}|\\{([a-zA-Z0-9,-]+?)\\}')

The default regular expression used to find citation references in sty and tex files (i.e. \cite commands).

__init__(pattern=re.compile('\\\\\\\\cite\\\\{(.+?)\\\\}|\\\\{([a-zA-Z0-9, -]+?)\\\\}'), collector=<factory>)
collector: Set[str]

The set to add found references.

find(fileobj)[source]
pattern: Pattern = re.compile('\\\\cite\\{(.+?)\\}|\\{([a-zA-Z0-9,-]+?)\\}')

The regular expression pattern used to find the references.

class zensols.bibstract.domain.TexPathIterator(texpaths)[source]

Bases: object

Base class that finds LaTeX files (.tex, .sty, etc).

TEX_FILE_REGEX = re.compile('.+\\.(?:tex|sty|cls)$')
__init__(texpaths)
texpaths: Union[str, Path, Sequence[Path]]

Either a file or directory to recursively scan for files with LaTex citation references.

zensols.bibstract.extractor module

Extract BibTex references from a Tex file and add them from a master BibTex file.

class zensols.bibstract.extractor.Extractor(texpaths, converter_library, master_bib)[source]

Bases: TexPathIterator

Extracts references, parses the BibTex master source file, and extracts matching references from the LaTex file.

__init__(texpaths, converter_library, master_bib)
property bibtex_ids: iter

Return all BibTex string IDs. These could be BetterBibtex citation references.

converter_library: ConverterLibrary

The converter library used to print what’s available.

property converters: Tuple[Converter]
property database: BibDatabase

Return the BibTex Python object representation of master file.

property entries: Dict[str, Dict[str, str]]

The BibTex entries parsed from the master bib file.

extract(writer=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>, extracted_entries=None)[source]

Extract the master source BibTex matching citation references from the LaTex file(s) and write them to writer.

Parameters:

writer (TextIOWrapper) – the BibTex entry data sink

property extract_ids: set

Return the set of BibTex references to be extracted.

property extracted_entries: Dict[str, Dict[str, str]]

The BibTex entries parsed from the master bib file.

get_entry(citation_key)[source]
Return type:

Dict[str, Dict[str, str]]

master_bib: Path

The path to the master BibTex file.

print_bibtex_ids()[source]
print_extracted_ids()[source]
print_texfile_refs()[source]
property tex_refs: set

Return the set of parsed citation references.

write_entry(entry, bwriter=None, writer=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]

zensols.bibstract.pkg module

Find packages used in the tex path.

class zensols.bibstract.pkg.PackageFinder(texpaths, package_regex=re.compile('.*'), library_dirs=None, inverse=False)[source]

Bases: TexPathIterator

Find packages used in the tex path.

__init__(texpaths, package_regex=re.compile('.*'), library_dirs=None, inverse=False)
get_packages()[source]
Return type:

Set[str]

inverse: bool = False

Whether to invert the packages with all those packages found in library_dirs.

library_dirs: Union[str, Path, Sequence[Path]] = None

The list of library paths. Each path is not traversed to find packages.

package_regex: Union[str, Pattern] = re.compile('.*')

The regular expression used to filter what to return.

Module contents