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
7
8from .Overwrite import Overwrite
9
11 def __init__(self,headfile_template,cppfile_template,classname,namespace,subdirectory,dictionary,default_overwrite=True):
12 """
13 The template files should be fully qualified
14 classname is a string
15 namespace is a (possibly empty) list of strings
16
17 cppfile_template can be None
18 """
19 self.headerfile_template = headfile_template
20 self.cppfile_template = cppfile_template
21 self.classname = classname
22 self.namespace = namespace
23 self.subdirectory = subdirectory
24 self.default_overwrite = default_overwrite
25
26 self.d = dictionary
27 self.d[ "OPEN_NAMESPACE" ] = ""
28 self.d[ "CLOSE_NAMESPACE" ] = ""
29 self.d[ "FULL_NAMESPACE" ] = ""
30 self.d[ "FULL_QUALIFIED_CLASSNAME" ] = ""
31 for i in namespace:
32 self.d[ "OPEN_NAMESPACE" ] += "namespace " + i + "{\n"
33 self.d[ "CLOSE_NAMESPACE" ] += "}\n"
34 self.d[ "FULL_QUALIFIED_CLASSNAME" ] += i + "::"
35 if self.d[ "FULL_NAMESPACE" ] != "":
36 self.d[ "FULL_NAMESPACE" ] += "::"
37 self.d[ "FULL_NAMESPACE" ] += i
38 self.d[ "CLASSNAME" ] = classname
39 self.d[ "FULL_QUALIFIED_CLASSNAME" ] += classname
40 self.d[ "INCLUDE_GUARD" ] = self.d[ "FULL_QUALIFIED_CLASSNAME" ].replace( "::", "_" ).replace( "-", "_" ).upper()
41
42 def generate_file(self,overwrite,full_qualified_filename,template_file):
43 if template_file!=None and write_file(overwrite,self.default_overwrite,full_qualified_filename):
44 import inspect
45 print( "{} written by {}".format(full_qualified_filename, os.path.basename(inspect.getfile(self.__class__))))
46 with open( os.path.realpath(template_file), "r" ) as input:
47 template = input.read()
48 with open( full_qualified_filename, "w" ) as output:
49 output.write( template.format(**self.d) )
50
51 def generate(self,overwrite,directory):
52 if not os.path.exists( directory + "/" + self.subdirectory ):
53 os.mkdir(directory + "/" + self.subdirectory)
54
55 header_filename = directory + "/" + self.subdirectory + "/" + self.classname + ".h"
56 cpp_filename = directory + "/" + self.subdirectory + "/" + self.classname + ".cpp"
57
58 self.generate_file(overwrite,header_filename,self.headerfile_template)
59 self.generate_file(overwrite,cpp_filename ,self.cppfile_template)
__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...