pydocmaker.templating
Attributes
Classes
A class used to manage templates, parameters, and attachments from a specified directory. |
|
A class used to represent a document template. |
Functions
|
Register a new template directory. |
|
Removes an existing template directory if it exists. |
|
Returns a list of registered template directories. |
|
tests if a template with a given id and optional in a given format exists |
|
This function retrieves the template IDs from the list of templates. |
Module Contents
- pydocmaker.templating.default_template_dir
- pydocmaker.templating.registered_template_dirs
- pydocmaker.templating.register_new_template_dir(new_template_dir: str, check_exists=True) bool
Register a new template directory.
- Parameters:
new_template_dir (str) – The path to the new template directory.
check_exists (bool, optional) – Whether to check if the directory exists. Defaults to True.
- Raises:
FileNotFoundError – If the directory does not exist and check_exists is True.
- Returns:
True if the directory was successfully registered, False otherwise.
- Return type:
bool
- pydocmaker.templating.remove_from_template_dir(to_remove: str) bool
Removes an existing template directory if it exists.
- Parameters:
to_remove (str) – The path to remove from the template dirs.
- Returns:
Always True
- Return type:
bool
- pydocmaker.templating.get_registered_template_dirs(include_default=True)
Returns a list of registered template directories.
- Parameters:
include_default (bool, optional) – Whether to include the default template directory. Defaults to True.
- Returns:
- A list of registered template directories. If include_default is True, the default
template directory is the first element in the list.
- Return type:
list
- pydocmaker.templating.test_template_exists(template_id, tformat='', template_dir=None)
tests if a template with a given id and optional in a given format exists
- Parameters:
template_id (str) – the template id to search for e.G. ‘base’
tformat (str, optional) – optinal the template format to look for (either ‘tex’, or ‘html’) if nothing is given the first found template with that name independent of the format is returned. Defaults to ‘’.
template_dir (str, optional) – if this is given only the given template directory is mounted to load jinja templates. Defaults to ‘’.
- Returns:
True if found False otherwise
- Return type:
bool
- pydocmaker.templating.get_available_template_ids(template_dir=None)
This function retrieves the template IDs from the list of templates.
- Returns:
A list of template IDs, which are the names of the templates without the file extension.
- Return type:
list
- class pydocmaker.templating.TemplateDirSource(template_dirs: List[str] = None)
A class used to manage templates, parameters, and attachments from a specified directory.
…
- template_dir
a string representing the directory path where the templates are located
- Type:
str
- env
an Environment object from the jinja2 library used to load templates
- Type:
Environment
- get_params(templates=None)
Returns a dictionary of parameters for the specified templates.
- get_templates()
Returns a dictionary of templates in the directory.
- get_template_ids()
Returns a list of template IDs.
- get_attachments(templates=None)
Returns a dictionary of attachments for the specified templates.
- get_all(load_params=True, load_attachments=True)
Returns a tuple containing dictionaries of templates, parameters, and attachments.
- resolve_template_id(my_template_id)
Returns the full template name corresponding to the given template ID.
- template_dirs = None
- env
- find_undeclared_variables(template)
Find undeclared variables in a template.
- Parameters:
template (str) – The template to analyze.
- Returns:
A set of undeclared variable names used in the template.
- Return type:
set
- get_params(templates=None) dict
Returns a dictionary of (default) parameters for the specified templates.
- Parameters:
templates (iterable str) – A dictionary of template params. If None, all templates are loaded.
- Returns:
A dictionary of parameters for the specified templates with dict[template_id, dict[param_name,param_value]].
- Return type:
dict
- get_templates() dict
Retrieves all the templates from the directory.
- Returns:
- A dictionary of templates where the keys are the template names
and the values are the corresponding Jinja2 Template objects.
- Return type:
dict
- get_template_ids()
This function retrieves the template IDs from the list of templates.
- Returns:
A list of template IDs, which are the names of the templates without the file extension.
- Return type:
list
- get_attachments(templates=None)
Get attachments from the template directory.
- Parameters:
templates (list or dict, optional) – A list or dictionary of templates. If None, all templates will be used. Defaults to None.
- Returns:
- A dictionary of attachments, where the keys are the template names
and the values are dictionaries of attachments for that template.
- Return type:
dict
- get_all_filenames()
Get all filenames in all template directories.
- get_all(load_params=True, load_attachments=True)
Get all templates, parameters, and attachments.
- Parameters:
load_params (bool, optional) – Whether to load parameters. Defaults to True.
load_attachments (bool, optional) – Whether to load attachments. Defaults to True.
- Returns:
A tuple containing templates, parameters, and attachments.
- Return type:
tuple
- resolve_template_id(my_template_id)
Resolve the template ID to the actual template file name.
- Parameters:
my_template_id (str) – The template ID to resolve.
- Returns:
The actual template file name.
- Return type:
str
- Raises:
KeyError – If the template ID is not found in the available templates.
- __contains__(template_id)
Check if a template_id is in any of the template directories.
- class pydocmaker.templating.DocTemplate(template, params=None, attachments=None, env=None, template_id=None)
A class used to represent a document template.
This class provides methods to load a template from a template directory, render the template with given parameters, and manage attachments.
- template
A string representation of the template.
- Type:
str
- params
A dictionary of parameters to be used in the template.
- Type:
dict
- attachments
A dictionary of attachments to be used in the template.
- Type:
dict
- env
An instance of the jinja2 Environment class.
- Type:
Environment
- static from_tid(template_id: str, tformat='', template_dir=None)
Load a template by supplying a template_id and possibly a template format
- Parameters:
template_id (str) – The ID of the template to load e.G. “base”
tformat (str, optional) – optinal the template format to get e.G. “.tex” or “.html”. Defaults to ‘’ which means first of any format being found.
template_dir (str, optional) – The directory containing the templates. If not provided, the default template directory is used.
- Returns:
An instance of the DocTemplate class.
- Return type:
- static test_tid_exists(template_id: str, tformat='', template_dir=None)
- static get_available_tids(template_dir=None)
- template
- params = None
- attachments = None
- env
- template_id = None
- property tformat
- __str__()
- __repr__()
- find_undeclared_variables()
Find undeclared variables in the template.
- Returns:
A set of undeclared variable names used in the template.
- Return type:
set
- render(**kwargs)
Render the Jinja2 template with given parameters.
- Parameters:
**kwargs – Additional parameters to be used in the template.
- Returns:
The rendered template as a string.
- Return type:
str
- pydocmaker.templating.res = False