Added script for generating modules.
This commit is contained in:
@@ -49,6 +49,13 @@ def prompt(prefix: str = '> ') -> str:
|
||||
return sys.stdin.readline()
|
||||
|
||||
|
||||
def prompt_and_validate(message: str, validator: _Validator[str], prefix: str = '> ') -> str:
|
||||
while True:
|
||||
print(message)
|
||||
input = prompt(prefix)
|
||||
if validator(input):
|
||||
return input
|
||||
|
||||
def prompt_choices[T](message: str, choices: dict[str, T], prefix: str = '> ') -> T:
|
||||
while True:
|
||||
print(f'{message} [{", ".join(choices.keys())}]')
|
||||
|
||||
@@ -34,11 +34,16 @@ def make_env(filters: Optional[dict[str, Callable]] = None,
|
||||
return jinja_env
|
||||
|
||||
|
||||
def generate_file(template: str, target: Path, context: dict[str, Any], **kwargs) -> None:
|
||||
def generate_file(template_name: str, target: Path, context: dict[str, Any], **kwargs) -> None:
|
||||
jinja_env = make_env(**kwargs)
|
||||
template = jinja_env.get_template(template)
|
||||
template = jinja_env.get_template(template_name)
|
||||
result = template.render(**context)
|
||||
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
with target.open('w') as f:
|
||||
f.write(result)
|
||||
|
||||
def render_string(input: str, context: dict[str, Any], **kwargs) -> str:
|
||||
jinja_env = make_env(**kwargs)
|
||||
template = jinja_env.from_string(input)
|
||||
return template.render(**context)
|
||||
|
||||
Reference in New Issue
Block a user