nmk_base.common¶
Python module for nmk-base utility classes and builders
Classes¶
Generic builder logic to generate files from templates |
|
Generic builder logic to generate TOML files from templates and contributed items |
|
Generic builder logic to create directory |
|
Generic builder logic to call a sub-process |
|
Generic builder logic to clean a directory |
|
Generic builder logic to download a file from a URL |
Module Contents¶
- class nmk_base.common.TemplateBuilder(model: nmk.model.model.NmkModel)¶
Bases:
nmk.model.builder.NmkTaskBuilderGeneric builder logic to generate files from templates
- relative_path(v: str) str¶
Make an absolute path as project relative if possible
- Parameters:
v – Path string to be converted
- Returns:
Project relative path (if possible); unchanged input value otherwise
- config_value(config_name: str) Any¶
Get config value by name & turn absolute paths to project relative ones (if possible)
- Parameters:
config_name – Config item name
- Returns:
Config item value
- render_template(template: pathlib.Path, kwargs: dict[str, str]) str¶
Render template into a string, with provided keywords and config items
- Parameters:
template – Path to template file to be rendered
kwargs – Map of keywords for templates rendering, indexed by name
- Returns:
Rendered template string
- Throw:
AssertionError if unknown keyword is referenced in template
- build_from_template(template: pathlib.Path, output: pathlib.Path, kwargs: dict[str, str], file_updated_info: str | None = None) str¶
Generate file from template
- Parameters:
template – Path to template file to be rendered
output – Path to output file to be generated
kwargs – Map of keywords for templates rendering, indexed by name
file_updated_info – Config item name where to store information that output file has been updated
- Returns:
Rendered template string
- Throw:
AssertionError if unknown keyword is referenced in template
- build(template: str, kwargs: dict[str, str] | None = None, file_updated_info: str | None = None)¶
Default build behavior: generate main output file from provided template
- Parameters:
template – Path to the Jinja template to use for generation
kwargs – Map of keywords for templates rendering, indexed by name
file_updated_info – Config item name where to store information that output file has been updated
- class nmk_base.common.TomlFileBuilder(model: nmk.model.model.NmkModel)¶
Bases:
TemplateBuilderGeneric builder logic to generate TOML files from templates and contributed items
- build(fragment_files: list[str], items: dict, plugin_name: str = 'nmk-base', kwargs: dict[str, str] = None)¶
Generates toml file from fragments and items
- Parameters:
fragment_files – List of fragment files (processed as Jinja templates) to be merged
items – Dict of toml items to be merged; only non-empty items are considered (no empty section will be added)
plugin_name – Plugin name to be inserted in generated file heading comment
kwargs – Map of keywords for templates rendering, indexed by name
- class nmk_base.common.MkdirBuilder(model: nmk.model.model.NmkModel)¶
Bases:
nmk.model.builder.NmkTaskBuilderGeneric builder logic to create directory
- build()¶
Build logic: create specified directory (main output of the task)
- class nmk_base.common.ProcessBuilder(model: nmk.model.model.NmkModel)¶
Bases:
nmk.model.builder.NmkTaskBuilderGeneric builder logic to call a sub-process
- build(cmd: str | list[str], verbose: bool = False)¶
Build logic:
call subprocess specified through cmd parameter; process is invoked in project directory
depending on the verbose parameter, redirect output to stdout (if True) or to nmk logs (if False)
touch the specified output file
- Parameters:
cmd – process command line; may be a string or a list of parameters
verbose – states if the process output shall be displayed in stdout or saved in logs
- class nmk_base.common.CleanBuilder(model: nmk.model.model.NmkModel)¶
Bases:
nmk.model.builder.NmkTaskBuilderGeneric builder logic to clean a directory
- build(path: str)¶
Build logic: delete (recursively) provided directory, if it exists
- Parameters:
path – Directory to be deleted
- class nmk_base.common.DownloadBuilder(model: nmk.model.model.NmkModel)¶
Bases:
nmk.model.builder.NmkTaskBuilderGeneric builder logic to download a file from a URL
- download(url: str, target: pathlib.Path, request_function: collections.abc.Callable[[str], requests.Response], request_kwargs: dict[str, Any] | None = None, extract: bool = False)¶
Download file from provided URL and save it as main output
- Parameters:
url – URL to download file from
target – Path to save/extract the downloaded file
request_function – Function to use for the HTTP request. The function must behave like requests.get
request_kwargs – Map of extra keyword arguments to be passed to the request function
extract – Whether to extract the downloaded file if it’s an archive (zip, tar.gz, or others). In this case, the main output is expected to be a directory instead of a file, and the downloaded file will be extracted into it.
- build(url: str, request_function: str = 'requests.get', request_kwargs: dict[str, Any] | None = None, extract: bool = False)¶
Build logic: download file from provided URL and save it as main output
- Parameters:
url – URL to download file from
request_function – Function name to import and use for the HTTP request. The function must behave like requests.get; default is requests.get.
request_kwargs – Map of extra keyword arguments to be passed to the request function
extract – Whether to extract the downloaded file if it’s an archive (zip, tar.gz, or others). In this case, the main output is expected to be a directory instead of a file, and the downloaded file will be extracted into it.