Common library for code generation
Project description
hmd-lib-mickey
Common library for code generation used in hmd-tf-mickey.
The library provides basic abstractions around building data contexts, downloading and using remote packages, and rendering Jinja templates.
It also provides mechanisms to provide pre/post-hooks to various parts of the generation process.
Basic Usage
There are two core classes required perform code generation, MickeyRunConfiguration and MickeyRun.
MickeyRunConfiguration represents the basic configuration of the current code generation run.
It contains properties for the context definitions, template paths to render, data contexts to use, etc.
The easiest way to build it is passing a simple Mickey transform instance context.
config = MickeyRunConfiguration(
project_name, # project repository name
{
"input": input_content_path,
"output": output_content_path,
"cache": cache_path,
}, # Dirs dictionary containing paths for finding inputs, where to output results, and cache location
transform_instance_context,
variables=transform_instance_context.get("variables", {}), # Additional variables to use
skip_deps=skip_deps, # Flad to download remote packages or not
hook_scripts=hooks, # Paths to hook scripts
)
The transform_instance_context has the following shape:
{
"context_definitions": {},
"template_packages": [],
"templates": [],
"contexts": [],
"hooks": []
}
A list of MickeyPhase instances and a name is passed to MickeyRun.
This represents the actual running of the code generator.
You can build your own set of phases, but it is probably easier to use the default set PHASES found in hmd_lib_mickey.phases.
from hmd_lib_mickey.types import MickeyRun
from hmd_lib_mickey.phases import PHASES
run = MickeyRun(PHASES, 'run')
The final step is to actually perform the code generation with the generate function.
from hmd_lib_mickey.hmd_lib_mickey import generate
generate(run, config)
Phase Hooks
The base class MickeyPhase has two decorator methods for extension via pre/post hooks.
The decorator methods expect a function that takes a MickeyRunConfiguration instance as its only argument and returns either the same instance or a mutated copy.
Example:
from hmd_lib_mickey.phases import BuildEnvironment
from hmd_lib_mickey.types import MickeyRunConfiguration
@BuildEnvironment.post_hook
def add_jinja_filters(config: MickeyRunConfiguration):
env = config.environment
# Mutate the Jinja Environment
# ...
return config.copy(environment=env)
The above example adds a post-hook to the BuildEnvironment phase to edit the standard Jinja Environment used.
You can use it to add filter functions to be used by templates for example.
Hooks can included in the code generation run by giving a list of paths to single Python scripts in the MickeyRunConfiguration.
Currently, you cannot import relative packages from the scripts because Mickey copies only the script to the cache location.
The MickeyRun will handle importing each listed script, which will cause any decorators used to run and register with the appropriate phase.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hmd_lib_mickey-0.1.77-py3-none-any.whl.
File metadata
- Download URL: hmd_lib_mickey-0.1.77-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
505aac079b3aa784e887e9ec253553c69a7dcc6a9b7639721f976c0fc891ccfa
|
|
| MD5 |
dfffcca2640421c2b648111b97b7c0c6
|
|
| BLAKE2b-256 |
889e4a42faa61de73077aed054e76ac99bef1766a45d561c58b1af88bcde198e
|