-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenModule.py
194 lines (152 loc) · 5.58 KB
/
GenModule.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'''
@file CPPModule.py
@author David R
@date 06 Dec 2022 16:42
This is a short little script that will generate a C/CPP Module based on the
module name passed as an arg from invocation.
'''
# Common Imports -------------------------------------------------------------
import sys
import os
from datetime import date
# Custom Imports -------------------------------------------------------------
# Global Vars ----------------------------------------------------------------
PROGRAM_NAME = "GenModule.py"
ARG_OPT_CPP = "-cpp"
ARG_OPT_C = "-c"
ARG_IDX_PROGNAME = 0
ARG_IDX_MODULENAME = 1
ARG_IDX_MODULETYPE = 2
OPT_CPP_STR = "C++"
OPT_C_STR = "C"
dictModTypeStrings = \
{
ARG_OPT_CPP : OPT_CPP_STR,
ARG_OPT_C : OPT_C_STR
}
# C/C++ output file related variables
# Module Class ---------------------------------------------------------------
class CodeModule:
def __init__(self, name, type):
self.name = name
self.type = type
self.srcFileName = ""
self.hdrFileName = f'{name}.h'
if ( self.type != None ):
if ( self.type == OPT_CPP_STR ):
self.srcFileName = f'{self.name}.cpp'
elif ( self.type == OPT_C_STR ):
self.srcFileName = f'{self.name}.c'
self.today = date.today().strftime("%d %b %Y")
def getName(self):
return self.name
def getSrcFileName(self):
return self.srcFileName
def getHdrFileName(self):
return self.hdrFileName
def addSections(self, outfile):
tempStr = "-" * 60
outfile.writelines(f'// Core includes {tempStr}\n\n')
outfile.writelines(f'// Module includes {tempStr}\n\n')
outfile.writelines(f'// Defines/Macros {tempStr}\n\n')
outfile.writelines(f'// Enumerations {tempStr}\n\n')
outfile.writelines(f'// Structures {tempStr}\n\n')
outfile.writelines(f'// Local Functions {tempStr}\n\n')
outfile.writelines(f'// Local Variables {tempStr}\n\n')
outfile.writelines(f'// Const variables {tempStr}\n\n')
outfile.writelines(f'// Global variables {tempStr}\n\n')
def genHeader(self, outfile):
# 80-char wide column
strStartBlock = "/" + ("*" * 79) + "\n"
srtEndBlock = ("*" * 79) + "/\n\n"
# First is the starting block
outfile.writelines(strStartBlock)
outfile.writelines(f' @file {self.hdrFileName}\n')
outfile.writelines(f' @date {self.today}\n')
outfile.writelines(f' @author David R\n')
outfile.writelines(f' @brief \n')
outfile.writelines("\n")
outfile.writelines(f' @version 1.0\n')
outfile.writelines("\n")
outfile.writelines(f' \\addtogroup {module.getName()} \n')
outfile.writelines(" @{\n")
outfile.writelines(srtEndBlock)
# Next is the header guard
outfile.writelines(f'#ifndef _{module.getName().upper()}_H \n')
outfile.writelines(f'#define _{module.getName().upper()}_H \n')
outfile.writelines("\n")
self.addSections(outfile)
tempStr = "-" * 60
outfile.writelines(f'// Func Prototypes {tempStr}\n\n')
outfile.writelines(f'#ifdef __cplusplus\n')
outfile.writelines(f'extern "C" {{\n')
outfile.writelines(f'#endif // __cplusplus\n\n\n\n')
outfile.writelines(f'#ifdef __cplusplus\n')
outfile.writelines(f'}};\n')
outfile.writelines(f'#endif // __cplusplus\n')
outfile.writelines("\n")
outfile.writelines(f'#endif // _{module.getName().upper()}_H \n\n')
# Common to both Source and Header files
tempStr = "}"
outfile.writelines(f'/** @{tempStr} {self.hdrFileName} */\n')
def genSourceFile(self, outfile):
# 80-char wide column
strStartBlock = "/" + ("*" * 79) + "\n"
srtEndBlock = ("*" * 79) + "/\n\n"
# First is the starting block
outfile.writelines(strStartBlock)
outfile.writelines(f' @file {self.srcFileName}\n')
outfile.writelines(f' @date {self.today}\n')
outfile.writelines(f' @author David R\n')
outfile.writelines(f' @brief \n')
outfile.writelines("\n")
outfile.writelines(f' @version 1.0\n')
outfile.writelines("\n")
outfile.writelines(f' \\addtogroup {module.getName()} \n')
outfile.writelines(" @{\n")
outfile.writelines(srtEndBlock)
self.addSections(outfile)
tempStr = "-" * 60
outfile.writelines(f'// Func Definitions {tempStr}\n\n')
# One function documentation thing
outfile.writelines(strStartBlock)
outfile.writelines(f' * @function \n')
outfile.writelines(f' *\n')
outfile.writelines(f' * @brief \n')
outfile.writelines(f' *\n')
outfile.writelines(f' * @param[io] \n')
outfile.writelines(f' * @param[in] \n')
outfile.writelines(f' * @param[out] \n')
outfile.writelines(f' *\n')
outfile.writelines(f' * @return \n')
outfile.writelines(srtEndBlock)
# Section div for private functions
tempStr = "/" * 80
outfile.writelines(f'{tempStr}\n')
outfile.writelines(f'// Static Functions \n')
outfile.writelines(f'{tempStr}\n\n\n\n')
# Common to both Source and Header files
tempStr = "}"
outfile.writelines(f'/** @{tempStr} {self.srcFileName} */\n')
# Helper functions -----------------------------------------------------------
def printUsage():
print(f'Usage: \npython3 {PROGRAM_NAME} <module_name> {ARG_OPT_CPP}/{ARG_OPT_C}')
# Main -----------------------------------------------------------------------
numArgs = len(sys.argv)
if (numArgs < 2):
printUsage()
exit()
moduleName = sys.argv[ARG_IDX_MODULENAME]
moduleType = dictModTypeStrings[sys.argv[ARG_IDX_MODULETYPE]]
print(f'You want to create a {moduleType} module called {moduleName}')
module = CodeModule(moduleName, moduleType)
try:
moduleDir = os.mkdir(moduleName)
except OSError as error:
print(error)
srcFile = open(f'{moduleName}/{module.getSrcFileName()}', "w")
module.genSourceFile(srcFile)
srcFile.close()
hdrFile = open(f'{moduleName}/{module.getHdrFileName()}', "w")
module.genHeader(hdrFile)
hdrFile.close()