jw-pkg/src/python/jw/pkg/cmds/projects/lib/templates.py

73 lines
1.8 KiB
Python
Raw Normal View History

import textwrap
def format_lines(template, values, li_quote, li_delimiter):
def __format_value(val):
if not li_quote:
return str(val)
return f'"{val}"'
for key, value in values.items():
if isinstance(value, (list, tuple)):
values[key] = li_delimiter.join(map(__format_value, value))
ret = []
parts = template.splitlines(keepends = True)
for line in parts:
for key, value in values.items():
marker = '{' + key + '}'
if marker in line:
indent = line[:line.index(marker)]
value = str(value).replace('\n', '\n' + indent)
line = line.replace(marker, value)
ret.append(line)
return ''.join(ret)
_templates = {
'pkg-config':
"""\
prefix = {prefix}
exec_prefix = {{prefix}}
includedir = {{prefix}}/include
libdir = {{exec_prefix}}/lib
Name: {name}
Description: {description}
Version: {version}
""",
'pyrightconfig.json':
"""\
{
"extends": {base},
"include": [
{include}
],
"exclude": [
"**/__pycache__",
"**/.pytest_cache",
"**/.mypy_cache",
"**/.ruff_cache",
"**/.venv",
"**/build",
"**/dist"
],
"extraPaths": [
{extra_paths}
],
"typeCheckingMode": "basic",
"pythonPlatform": "Linux"
}
""",
}
def tmpl_render(
template_name: str, values, li_quote = False, li_delimiter = '\n'
) -> str:
return format_lines(
textwrap.dedent(_templates[template_name]),
values,
li_quote = li_quote,
li_delimiter = li_delimiter,
)