bookbuilderpy package¶
bookbuilderpy is a package for compiling electronic books.
- class bookbuilderpy.Build(input_file, output_dir, fail_without_pandoc=True)[source]¶
Bases:
AbstractContextManager
A class to keep and access information about the build process.
Subpackages¶
Submodules¶
bookbuilderpy.build module¶
The build process: The main class of the book building tool chain.
- class bookbuilderpy.build.Build(input_file, output_dir, fail_without_pandoc=True)[source]¶
Bases:
AbstractContextManager
A class to keep and access information about the build process.
bookbuilderpy.build_result module¶
A collection for build results.
- class bookbuilderpy.build_result.File(path)[source]¶
Bases:
object
A single file created by a build.
bookbuilderpy.compress module¶
Routines for compressing lists of files.
- bookbuilderpy.compress.can_xz_compress()[source]¶
Check if xz compression is available.
- Return type:
- Returns:
True if xz compression is available, False otherwise
- bookbuilderpy.compress.can_zip_compress()[source]¶
Check if zip compression is available.
- Return type:
- Returns:
True if zip compression is available, False otherwise
bookbuilderpy.constants module¶
The basic preprocessor command strings.
- bookbuilderpy.constants.CMD_ABSOLUTE_CODE: Final[str] = 'abs.code'¶
an absolute code input to be included as a listing: abs.code{label}{caption}{path}{lines}{labels}{args} label: the label of the listing caption: the caption of the listing path: the absolute path fragment to resolve lines: the lines of the code to keep, or empty to keep all labels: the labels for selecting code pieces, or empty to keep all args: any additional arguments to pass to the code renderer
- bookbuilderpy.constants.CMD_ABSOLUTE_FIGURE: Final[str] = 'abs.figure'¶
an absolute figure reference abs.figure{label}{caption}{path}{args}
- bookbuilderpy.constants.CMD_DEFINITION: Final[str] = 'definition'¶
an command for formatting definitions definition{type}{label}{body}
- bookbuilderpy.constants.CMD_GET_REPO: Final[str] = 'repo'¶
obtain information about a repository repo{repo-id}{info-id}
- bookbuilderpy.constants.CMD_GIT_CODE: Final[str] = 'git.code'¶
a git code include: git.code{repo}{label}{caption}{path}{lines}{labels}{args} repo: the repository ID to use label: the label of the listing caption: the caption of the listing path: the relative path fragment to resolve lines: the lines of the code to keep, or empty to keep all labels: the labels for selecting code pieces, or empty to keep all args: any additional arguments to pass to the code renderer
- bookbuilderpy.constants.CMD_INPUT: Final[str] = 'rel.input'¶
the command for loading the input data rel.inputpath}
- bookbuilderpy.constants.CMD_RELATIVE_CODE: Final[str] = 'rel.code'¶
a relative code input to be included as a listing: rel.code{label}{caption}{path}{lines}{labels}{args} label: the label of the listing caption: the caption of the listing path: the relative path fragment to resolve lines: the lines of the code to keep, or empty to keep all labels: the labels for selecting code pieces, or empty to keep all args: any additional arguments to pass to the code renderer
- bookbuilderpy.constants.CMD_RELATIVE_FIGURE: Final[str] = 'rel.figure'¶
a relative figure reference rel.figure{label}{caption}{path}{args}
- bookbuilderpy.constants.META_AUTHOR: Final[str] = 'author'¶
A meta-data property identifying the author of a book.
- bookbuilderpy.constants.META_CUR_LANG_NAME: Final[str] = 'lang.name'¶
The name of the current language.
- bookbuilderpy.constants.META_DATE_TIME: Final[str] = 'time'¶
the meta data id for the current date and time
- bookbuilderpy.constants.META_REPO_INFO_COMMIT: Final[str] = 'repo.commit'¶
the key for the repository commit
- bookbuilderpy.constants.META_REPO_INFO_DATE: Final[str] = 'repo.date'¶
the key for the repository date
- bookbuilderpy.constants.META_REPO_INFO_NAME: Final[str] = 'repo.name'¶
the key for the repository name
- bookbuilderpy.constants.META_TITLE: Final[str] = 'title'¶
A meta-data property identifying the title of a book.
- bookbuilderpy.constants.PANDOC_BIBLIOGRAPHY: Final[str] = 'bibliography'¶
the pandoc bibliography key
- bookbuilderpy.constants.PANDOC_TEMPLATE_EPUB: Final[str] = 'template.epub'¶
the pandoc epub template
- bookbuilderpy.constants.PANDOC_TEMPLATE_HTML5: Final[str] = 'template.html'¶
the pandoc html template
- bookbuilderpy.constants.PANDOC_TEMPLATE_LATEX: Final[str] = 'template.latex'¶
the pandoc latex template
- bookbuilderpy.constants.WEBSITE_BODY_TAG_1: Final[str] = '<div id="files">'¶
The begin of the tag to be replaced.
- bookbuilderpy.constants.WEBSITE_BODY_TAG_2: Final[str] = '</div>'¶
The end of the tag to be replaced.
- bookbuilderpy.constants.WEBSITE_DOWNLOAD_DOWNLOAD_SPAN_ARG: Final[str] = ' class="downloadFile"'¶
The argument to the single file span entry.
- bookbuilderpy.constants.WEBSITE_DOWNLOAD_FILE_A_ARG: Final[str] = ' class="downloadFileName"'¶
The argument to the single file name span entry.
- bookbuilderpy.constants.WEBSITE_DOWNLOAD_FILE_DESC_SPAN_ARG: Final[str] = ' class="downloadFileDesc"'¶
The argument to the single file description entry.
- bookbuilderpy.constants.WEBSITE_DOWNLOAD_LI_ARG: Final[str] = ' class="download"'¶
The argument to the single file download entry.
- bookbuilderpy.constants.WEBSITE_DOWNLOAD_SIZE_SPAN_ARG: Final[str] = ' class="downloadFileSize"'¶
The argument to the single file size span entry.
- bookbuilderpy.constants.WEBSITE_DOWNLOAD_UL_ARG: Final[str] = ' class="downloads"'¶
The argument to the single language file download list.
- bookbuilderpy.constants.WEBSITE_LANGS_LI_ARG: Final[str] = ' class="oneLang"'¶
The argument to the language list item in the download files.
- bookbuilderpy.constants.WEBSITE_LANGS_NAME_SPAN_ARG: Final[str] = ' class="oneLangName"'¶
The argument to the language name item in the download files.
bookbuilderpy.format_python module¶
A formatter for python code.
- bookbuilderpy.format_python.format_python(code, strip_docstrings=True, strip_comments=True, strip_hints=True)[source]¶
Format a python code fragment.
- Parameters:
- Return type:
- Returns:
the formatted code
- bookbuilderpy.format_python.preprocess_python(code, lines=None, labels=None, args=None)[source]¶
Preprocess Python code.
First, we select all lines of the code we want to keep. If labels are defined, then lines can be kept as ranges or as single lines. Otherwise, all lines are selected in this step.
Then, if line numbers are provided, we selected the lines based on the line numbers from the lines we have preserved.
Finally, the Python formatter is applied.
- Parameters:
- Return type:
- Returns:
the formatted code string
bookbuilderpy.git module¶
Tools for interacting with git.
- class bookbuilderpy.git.Repo(path, url, commit, date_time)[source]¶
Bases:
object
An immutable record of a git repository.
- get_base_url()[source]¶
Get the base url of this repository.
- Return type:
- Returns:
the base url of this repository
- get_name()[source]¶
Get the name of this repository in the form ‘user/name’.
- Return type:
- Returns:
the name of this repository in the form ‘user/name’.
bookbuilderpy.html module¶
Post-process HTML files.
- bookbuilderpy.html.html_postprocess(in_file, out_file, flatten_data_uris=True, fully_evaluate_html=False, purge_scripts=False, minify=True, purge_mathjax=True, canonicalize_ids=True, overwrite=False)[source]¶
Post-process a html file.
- Parameters:
in_file (
str
) – the input fileout_file (
str
) – the output fileflatten_data_uris (
bool
, default:True
) – should we flatten data URIs?fully_evaluate_html (
bool
, default:False
) – should we use selenium to fully evaluate all html and javascript?purge_scripts (
bool
, default:False
) – should we purge all javascripts from the file?minify (
bool
, default:True
) – should we minify the HTML output?purge_mathjax (
bool
, default:True
) – purge all mathjax stuff?canonicalize_ids (
bool
, default:True
) – should we canonicalize the IDs?overwrite (
bool
, default:False
) – should the output file be overwritten if it exists?
- Return type:
- Returns:
the output file
bookbuilderpy.logger module¶
The logger routine for writing a log string to stdout.
bookbuilderpy.pandoc module¶
A routine for invoking pandoc.
- bookbuilderpy.pandoc.epub(source_file, dest_file, format_in='markdown', locale=None, standalone=True, tabstops=2, toc_print=True, toc_depth=3, crossref=True, bibliography=True, number_sections=True, get_meta=<function <lambda>>, resolve_resources=<function <lambda>>)[source]¶
Invoke pandoc to build epub output.
- Parameters:
source_file (
str
) – the source filedest_file (
str
) – the destination fileformat_in (
str
, default:'markdown'
) – the input formatlocale (
Optional
[str
], default:None
) – the language to be used for compilingstandalone (
bool
, default:True
) – should we produce a stand-alone document?tabstops (
Optional
[int
], default:2
) – the number of spaces with which we replace a tab character, or None to not replacetoc_print (
bool
, default:True
) – should we print the table of contentstoc_depth (
int
, default:3
) – the depth of the table of contentscrossref (
bool
, default:True
) – should we use crossrefbibliography (
bool
, default:True
) – should we use a bibliographynumber_sections (
bool
, default:True
) – should sections be numbered?get_meta (
Callable
, default:<function <lambda> at 0x7fa5c66f5120>
) – a function to access meta-dataresolve_resources (
Callable
, default:<function <lambda> at 0x7fa5c66f51b0>
) – a function to resolve resources
- Return type:
- Returns:
the Path to the generated output file and it size
- bookbuilderpy.pandoc.html(source_file, dest_file, format_in='markdown', locale=None, standalone=True, tabstops=2, toc_print=True, toc_depth=3, crossref=True, bibliography=True, number_sections=True, get_meta=<function <lambda>>, resolve_resources=<function <lambda>>)[source]¶
Invoke pandoc to build HTML output.
- Parameters:
source_file (
str
) – the source filedest_file (
str
) – the destination fileformat_in (
str
, default:'markdown'
) – the input formatlocale (
Optional
[str
], default:None
) – the language to be used for compilingstandalone (
bool
, default:True
) – should we produce a stand-alone document?tabstops (
Optional
[int
], default:2
) – the number of spaces with which we replace a tab character, or None to not replacetoc_print (
bool
, default:True
) – should we print the table of contentstoc_depth (
int
, default:3
) – the depth of the table of contentscrossref (
bool
, default:True
) – should we use crossrefbibliography (
bool
, default:True
) – should we use a bibliographynumber_sections (
bool
, default:True
) – should sections be numbered?get_meta (
Callable
, default:<function <lambda> at 0x7fa5c66f4f70>
) – a function to access meta-dataresolve_resources (
Callable
, default:<function <lambda> at 0x7fa5c66f5000>
) – a function to resolve resources
- Returns:
the Path to the generated output file and it size
- Return type:
- bookbuilderpy.pandoc.latex(source_file, dest_file, format_in='markdown', locale=None, standalone=True, tabstops=2, toc_print=True, toc_depth=3, crossref=True, bibliography=True, number_sections=True, top_level_division='chapter', use_listings=False, get_meta=<function <lambda>>, resolve_resources=<function <lambda>>)[source]¶
Invoke pandoc to build LaTeX and then PDF output.
- Parameters:
source_file (
str
) – the source filedest_file (
str
) – the destination fileformat_in (
str
, default:'markdown'
) – the input formatlocale (
Optional
[str
], default:None
) – the language to be used for compilingstandalone (
bool
, default:True
) – should we produce a stand-alone document?tabstops (
Optional
[int
], default:2
) – the number of spaces with which we replace a tab character, or None to not replacetoc_print (
bool
, default:True
) – should we print the table of contentstoc_depth (
int
, default:3
) – the depth of the table of contentscrossref (
bool
, default:True
) – should we use crossrefbibliography (
bool
, default:True
) – should we use a bibliographynumber_sections (
bool
, default:True
) – should sections be numbered?top_level_division (
str
, default:'chapter'
) – the top-level divisionuse_listings (
bool
, default:False
) – should the listings package be used?get_meta (
Callable
, default:<function <lambda> at 0x7fa5c66d4820>
) – a function to access meta-dataresolve_resources (
Callable
, default:<function <lambda> at 0x7fa5c66f4a60>
) – a function to resolve resources
- Return type:
- Returns:
the Path to the generated output file and it size
- bookbuilderpy.pandoc.pandoc(source_file, dest_file, format_in='markdown', format_out='latex', locale=None, standalone=True, tabstops=2, toc_print=True, toc_depth=3, crossref=True, bibliography=True, template=None, csl=None, number_sections=True, args=None, resolve_resources=<function <lambda>>, overwrite=False)[source]¶
Invoke pandoc.
- Parameters:
source_file (
str
) – the source filedest_file (
str
) – the destination fileformat_in (
str
, default:'markdown'
) – the input formatformat_out (
str
, default:'latex'
) – the output formatstandalone (
bool
, default:True
) – should we produce a stand-alone document?tabstops (
Optional
[int
], default:2
) – the number of spaces with which we replace a tab character, or None to not replacetoc_print (
bool
, default:True
) – should we print the table of contentstoc_depth (
int
, default:3
) – the depth of the table of contentscrossref (
bool
, default:True
) – should we use crossrefbibliography (
bool
, default:True
) – should we use a bibliographytemplate (
Optional
[str
], default:None
) – which template should we use, if any?csl (
Optional
[str
], default:None
) – which csl file should we use, if any?number_sections (
bool
, default:True
) – should sections be numbered?locale (
Optional
[str
], default:None
) – the language to be used for compilingargs (
Optional
[list
[str
]], default:None
) – any additional argumentsresolve_resources (
Callable
, default:<function <lambda> at 0x7fa5c66d4430>
) – a function to resolve resourcesoverwrite (
bool
, default:False
) – should the output file be overwritten if it exists?
- Return type:
- Returns:
the Path to the generated output file and it size
bookbuilderpy.parse_metadata module¶
An internal package for loading metadata.
- bookbuilderpy.parse_metadata.load_initial_metadata(in_file, in_dir)[source]¶
Load the initial metadata.
This function does not process the complete document structure but only resolves at most one include. It also does not expand other commands and it does not perform any language-based resolution. It is only there to gain access to the raw metadata which should be the same over all builds of a book. This means things such as shared source code repositories.
bookbuilderpy.path module¶
The base class with the information of a build.
- class bookbuilderpy.path.Path(value)[source]¶
Bases:
str
An immutable representation of a path.
- as_directory()[source]¶
Return the closest directory along this path.
- Return type:
- Returns:
the directory: either this path if it already identifies a directory, or the parent directory if this path identifies a file.
- Raises:
ValueError – if no containing directory exists
- static copy_file(source, dest)[source]¶
Copy one file to another one, doing gz-unzipping if necessary.
This method copies a source file to a destination file. If the source file has suffix “svgz” and the destination file has suffix “svg” OR if the source file has suffix “gz” and the destination file has not, then we will unzip the source file to the destination file. Otherwise, a normal copy is performed.
- static copy_resource(source_dir, input_file, dest_dir)[source]¶
Copy an input file to an destination directory.
- enforce_contains(other)[source]¶
Raise an exception if this path does not contain the other path.
- Parameters:
other (
str
) – the other path- Raises:
ValueError – if other is not a sub-path of this path
- Return type:
- enforce_dir()[source]¶
Enforce that a path references an existing directory.
- Raises:
ValueError – if path does not reference an existing directory
- Return type:
- enforce_file()[source]¶
Enforce that a path references an existing file.
- Raises:
ValueError – if path does not reference an existing file
- Return type:
- enforce_neither_contains(other)[source]¶
Enforce that neither path contains another one.
- Parameters:
other (
str
) – the other path- Raises:
ValueError – if other is contained in this path or vice versa
- Return type:
- ensure_file_exists()[source]¶
Atomically ensure that the file exists and create it otherwise.
- Return type:
- Returns:
True if the file already existed and False if it was newly and atomically created.
- Raises:
ValueError if anything goes wrong during the file creation
- read_all_str()[source]¶
Read a file as a single string.
- Return type:
- Returns:
the single string of text
- relative_to(base_path)[source]¶
Compute a relative path of this path towards the given base path.
- Parameters:
base_path (
str
) – the string- Return type:
- Returns:
a relative path
- Raises:
ValueError – if this path is not inside base_path
- resolve_input_file(relative_path, lang=None)[source]¶
Resolve a path to an input file relative to this path.
- Parameters:
- Return type:
- Returns:
the resolved path
- Raises:
ValueError – if the path cannot be resolved to a file
- resolve_inside(relative_path)[source]¶
Resolve a relative path to an absolute path inside this path.
- Parameters:
relative_path (
str
) – the path to resolve- Return type:
- Returns:
the resolved child path
- Raises:
ValueError – If the path would resolve to something outside of this path and/or if it is empty.
bookbuilderpy.pdf module¶
Post-process PDF files.
bookbuilderpy.preprocessor module¶
The preprocessor commands to be applied once the text has been loaded.
- bookbuilderpy.preprocessor.preprocess(text, input_dir, get_meta, get_repo, repo, output_dir)[source]¶
Apply all the preprocessor commands to the given text.
bookbuilderpy.preprocessor_code module¶
A preprocessor for loading code.
- bookbuilderpy.preprocessor_code.get_programming_language(path)[source]¶
Get the programming language corresponding to a path.
bookbuilderpy.preprocessor_commands module¶
Regular-expression based command generation and invocation.
- bookbuilderpy.preprocessor_commands.create_preprocessor(name, func, n=1, strip_white_space=False, wrap_in_newlines=0)[source]¶
Create a preprocessor command.
A LaTeX-style command can be defined as an (recursive) regular expression. The start of the command is indicated by name. It then has n arguments with n>=0. Each argument is wrapped into a { and a }. Example: sub{1}{2}.
This function returns a function f which can be applied an arbitrary string s. The function f will iteratively process all invocations of name that appear in s, pass the extracted parameter values to func, and replace the whole matched string with the return value of func.
The command can appear nested in its arguments. In this case, the preprocessor f will resolve the inner-most occurences first.
- Parameters:
name (
str
) – the command namefunc (
Callable
) – the function to calln (
int
, default:1
) – the number of arguments to pass to funcstrip_white_space (
bool
, default:False
) – should surrounding white space be stripped?wrap_in_newlines (
int
, default:0
) – the number of newlines into which the output should be wrapped
- Return type:
- Returns:
a function that can be invoked on a string and which replaces all the occurences of the command with the results of corresponding func invocations
>>> f = lambda a, b: a + "-" + b >>> cmd = create_preprocessor("sub", f, 2) >>> cmd("x \\sub{7}{3} y \\sub{\\sub{8}{5}}{\\sub{4}{3}}") 'x 7-3 y 8-5-4-3' >>> cmd = create_preprocessor("mm", lambda: "Z", 0, True) >>> cmd("a\\mm\\mm\\mmb") 'aZZZb' >>> cmd = create_preprocessor("swp", lambda a, b: "("+b+","+a+")", 2) >>> cmd("\\swp{1}{2}") '(2,1)' >>> cmd("\\swp{\\swp{1}{2}}{3}") '(3,(2,1))' >>> cmd("\\swp{\\swp{\\swp{1}{2}}{3}}{\\swp{4}{5}}") '((5,4),(3,(2,1)))' >>> cmd = create_preprocessor("y", lambda x: str(int(x)*2), 1) >>> cmd("12\\y{3}4") '1264' >>> cmd = create_preprocessor("y", lambda x: f"a{x}b", 1, ... wrap_in_newlines=2) >>> cmd("12\\y{3}4") '12\n\na3b\n\n4'
bookbuilderpy.preprocessor_input module¶
A preprocessor that loads one root file and resolves are relative inputs.
bookbuilderpy.shell module¶
The tool for invoking shell commands.
- bookbuilderpy.shell.shell(command, timeout=3600, cwd=None, wants_stdout=False, exit_code_to_str=None, check_stderr=<function <lambda>>)[source]¶
Execute a text-based command on the shell.
The command is executed and its stdout and stderr and return code are captured. If the command had a non-zero exit code, an exception is thrown. The command itself, as well as the parameters are logged via the logger. If wants_stdout is True, the command’s stdout is returned. Otherwise, None is returned.
- Parameters:
command (
Union
[str
,Iterable
[str
]]) – the command to executetimeout (
int
, default:3600
) – the timeoutcwd (
Optional
[str
], default:None
) – the directory to run insidewants_stdout (
bool
, default:False
) – if True, the stdout is returned, if False, None is returnedexit_code_to_str (
Optional
[dict
[int
,str
]], default:None
) – an optional map converting erroneous exit codes to stringscheck_stderr (
Callable
[[str
],Optional
[BaseException
]], default:<function <lambda> at 0x7fa5bfcac700>
) – an optional callable that is applied to the std_err string and may raise an exception if need be
- Return type:
bookbuilderpy.source_tools module¶
In this file, we put some shared tools for rendering source codes.
- bookbuilderpy.source_tools.format_empty_lines(lines, empty_before=<function <lambda>>, no_empty_after=<function <lambda>>, force_no_empty_after=<function <lambda>>, max_consecutive_empty_lines=1)[source]¶
Obtain a generator that strips any consecutive empty lines.
- Parameters:
empty_before (
Callable
, default:<function <lambda> at 0x7fa5bf211630>
) – a function checking whether an empty line is required before a certain stringno_empty_after (
Callable
, default:<function <lambda> at 0x7fa5bf2116c0>
) – a function checking whether an empty line is prohibited after a stringforce_no_empty_after (
Callable
, default:<function <lambda> at 0x7fa5bf211750>
) – a function checking whether an empty line is prohibited after a stringmax_consecutive_empty_lines (
int
, default:1
) – the maximum number of permitted consecutive empty lines
- Return type:
- Returns:
the generation
>>> code = ["", "a", "", "b", "", "", "c", "", "", "", "d", "e", ""] >>> format_empty_lines(code, max_consecutive_empty_lines=3) ['a', '', 'b', '', '', 'c', '', '', '', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=2) ['a', '', 'b', '', '', 'c', '', '', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=1) ['a', '', 'b', '', 'c', '', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=0) ['a', 'b', 'c', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=2, ... no_empty_after=lambda s: s == "b") ['a', '', 'b', 'c', '', '', 'd', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=2, ... no_empty_after=lambda s: s == "b", ... empty_before=lambda s: s == "e") ['a', '', 'b', 'c', '', '', 'd', '', 'e'] >>> format_empty_lines(code, max_consecutive_empty_lines=2, ... no_empty_after=lambda s: s == "b", ... empty_before=lambda s: s == "e", ... force_no_empty_after=lambda s: s == "d") ['a', '', 'b', 'c', '', '', 'd', 'e']
- bookbuilderpy.source_tools.select_lines(code, lines=None, labels=None, line_comment_start='#', max_consecutive_empty_lines=1)[source]¶
Select lines of source code based on labels and line indices.
First, we select all lines of the code we want to keep. If labels are defined, then lines are kept as ranges or as single lines for all pre-defined labels. Ranges may overlap and/or intersect. Otherwise, all lines are selected in this step.
Then, if line numbers are provided, we selected the lines based on the line numbers from the lines we have preserved.
Finally, leading and trailing empty lines as well as superfluous empty lines are removed.
- Parameters:
lines (
Optional
[Iterable
[int
]], default:None
) – the lines to keep, or None if we keep alllabels (
Optional
[Iterable
[str
]], default:None
) – a list of labels marking start and end of code snippets to includeline_comment_start (
str
, default:'#'
) – the string marking the line comment startmax_consecutive_empty_lines (
int
, default:1
) – the maximum number of permitted consecutive empty lines
- Return type:
- Returns:
the list of selected lines
>>> select_lines(["def a():", " b=c", " return x"]) ['def a():', ' b=c', ' return x'] >>> pc = ["# start x", "def a():", " b=c # -x", " return x", "# end x"] >>> select_lines(pc, labels={"x"}) ['def a():', ' return x']
- bookbuilderpy.source_tools.strip_common_whitespace_prefix(lines)[source]¶
Strip a common whitespace prefix from a list of strings and merge them.
- Parameters:
- Return type:
- Returns:
the code with the white space prefix stripped
>>> strip_common_whitespace_prefix([" a", " b"]) ['a', ' b'] >>> strip_common_whitespace_prefix([" a", " b"]) ['a', 'b'] >>> strip_common_whitespace_prefix([" a", " b"]) ['a', 'b'] >>> strip_common_whitespace_prefix([" a", " b", "c"]) [' a', ' b', 'c'] >>> strip_common_whitespace_prefix([" a", " b", "c"]) [' a', ' b', 'c'] >>> strip_common_whitespace_prefix([" a", " b", " c"]) ['a', 'b', ' c']
bookbuilderpy.strings module¶
Some utility methods for string processing.
- bookbuilderpy.strings.datetime_to_date_str(date)[source]¶
Convert a datetime object to a date string.
- bookbuilderpy.strings.datetime_to_datetime_str(date)[source]¶
Convert a datetime object to a date-time string.
- bookbuilderpy.strings.enforce_non_empty_str(text)[source]¶
Enforce that a text is a non-empty string.
- Parameters:
text (
str
) – the text- Return type:
- Returns:
the text
- Raises:
TypeError – if text is not a str
ValueError – if text is empty
- bookbuilderpy.strings.enforce_non_empty_str_without_ws(text)[source]¶
Enforce that a text is a non-empty string without white space.
- Parameters:
text (
str
) – the text- Return type:
- Returns:
the text
- Raises:
TypeError – if text is not a str
ValueError – if text is empty or contains any white space characters
- bookbuilderpy.strings.get_prefix_str(str_list)[source]¶
Compute the common prefix string.
- Parameters:
str_list (
tuple
[str
,...
] |list
[str
]) – the list of strings- Return type:
- Returns:
the common prefix
>>> get_prefix_str(["abc", "acd"]) 'a' >>> get_prefix_str(["xyz", "gsdf"]) '' >>> get_prefix_str([]) '' >>> get_prefix_str(["abx"]) 'abx' >>> get_prefix_str(("\\relative.path", "\\relative.figure", ... "\\relative.code")) '\\relative.'
- bookbuilderpy.strings.lines_to_str(lines, trailing_newline=True)[source]¶
Convert an iterable of strings to a single string.
- Parameters:
- Return type:
- Returns:
the single string
>>> lines_to_str(["a", "b", "", "c", ""], trailing_newline=True) 'a\nb\n\nc\n' >>> lines_to_str(["a", "b", "", "c"], trailing_newline=True) 'a\nb\n\nc\n' >>> lines_to_str(["a", "b", "", "c"], trailing_newline=False) 'a\nb\n\nc' >>> lines_to_str(["a", "b", "", "c", ""], trailing_newline=False) 'a\nb\n\nc'
- bookbuilderpy.strings.regex_sub(search, replace, inside)[source]¶
Replace all occurrences of ‘search’ in ‘inside’ with ‘replace’.
- Parameters:
- Return type:
- Returns:
the new string after the recursive replacement
>>> regex_sub('[ \t]+\n', '\n', ' bla \nxyz\tabc\t\n') ' bla\nxyz\tabc\n' >>> regex_sub('[0-9]A', 'X', '23A7AA') '2XXA'
- bookbuilderpy.strings.str_to_lines(text)[source]¶
Convert a string to an iterable of lines.
>>> str_to_lines("\n123\n 456\n789 \n 10\n\n") ['', '123', ' 456', '789 ', ' 10', '', '']
bookbuilderpy.temp module¶
A set of utilities interactions with the file system.
- class bookbuilderpy.temp.TempDir(value)[source]¶
Bases:
Path
,AbstractContextManager
A scoped temporary directory to be used in a ‘with’ block.
The directory and everything in it will be deleted upon exiting the ‘with’ block.
- class bookbuilderpy.temp.TempFile(value)[source]¶
Bases:
Path
,AbstractContextManager
A scoped temporary file to be used in a ‘with’ block.
This file will be deleted upon exiting the ‘with’ block.
bookbuilderpy.types module¶
Some basic type handling routines.
- bookbuilderpy.types.type_error(obj, name, expected=None, call=False)[source]¶
Create an error to raise if a type did not fit.
- Parameters:
- Return type:
- Returns:
a
TypeError
with a descriptive information
>>> type_error(1.3, "var", int) TypeError("var should be an instance of int but is float, namely '1.3'.") >>> type_error("x", "z", (int, float)).args[0] "z should be an instance of any in {float, int} but is str, namely 'x'." >>> type_error("f", "q", call=True).args[0] "q should be a callable but is str, namely 'f'." >>> type_error("1", "2", bool, call=True).args[0] "2 should be an instance of bool or a callable but is str, namely '1'." >>> type_error(None, "x", str) TypeError('x should be an instance of str but is None.')
bookbuilderpy.url module¶
Loading of data from urls.
bookbuilderpy.version module¶
An internal file with the version of the bookbuilderpy package.
bookbuilderpy.versions module¶
Get the versions of all involved libraries and tools.
- bookbuilderpy.versions.TOOL_CALIBRE: Final[str] = 'calibre'¶
the name of the calibre executable tool
- bookbuilderpy.versions.TOOL_FIREFOX: Final[str] = 'firefox'¶
the name of the firefox browser executable tool
- bookbuilderpy.versions.TOOL_FIREFOX_DRIVER: Final[str] = 'geckodriver'¶
the name of the firefox driver tool
- bookbuilderpy.versions.TOOL_GHOSTSCRIPT: Final[str] = 'gs'¶
the name of the ghostscript executable tool
- bookbuilderpy.versions.TOOL_PDFLATEX: Final[str] = 'pdflatex'¶
the name of the pdflatex executable tool
- bookbuilderpy.versions.TOOL_RSVG_CONVERT: Final[str] = 'rsvg-convert'¶
the name of the rsvg-convert executable tool
- bookbuilderpy.versions.TOOL_XELATEX: Final[str] = 'xelatex'¶
the name of the xelatex executable tool
bookbuilderpy.website module¶
The routine for building the website for the book.