Peano
Loading...
Searching...
No Matches
Jinja2TemplateStringHeaderImplementationFilePair.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
13from ..Logger import Logger
14
16 """!
17Represents a pair of C++ header and implementation files
18
19Same idea as Jinja2TemplatedHeaderImplementationFilePair, but the two
20templates are handed over as plain strings rather than as fully qualified
21file names. This is convenient if the template content is assembled/built
22dynamically in Python rather than read from a file on disk.
23 """
24
25 def __init__(self,
26 headerfile_template,
27 cppfile_template,
28 classname,
29 namespace,
30 subdirectory,
31 dictionary,
32 default_overwrite=True,
33 apply_iteratively=False):
34 """
35 headerfile_template and cppfile_template are strings holding the actual
36 Jinja2 template content (not file names).
37
38 classname is a string
39 namespace is a (possibly empty) list of strings
40
41 cppfile_template can be None
42 """
43 self.headerfile_template = headerfile_template
44 self.cppfile_template = cppfile_template
45 self.classname = classname
46 self.namespace = namespace
47 self.subdirectory = subdirectory
48 self.default_overwrite = default_overwrite
49 self.apply_iteratively = apply_iteratively
50
51 self.d = deepcopy(dictionary)
52 self.d[ "CLASSNAME" ] = classname
53 self.d[ "NAMESPACE" ] = []
54 for i in namespace:
55 self.d["NAMESPACE"].append(i)
56 # Eliminate the prepending "::" that can lead to compatibility issues across the solvers
57 self.d["FULL_QUALIFIED_NAMESPACE"] = "::".join(namespace)
58
59 def __generate_file(self,overwrite,full_qualified_filename,template_string):
60 if template_string!=None and write_file(overwrite,self.default_overwrite,full_qualified_filename):
61 Logger().log( "write " + full_qualified_filename )
62
63 templateEnv = jinja2.Environment(undefined=jinja2.DebugUndefined)
64 template = templateEnv.from_string( template_string )
65
66 if self.apply_iteratively:
67 old_size = 0
68 rendered_text = template.render(self.d)
69 while "{{" in rendered_text and len(rendered_text)!=old_size:
70 old_size = len(rendered_text)
71 template = jinja2.Template(rendered_text, undefined=jinja2.DebugUndefined)
72 rendered_text = template.render( self.d )
73
74 rendered_text = template.render(self.d)
75 if self.default_overwrite:
76 rendered_text = WARNING_BANNER + rendered_text
77
78 with open( full_qualified_filename, "w" ) as output:
79 output.write( rendered_text )
80
81 def generate(self,overwrite,directory):
82 subdirectory_path = os.path.normpath( directory + "/" + self.subdirectory )
83 if not os.path.exists( subdirectory_path ):
84 os.mkdir(subdirectory_path)
85
86 header_filename = os.path.normpath( subdirectory_path + "/" + self.classname + ".h" )
87 cpp_filename = os.path.normpath( subdirectory_path + "/" + self.classname + ".cpp" )
88
89 self.__generate_file(overwrite,header_filename,self.headerfile_template)
90 self.__generate_file(overwrite,cpp_filename ,self.cppfile_template)
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37
__init__(self, headerfile_template, cppfile_template, classname, namespace, subdirectory, dictionary, default_overwrite=True, apply_iteratively=False)
headerfile_template and cppfile_template are strings holding the actual Jinja2 template content (not ...