Reintroduce jinja2
This commit is contained in:
@@ -1,18 +1,29 @@
|
||||
import os
|
||||
import io
|
||||
from typing import List, Tuple
|
||||
import jinja2
|
||||
from typing import Union, Dict, Any
|
||||
|
||||
|
||||
FilesDict = Dict[str, Union[str, jinja2.Template]]
|
||||
|
||||
|
||||
class TemplateProject:
|
||||
files: List[Tuple[str, str]]
|
||||
default_files: FilesDict = {}
|
||||
files: FilesDict
|
||||
context: Dict[str, Any]
|
||||
|
||||
def __init__(self, files):
|
||||
self.files = files
|
||||
def __init__(self, *, extra_files: FilesDict):
|
||||
self.files = self.default_files.copy()
|
||||
self.files.update(extra_files)
|
||||
self.context = {}
|
||||
|
||||
def generate(self, path):
|
||||
for filename, content in self.files:
|
||||
for filename, content in self.files.items():
|
||||
file_path = os.path.join(path, filename)
|
||||
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
||||
|
||||
with io.open(file_path, 'w', encoding='utf8') as f:
|
||||
if isinstance(content, jinja2.Template):
|
||||
content = content.render(self.context)
|
||||
|
||||
f.write(content)
|
||||
|
||||
+31
-10
@@ -1,26 +1,30 @@
|
||||
import textwrap
|
||||
import jinja2
|
||||
from .base import TemplateProject
|
||||
|
||||
def indent(str, level):
|
||||
return str
|
||||
|
||||
def CTemplateProject(TemplateProject):
|
||||
files = {
|
||||
'spam.c': textwrap.dedent(fr'''
|
||||
class CTemplateProject(TemplateProject):
|
||||
default_files = {
|
||||
'spam.c': jinja2.Template(textwrap.dedent(r'''
|
||||
#include <Python.h>
|
||||
|
||||
{spam_c_top_level_add}
|
||||
{{ spam_c_top_level_add }}
|
||||
|
||||
static PyObject *
|
||||
spam_system(PyObject *self, PyObject *args)
|
||||
{{
|
||||
{
|
||||
const char *command;
|
||||
int sts;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "s", &command))
|
||||
return NULL;
|
||||
|
||||
sts = system(command);
|
||||
{indent(spam_c_function_add, 4)}
|
||||
|
||||
{{ spam_c_function_add | indent(4) }}
|
||||
|
||||
return PyLong_FromLong(sts);
|
||||
}}
|
||||
}
|
||||
|
||||
/* Module initialization */
|
||||
|
||||
@@ -56,5 +60,22 @@ def CTemplateProject(TemplateProject):
|
||||
|
||||
MOD_RETURN(m)
|
||||
}
|
||||
''')
|
||||
''')),
|
||||
'setup.py': jinja2.Template(textwrap.dedent(r'''
|
||||
from setuptools import setup, Extension
|
||||
|
||||
{{ setup_py_add }}
|
||||
|
||||
setup(
|
||||
name="spam",
|
||||
ext_modules=[Extension("spam", sources=["spam.c"])],
|
||||
version="0.1.0",
|
||||
{{ setup_py_setup_args_add | indent(4) }}
|
||||
)
|
||||
'''))
|
||||
}
|
||||
|
||||
def __init__(self, spam_c_top_level_add, spam_c_function_add, setup_py_add,
|
||||
setup_py_setup_args_add, extra_files):
|
||||
super().__init__(extra_files)
|
||||
self.context = {}
|
||||
|
||||
Reference in New Issue
Block a user