1 # Module with templates and tools for writing generated code,
2 # especially C source and headers
4 # 2011 - Tomas Gavenciak <gavento@ucw.cz>
9 from gfxprim
import die
11 def j2render(tmpl
, **kw
):
12 "Internal helper to render jinja2 templates (with StrictUndefined)"
13 t2
= tmpl
.rstrip('\n') # Jinja strips the last '\n', so add these later
14 return jinja2
.Template(t2
, undefined
=jinja2
.StrictUndefined
).render(**kw
) + tmpl
[len(t2
):]
16 def load_generators():
17 "Load all modules containig generators to allow them to register"
18 # TODO: write proper discovery
19 import gfxprim
.generators
.make_collected_tests
20 import gfxprim
.generators
.core
.make_GP_Pixel
21 import gfxprim
.generators
.core
.make_GP_Blit
24 def generate_file(fname
):
25 from gfxprim
.generators
.generator
import known_generators
26 "Function trying to generate file `fname` using matching known generator."
28 for k
in known_generators
:
29 if k
.matches_path(fname
):
32 die("No known generators match '%s'" % (fname
,))
34 die("Multiple generators match '%s': %s" % (fname
, str(matches
)))
35 s
= matches
[0].generate()
36 with
open(fname
, "wt") as f
:
40 "Helper returning hex mask for that many bits"
41 return hex((1<<bits
)-1).rstrip('L')