Peano
Loading...
Searching...
No Matches
TemplatedHeaderImplementationFilePair.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
5
6from .Helper import write_file, WARNING_BANNER
7
8from .Overwrite import Overwrite
9
10from ..Logger import Logger
11
13 def __init__(self,headfile_template,cppfile_template,classname,namespace,subdirectory,dictionary,default_overwrite=True):
14 """
15 The template files should be fully qualified
16 classname is a string
17 namespace is a (possibly empty) list of strings
18
19 cppfile_template can be None
20 """
21 self.headerfile_template = headfile_template
22 self.cppfile_template = cppfile_template
23 self.classname = classname
24 self.namespace = namespace
25 self.subdirectory = subdirectory
26 self.default_overwrite = default_overwrite
27
28 self.d = dictionary
29 self.d[ "OPEN_NAMESPACE" ] = ""
30 self.d[ "CLOSE_NAMESPACE" ] = ""
31 self.d[ "FULL_NAMESPACE" ] = ""
32 self.d[ "FULL_QUALIFIED_CLASSNAME" ] = ""
33 for i in namespace:
34 self.d[ "OPEN_NAMESPACE" ] += "namespace " + i + "{\n"
35 self.d[ "CLOSE_NAMESPACE" ] += "}\n"
36 self.d[ "FULL_QUALIFIED_CLASSNAME" ] += i + "::"
37 if self.d[ "FULL_NAMESPACE" ] != "":
38 self.d[ "FULL_NAMESPACE" ] += "::"
39 self.d[ "FULL_NAMESPACE" ] += i
40 self.d[ "CLASSNAME" ] = classname
41 self.d[ "FULL_QUALIFIED_CLASSNAME" ] += classname
42 self.d[ "INCLUDE_GUARD" ] = self.d[ "FULL_QUALIFIED_CLASSNAME" ].replace( "::", "_" ).replace( "-", "_" ).upper()
43
44 def generate_file(self,overwrite,full_qualified_filename,template_file):
45 if template_file!=None and write_file(overwrite,self.default_overwrite,full_qualified_filename):
46 Logger().log( "write " + full_qualified_filename )
47 with open( os.path.realpath(template_file), "r" ) as input:
48 template = input.read()
49 rendered_text = template.format(**self.d)
50 if self.default_overwrite:
51 rendered_text = WARNING_BANNER + rendered_text
52 with open( full_qualified_filename, "w" ) as output:
53 output.write( rendered_text )
54
55 def generate(self,overwrite,directory):
56 subdirectory_path = os.path.normpath( directory + "/" + self.subdirectory )
57 if not os.path.exists( subdirectory_path ):
58 os.mkdir(subdirectory_path)
59
60 header_filename = os.path.normpath( subdirectory_path + "/" + self.classname + ".h" )
61 cpp_filename = os.path.normpath( subdirectory_path + "/" + self.classname + ".cpp" )
62
63 self.generate_file(overwrite,header_filename,self.headerfile_template)
64 self.generate_file(overwrite,cpp_filename ,self.cppfile_template)
Singleton logger for the Peano 4 Python API.
Definition Logger.py:37
__init__(self, headfile_template, cppfile_template, classname, namespace, subdirectory, dictionary, default_overwrite=True)
The template files should be fully qualified classname is a string namespace is a (possibly empty) li...