Peano
Loading...
Searching...
No Matches
TemplatedHeaderFile.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
10class TemplatedHeaderFile(object):
11 def __init__(self,headerfile_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 self.headerfile_template = headerfile_template
18 self.classname = classname
19 self.namespace = namespace
20 self.subdirectory = subdirectory
21 self.default_overwrite = default_overwrite
22
23 self.d = dictionary
24 self.d[ "OPEN_NAMESPACE" ] = ""
25 self.d[ "CLOSE_NAMESPACE" ] = ""
26 self.d[ "FULL_NAMESPACE" ] = ""
27 self.d[ "FULL_QUALIFIED_CLASSNAME" ] = ""
28 for i in namespace:
29 self.d[ "OPEN_NAMESPACE" ] += "namespace " + i + "{\n"
30 self.d[ "CLOSE_NAMESPACE" ] += "}\n"
31 self.d[ "FULL_QUALIFIED_CLASSNAME" ] += i + "::"
32 if self.d[ "FULL_NAMESPACE" ] != "":
33 self.d[ "FULL_NAMESPACE" ] += "::"
34 self.d[ "FULL_NAMESPACE" ] += i
35 self.d[ "CLASSNAME" ] = classname
36 self.d[ "FULL_QUALIFIED_CLASSNAME" ] += classname
37 self.d[ "INCLUDE_GUARD" ] = self.d[ "FULL_QUALIFIED_CLASSNAME" ].replace( "::", "_" ).replace( "-", "_" ).upper()
38
39 def generate_file(self,overwrite,full_qualified_filename,template_file):
40 if template_file!=None and write_file(overwrite,self.default_overwrite,full_qualified_filename):
41 import inspect
42 print( "[{}] writes {}".format(inspect.getfile(self.__class__), full_qualified_filename ))
43 with open( os.path.realpath(template_file), "r" ) as input:
44 template = input.read()
45 with open( full_qualified_filename, "w" ) as output:
46 output.write( template.format(**self.d) )
47
48 def generate(self,overwrite,directory):
49 if not os.path.exists( directory + "/" + self.subdirectory ):
50 os.mkdir(directory + "/" + self.subdirectory)
51
52 header_filename = directory + "/" + self.subdirectory + "/" + self.classname + ".h"
53
54 self.generate_file(overwrite,header_filename,self.headerfile_template)
generate_file(self, overwrite, full_qualified_filename, template_file)
__init__(self, headerfile_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...