Peano
Loading...
Searching...
No Matches
Jinja2TemplatedHeaderImplementationFilePair.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
10
11from .Overwrite import Overwrite
12
14 """!
15Represents a pair of C++ header and implementation files
16
17The content of these files is represented by jinja2 templates. While
18sorting out these templates, the class defines a couple of default
19dictionary entries besides the ones manually handed over by the user.
20 """
21
22 def __init__(self,
23 headfile_template,
24 cppfile_template,
25 classname,
26 namespace,
27 subdirectory,
28 dictionary,
29 default_overwrite=True,
30 apply_iteratively=False):
31 """
32 The template files should be fully qualified
33 classname is a string
34 namespace is a (possibly empty) list of strings
35
36 cppfile_template can be None
37 """
38 self.headerfile_template = headfile_template
39 self.cppfile_template = cppfile_template
40 self.classname = classname
41 self.namespace = namespace
42 self.subdirectory = subdirectory
43 self.default_overwrite = default_overwrite
44 self.apply_iteratively = apply_iteratively
45
46 self.d = deepcopy(dictionary)
47 self.d[ "CLASSNAME" ] = classname
48 self.d[ "NAMESPACE" ] = []
49 for i in namespace:
50 self.d["NAMESPACE"].append(i)
51 # Eliminate the prepending "::" that can lead to compatibility issues across the solvers
52 self.d["FULL_QUALIFIED_NAMESPACE"] = "::".join(namespace)
53
54 def __generate_file(self,overwrite,full_qualified_filename,template_file):
55 if template_file!=None and write_file(overwrite,self.default_overwrite,full_qualified_filename):
56 import inspect
57 print( "{} written by {} (from template {})".format(full_qualified_filename, os.path.basename(inspect.getfile(self.__class__)), template_file))
58
59 template_loader = jinja2.FileSystemLoader(searchpath=os.path.split(template_file)[0])
60 templateEnv = jinja2.Environment(loader=template_loader, undefined=jinja2.DebugUndefined)
61 template = templateEnv.get_template( os.path.split(template_file)[1] )
62
63 if self.apply_iteratively:
64 old_size = 0
65 rendered_text = template.render(self.d)
66 while "{{" in rendered_text and len(rendered_text)!=old_size:
67 old_size = len(rendered_text)
68 template = jinja2.Template(rendered_text, undefined=jinja2.DebugUndefined)
69 rendered_text = template.render( self.d )
70
71 with open( full_qualified_filename, "w" ) as output:
72 output.write( template.render(self.d) )
73
74 def generate(self,overwrite,directory):
75 if not os.path.exists( directory + "/" + self.subdirectory ):
76 os.mkdir(directory + "/" + self.subdirectory)
77
78 header_filename = directory + "/" + self.subdirectory + "/" + self.classname + ".h"
79 cpp_filename = directory + "/" + self.subdirectory + "/" + self.classname + ".cpp"
80
81 self.__generate_file(overwrite,header_filename,self.headerfile_template)
82 self.__generate_file(overwrite,cpp_filename ,self.cppfile_template)
__init__(self, headfile_template, cppfile_template, classname, namespace, subdirectory, dictionary, default_overwrite=True, apply_iteratively=False)
The template files should be fully qualified classname is a string namespace is a (possibly empty) li...