tempylate package#
Submodules#
tempylate.builtins module#
- tempylate.builtins.include(path: str, __tempylate_cached: bool = False) str #
Include other file.
- Parameters
path – The path to a file.
Notes: Use the last modified date of the file to cache it.
- tempylate.builtins.aioinclude(path: str, *args: Any, **kwargs: Any) Coroutine[Any, Any, str] #
This is an asynchronous version of
tempylate.builtins.include()
. This is using theutils.run_in_executor()
.- Parameters
path – The path to a file.
*args – Argument passed to
tempylate.utils.run_in_executor()
followingfunction
.**kwargs – Keyword arguments to be passed to
tempylate.utils.run_in_executor()
.
- tempylate.builtins.escape(s, quote=True)#
Replace special characters “&”, “<” and “>” to HTML-safe sequences. If the optional flag quote is true (the default), the quotation mark characters, both double quote (”) and single quote (’) characters are also translated.
- tempylate.builtins.CS = '^^'#
This is just a constant with two caret signs in it. Use this when you want to use two caret signs side by side in a string defined in the Python code in the block.
- tempylate.builtins.builtins: dict[str, collections.abc.Callable[..., Any]] = {'CS': '^^', 'aioinclude': <function aioinclude>, 'escape': <function escape>, 'include': <function include>, 'textwrap': <module 'textwrap' from '/home/docs/.asdf/installs/python/3.10.4/lib/python3.10/textwrap.py'>}#
A dictionary of what is in
tempylate.builtins
.
tempylate.exceptions module#
- exception tempylate.exceptions.TempylateError#
Bases:
Exception
Tempylate error base error.
- exception tempylate.exceptions.LoadBlockError#
Bases:
TempylateError
This error occurs when Tempylate fails to load a block.
tempylate.manager module#
- class tempylate.manager.Manager(builtins: Optional[dict[str, Any]] = None, adjustors: Any]]] = None, cls: Optional[type[+TemplateT]] = None, loop: Optional[AbstractEventLoop] = None, executor: Optional[Any] = None)#
Bases:
Generic
[TemplateT
]This class manages templates. Rendering with it will save cache and thus improve operating speed.
- Parameters
builtins – A dictionary containing the names and values of variables that are available by default in the template.
adjustors – This function is called each time a template is rendered. The function is passed an instance (
self
) of the templatetempylate.template.Template
and a dictionary containing the names and values of variables to pass to the template.cls – Template class used for template rendering. Defaults to
tempylate.template.Template
.loop – Event loop used for asynchronous rendering.
executor – An executor used for asynchronous rendering.
- render(raw: str, template_name: str, **kwargs: Any) str #
Renders the passed template.
- Parameters
raw – Template string.
template_name – The name of the template to be rendered. This is the name to be assigned to the cache and must be unique for each template.
**kwargs – Keyword argument passed to
tempylate.template.Template.render()
.
- async aiorender(raw: str, template_name: str, *args, **kwargs: Any) str #
This is an asynchronous version of
render()
.- Parameters
raw – Template string.
template_name – The name of the template to be rendered. This is the name to be assigned to the cache and must be unique for each template.
*args – Argument passed to
tempylate.template.Template.aiorender()
.**kwargs – Keyword argument passed to
tempylate.template.Template.aiorender()
.
- render_from_file(path: str, **kwargs: Any) str #
Render the template from file.
- Parameters
path – The path to the template file.
**kawrgs – Keyword argument passed to
render()
.
- async aiorender_from_file(path: str, *args, aioinclude_args: Iterable[Any] = (), aioinclude_kwargs: Optional[dict[str, Any]] = None, **kwargs: Any) str #
This is an asynchronous version of
render_from_file()
.- Parameters
path – The path to the template file.
*args – Arguments passed to
tempylate.builtins.aioinclude()
.**kwargs – Keyword argument passed to
aiorender()
.
tempylate.template module#
- tempylate.template.extract_texts(template: str) Iterator[tuple[tuple[int, int], bool, str]] #
Extract a block and text of template from a string.
- Parameters
template – Target text.
- Yields
This is an integer indicating how many lines are the first and last of the extracted string, a boolean indicating whether it is a block, and a tuple of the body text.
- class tempylate.template.Template(raw: str, builtins: dict[str, Any] | None = None, manager: Manager[SelfT] | None = None, loop: asyncio.AbstractEventLoop | None = None, executor: Any = None)#
Bases:
object
Class for storing template strings. This class can be used to render templates.
- Parameters
raw – The template string.
builtins – A dictionary containing the names and values of built-in variables that can be used from the beginning at rendering time.
manager – Instance of the Manager class. This argument is used when creating a template from Manager and does not have to be used.
loop – Event Loop.
executor – The executor to use with
loop.run_in_executor
.
- raw: str#
Template string.
- builtins: dict[str, Any]#
A dictionary containing variables that can be used by default in the template.
- blocks: dict[str, BlockFunction]#
A dictionary in which the functions of the block are stored.
- loop: asyncio.AbstractEventLoop | None = None#
Event Loop.
- executor: Any = None#
The executor to use with
loop.run_in_executor
.
- prepare(args: Iterable[str] = (), template_name: Optional[str] = None, async_mode: bool = False) None #
Prepare a template. Running this will store the blocks in the template as a function in
Template.blocks
. This can be done all at once.- Parameters
args – An iterable that returns the names of variables that can be used in a block of templates.
template_name – The name to be displayed in case of an error. If not specified,
"<unknown>"
is used.async_mode – Whether the function of the block to be generated should be a coroutine function or not.
Notes
This is done automatically when
Template.render()
orTemplate.aiorender()
is executed.- Raises
LoadBlockError – Occurs when preparing fails.
- render(template_name: Optional[str] = None, **kwargs: Any) str #
Renders the template.
- Parameters
template_name – The name of the template.
**kwargs – A dictionary of names and values of variables to be passed to the template.
Notes: If you want to use a variable named
self
in your template, pass the value tokwargs
with the name__self__
.
- async aiorender(template_name: Optional[str] = None, load_block_run_in_executor: bool = True, executor: Optional[Any] = None, **kwargs: Any) str #
Asynchronous template rendering.
- Parameters
load_block_run_in_executor – Whether the block should be loaded using
loop.run_in_executor
. If your blocks are often huge and complex, you may want to enable this. This is because it may take longer to load the blocks.executor – Used in
executor
when the argumentload_block_run_in_executor
isTrue
.template_name – The name of the template.
**kwargs – A dictionary of names and values of variables to be passed to the template.
- execute(block_name: str, **kwargs: Any) str #
Execute another block.
- Parameters
block_name – The name of the block.
**kwargs – The keyword arguments to be passed to the block function.
- Raises
KeyError – Occurs when a block is not found.
tempylate.types module#
- tempylate.types.BlockFunction#
The function type of the block.
tempylate.utils module#
- async tempylate.utils.run_in_executor(function: Callable[[], ReT], loop: Optional[AbstractEventLoop] = None, executor: Optional[Any] = None) ReT #
Execute the passed function in an asynchronous event loop
run_in_executor
.- Parameters
function – Function to execute.
loop – The event loop to be used. If not specified,
asyncio.get_running_loop
is used to automatically get the event loop.executor – The executor to be used.
Module contents#
- class tempylate.Template(raw: str, builtins: dict[str, Any] | None = None, manager: Manager[SelfT] | None = None, loop: asyncio.AbstractEventLoop | None = None, executor: Any = None)#
Bases:
object
Class for storing template strings. This class can be used to render templates.
- Parameters
raw – The template string.
builtins – A dictionary containing the names and values of built-in variables that can be used from the beginning at rendering time.
manager – Instance of the Manager class. This argument is used when creating a template from Manager and does not have to be used.
loop – Event Loop.
executor – The executor to use with
loop.run_in_executor
.
- raw: str#
Template string.
- builtins: dict[str, Any]#
A dictionary containing variables that can be used by default in the template.
- blocks: dict[str, BlockFunction]#
A dictionary in which the functions of the block are stored.
- loop: asyncio.AbstractEventLoop | None = None#
Event Loop.
- executor: Any = None#
The executor to use with
loop.run_in_executor
.
- prepare(args: Iterable[str] = (), template_name: Optional[str] = None, async_mode: bool = False) None #
Prepare a template. Running this will store the blocks in the template as a function in
Template.blocks
. This can be done all at once.- Parameters
args – An iterable that returns the names of variables that can be used in a block of templates.
template_name – The name to be displayed in case of an error. If not specified,
"<unknown>"
is used.async_mode – Whether the function of the block to be generated should be a coroutine function or not.
Notes
This is done automatically when
Template.render()
orTemplate.aiorender()
is executed.- Raises
LoadBlockError – Occurs when preparing fails.
- render(template_name: Optional[str] = None, **kwargs: Any) str #
Renders the template.
- Parameters
template_name – The name of the template.
**kwargs – A dictionary of names and values of variables to be passed to the template.
Notes: If you want to use a variable named
self
in your template, pass the value tokwargs
with the name__self__
.
- async aiorender(template_name: Optional[str] = None, load_block_run_in_executor: bool = True, executor: Optional[Any] = None, **kwargs: Any) str #
Asynchronous template rendering.
- Parameters
load_block_run_in_executor – Whether the block should be loaded using
loop.run_in_executor
. If your blocks are often huge and complex, you may want to enable this. This is because it may take longer to load the blocks.executor – Used in
executor
when the argumentload_block_run_in_executor
isTrue
.template_name – The name of the template.
**kwargs – A dictionary of names and values of variables to be passed to the template.
- execute(block_name: str, **kwargs: Any) str #
Execute another block.
- Parameters
block_name – The name of the block.
**kwargs – The keyword arguments to be passed to the block function.
- Raises
KeyError – Occurs when a block is not found.
- class tempylate.Manager(builtins: Optional[dict[str, Any]] = None, adjustors: Any]]] = None, cls: Optional[type[+TemplateT]] = None, loop: Optional[AbstractEventLoop] = None, executor: Optional[Any] = None)#
Bases:
Generic
[TemplateT
]This class manages templates. Rendering with it will save cache and thus improve operating speed.
- Parameters
builtins – A dictionary containing the names and values of variables that are available by default in the template.
adjustors – This function is called each time a template is rendered. The function is passed an instance (
self
) of the templatetempylate.template.Template
and a dictionary containing the names and values of variables to pass to the template.cls – Template class used for template rendering. Defaults to
tempylate.template.Template
.loop – Event loop used for asynchronous rendering.
executor – An executor used for asynchronous rendering.
- render(raw: str, template_name: str, **kwargs: Any) str #
Renders the passed template.
- Parameters
raw – Template string.
template_name – The name of the template to be rendered. This is the name to be assigned to the cache and must be unique for each template.
**kwargs – Keyword argument passed to
tempylate.template.Template.render()
.
- async aiorender(raw: str, template_name: str, *args, **kwargs: Any) str #
This is an asynchronous version of
render()
.- Parameters
raw – Template string.
template_name – The name of the template to be rendered. This is the name to be assigned to the cache and must be unique for each template.
*args – Argument passed to
tempylate.template.Template.aiorender()
.**kwargs – Keyword argument passed to
tempylate.template.Template.aiorender()
.
- render_from_file(path: str, **kwargs: Any) str #
Render the template from file.
- Parameters
path – The path to the template file.
**kawrgs – Keyword argument passed to
render()
.
- async aiorender_from_file(path: str, *args, aioinclude_args: Iterable[Any] = (), aioinclude_kwargs: Optional[dict[str, Any]] = None, **kwargs: Any) str #
This is an asynchronous version of
render_from_file()
.- Parameters
path – The path to the template file.
*args – Arguments passed to
tempylate.builtins.aioinclude()
.**kwargs – Keyword argument passed to
aiorender()
.