Skip to content

Commit

Permalink
No need for mako or jinja2 for such simple templating
Browse files Browse the repository at this point in the history
  • Loading branch information
paugier committed Feb 2, 2024
1 parent 1e2a45b commit 910b562
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 47 deletions.
1 change: 0 additions & 1 deletion .github/environment-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name:
test
dependencies:
- cxx-compiler
- mako
- transonic
- pythran
- cython
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Click on the links to know more:
P3DFFT, PFFT (for 3D solvers) either using a package manager or
[from
source](https://fluidfft.readthedocs.io/en/latest/install/fft_libs.html)
3. Python packages `fluiddyn mako cython pyfftw pythran mpi4py`
3. Python packages `fluiddyn cython pyfftw pythran mpi4py`
4. [A C++11 compiler and BLAS
libraries](https://github.com/serge-sans-paille/pythran#installation)
and
Expand Down
2 changes: 1 addition & 1 deletion docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cleanimages:
@echo "Clean dangling images with no tag."
for img in $$(docker images -qf "dangling=true"); do docker rmi -f $$img; done

cleanall: cleancontainers cleanimages cleanmako
cleanall: cleancontainers cleanimages

run:
docker run --name $(image) --restart always -it fluiddyn/$(image) bash
12 changes: 6 additions & 6 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_function_code(lines):

def create_fake_mod(dimension):
with open(
os.path.join(here, "template{dim}d_mako.pyx".format(dim=dimension)), "r"
os.path.join(here, "template{dim}d.pyx".format(dim=dimension)), "r"
) as f:
lines_text = f.read().splitlines()

Expand Down
40 changes: 20 additions & 20 deletions plugins/fluidfft-builder/fluidfft_builder/make_cy_files.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import os
from datetime import datetime
from importlib import resources
from pathlib import Path

from jinja2 import Environment, PackageLoader
from string import Template


def load_template(filename):
"""Load template file using Jinja2.
"""Load template file.
Parameters
----------
Expand All @@ -17,17 +17,16 @@ def load_template(filename):
Returns
-------
jinja2.Template object
A `string.Template` object
"""

env = Environment(
loader=PackageLoader("fluidfft_builder", "templates"),
# undefined=jinja2.StrictUndefined,
keep_trailing_newline=True,
)
resource = resources.files("fluidfft_builder.templates")

with resources.as_file((resource / filename)) as file:
txt = file.read_text()

return env.get_template(filename)
return Template(txt)


def modification_date(filename):
Expand All @@ -52,21 +51,22 @@ def make_file(path_output, class_name, numpy_api="numpy"):

template = load_template(template_name)

content = template.substitute(
{
"module_name": module_name,
"class_name": class_name,
"numpy_api": numpy_api,
}
)

if not path_output.exists():
hastomake = True
else:
hastomake = modification_date(path_output) < modification_date(
template.filename
)
content_old = path_output.read_text(encoding="utf8")
hastomake = content != content_old

if hastomake:
path_output.write_text(
template.render(
module_name=module_name,
class_name=class_name,
numpy_api=numpy_api,
)
)
path_output.write_text(content, encoding="utf8")


def main():
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ cdef extern from "base_fft.h":
ctypedef struct mycomplex:
pass

cdef extern from "{{ module_name }}.h":
cdef cppclass {{ class_name }}:
cdef extern from "${module_name}.h":
cdef cppclass ${class_name}:
int test()
void bench(int, double*)

Expand All @@ -17,7 +17,7 @@ cdef extern from "{{ module_name }}.h":
void get_shapeX_seq(int*, int*)
void get_shapeK_seq(int*, int*)

{{ class_name }}(int, int) except +
${class_name}(int, int) except +

void destroy()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
include 'base.pyx'


from fft2d_{{ module_name }} cimport (
{{ class_name }} as mycppclass,
from fft2d_${module_name} cimport (
${class_name} as mycppclass,
mycomplex)


cdef class {{ class_name }}:
cdef class ${class_name}:
"""Perform Fast Fourier Transform in 2d.
Parameters
Expand Down Expand Up @@ -65,7 +65,7 @@ cdef class {{ class_name }}:
@property
def _numpy_api(self):
"""A ``@property`` which imports and returns a NumPy-like array backend."""
import {{ numpy_api }} as np
import ${numpy_api} as np
return np

def get_short_name(self):
Expand Down Expand Up @@ -306,4 +306,4 @@ cdef class {{ class_name }}:
field.fill(value)
return field

FFTclass = {{ class_name }}
FFTclass = ${class_name}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ cdef extern from "base_fft.h":
ctypedef struct mycomplex:
pass

cdef extern from "{{ module_name }}.h":
cdef cppclass {{ class_name }}:
cdef extern from "${module_name}.h":
cdef cppclass ${class_name}:
int test()
void bench(int, double*)

Expand All @@ -21,7 +21,7 @@ cdef extern from "{{ module_name }}.h":
void get_seq_indices_first_X(int*, int*, int*)
void get_seq_indices_first_K(int*, int*, int*)

{{ class_name }}(int, int, int) except +
${class_name}(int, int, int) except +

void destroy()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
include 'base.pyx'


from fft3d_{{ module_name }} cimport (
{{ class_name }} as mycppclass,
from fft3d_${module_name} cimport (
${class_name} as mycppclass,
mycomplex)


Expand All @@ -32,7 +32,7 @@ def compute_k_adim_seq(nk, axis, dim_first_fft=2):
return np.r_[0:k_adim_max+1, k_adim_min:0]


cdef class {{ class_name }}:
cdef class ${class_name}:
"""Perform Fast Fourier Transform in 3D.
Parameters
Expand Down Expand Up @@ -95,7 +95,7 @@ cdef class {{ class_name }}:
@property
def _numpy_api(self):
"""A ``@property`` which imports and returns a NumPy-like array backend."""
import {{ numpy_api }} as np
import ${numpy_api} as np
return np

def get_short_name(self):
Expand Down Expand Up @@ -493,4 +493,4 @@ cdef class {{ class_name }}:
field.fill(value)
return field

FFTclass = {{ class_name }}
FFTclass = ${class_name}
1 change: 0 additions & 1 deletion plugins/fluidfft-builder/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ description = "Fluidfft plugin dependencies"
authors = [{name = "Pierre Augier", email = "pierre.augier@univ-grenoble-alpes.fr"}]
license = {file = "LICENSE"}
classifiers = ["License :: OSI Approved :: MIT License"]
dependencies = ["jinja2"]

[project.urls]
Home = "https://foss.heptapod.net/fluiddyn/fluidfft"
Expand Down

0 comments on commit 910b562

Please sign in to comment.