-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModuleGenerator.py
More file actions
65 lines (56 loc) · 2.38 KB
/
Copy pathModuleGenerator.py
File metadata and controls
65 lines (56 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import string
import os, os.path
import errno
class ModuleGenerator:
def __init__(self, author, date, name, company, path=""):
self.author = author
self.date = date
self.moduleName = name
self.company = company
self.outputPath = path
def Make(self):
self.WriteHeader()
self.WriteSource()
# Taken from https://stackoverflow.com/a/600612/119527
def mkdir_p(self, path):
if path == '':
pass
else:
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else: raise
def WriteHeader(self):
# Read in the .h template file
with open("templates/header.htf") as inputFile:
headerTemplate = string.Template(inputFile.read())
inputFile.close()
# Substitute the new strings with the template tags
headerFile = headerTemplate.substitute( module=self.moduleName,
author=self.author,
date=self.date,
company=self.company,
fileMacro=self.moduleName.upper())
# Write out to header file
self.mkdir_p(self.outputPath)
with open(self.outputPath + self.moduleName.lower() + ".h", "w") as outputFile:
outputFile.write(headerFile)
outputFile.close()
def WriteSource(self):
# Read in the .c template file
with open("templates/source.ctf") as inputFile:
sourceTemplate = string.Template(inputFile.read())
inputFile.close()
# Substitute the new strings with the template tags
sourceFile = sourceTemplate.substitute(module=self.moduleName,
author=self.author,
date=self.date,
company=self.company,
moduleInclude=self.moduleName.lower())
# Write out to source file
self.mkdir_p(self.outputPath)
with open(self.outputPath + self.moduleName.lower() + ".c", "w") as outputFile:
outputFile.write(sourceFile)
outputFile.close()