Peano
Loading...
Searching...
No Matches
Jinja2TemplatedHeaderFile.py
Go to the documentation of this file.
1# This file is part of the Peano project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import os
4import re
5import jinja2
6
7from copy import deepcopy
8
9from .Helper import write_file, WARNING_BANNER
10
11from .Overwrite import Overwrite
12
14 def __init__(self,headerfile_template,classname,namespace,subdirectory,dictionary,default_overwrite=True,apply_iteratively=False,announce=True):
15 """
16 The template files should be fully qualified
17
18 classname is a string
19 namespace is a (possibly empty) list of strings
20 dictionary a series of mappings (cmp. Jinja2 convention)
21 announce controls whether generate() prints a "write <file>" line.
22 Set it to False if the caller reports the write itself (e.g. as
23 part of a combined header/implementation message).
24 """
25 self.headerfile_template = headerfile_template
26 self.classname = classname
27 self.namespace = namespace
28 self.subdirectory = subdirectory
29 self.default_overwrite = default_overwrite
30 self.apply_iteratively = apply_iteratively
31 self.announce = announce
32
33 self.d = deepcopy(dictionary)
34 self.d[ "CLASSNAME" ] = classname
35 self.d[ "NAMESPACE" ] = []
36 for i in namespace:
37 self.d["NAMESPACE"].append(i)
38 # Eliminate the prepending "::" that can lead to compatibility issues across the solvers
39 self.d["FULL_QUALIFIED_NAMESPACE"] = "::".join(namespace)
40
41 def __generate_file(self,overwrite,full_qualified_filename,template_file):
42 """
43 template_file: string
44 Usually, this is an absolute file name.
45 """
46 if template_file!=None and write_file(overwrite,self.default_overwrite,full_qualified_filename):
47 if self.announce:
48 print( "write " + full_qualified_filename )
49
50 template_loader = jinja2.FileSystemLoader(searchpath=os.path.split(template_file)[0])
51 templateEnv = jinja2.Environment(loader=template_loader, undefined=jinja2.DebugUndefined)
52 template = templateEnv.get_template( os.path.split(template_file)[1] )
53
54 if self.apply_iteratively:
55 rendered_text = template.render(self.d)
56 while "{{" in rendered_text and len(rendered_text)!=old_size:
57 old_size = len(rendered_text)
58 template = jinja2.Template(rendered_text, undefined=jinja2.DebugUndefined)
59 rendered_text = template.render( self.d )
60
61 rendered_text = template.render(self.d)
62 if self.default_overwrite:
63 rendered_text = WARNING_BANNER + rendered_text
64
65 with open( full_qualified_filename, "w" ) as output:
66 output.write( rendered_text )
67
68 def generate(self,overwrite,directory):
69 if not os.path.exists( directory + "/" + self.subdirectory ):
70 os.mkdir(directory + "/" + self.subdirectory)
71
72 header_filename = directory + "/" + self.subdirectory + "/" + self.classname + ".h"
73
74 self.__generate_file(overwrite,header_filename,self.headerfile_template)
__init__(self, headerfile_template, classname, namespace, subdirectory, dictionary, default_overwrite=True, apply_iteratively=False, announce=True)
The template files should be fully qualified.
__generate_file(self, overwrite, full_qualified_filename, template_file)
template_file: string Usually, this is an absolute file name.