-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddModule.py
192 lines (151 loc) · 8.05 KB
/
addModule.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
#!/usr/bin/python
# This program creates module files in the flexible content format.
# It creates the file, file name, module ID, heading anchor and heading.
# The program must run in the same directory as the master.adoc and assembly
# files. If you specify the -o option, it will rename the module, include the
# assembly include directive if -a is specified.
import os
import sys
import getopt
import configparser
def main(argv):
#Load the default values to save time on CLI usage.
configParser = configparser.RawConfigParser()
configParser.read('./addModule.conf')
moduleType = configParser.get('add-module-conf', 'defaultModuleType')
moduleDestinationPath = configParser.get('add-module-conf', 'moduleDestinationPath')
componentType = configParser.get('add-module-conf', 'defaultComponentType')
locale = configParser.get('add-module-conf', 'defaultLocale')
fileExtension = configParser.get('add-module-conf', 'fileExtension')
fileNameFormat = configParser.get('add-module-conf', 'fileNameFormat')
#Initialize variables
ctx = "_{context}"
assemblyFileName = ""
oldName = ""
moduleName = ""
try:
#define command line arguments.
opts, args = getopt.getopt(argv,"n:r:t:c:a:d:f:h",["name=","rename=","type=","component=","assembly=", "destination=", "format=", "help"])
for opt, arg in opts:
if opt in ("-n", "--name"):
moduleName = arg
elif opt in ("-r", "--rename"):
oldName = arg
elif opt in ("-t", "--type"):
moduleType = arg
elif opt in ("-c", "--component"):
componentType = arg
elif opt in ("-a", "--assembly"):
assemblyFileName = arg
elif opt in ("-d", "--destination"):
moduleDestinationPath = arg
elif opt in ("-f", "--format"):
fileNameFormat = arg
elif opt in ("-h", "--help"):
printUsage()
sys.exit()
#addModule ALWAYS requires a module name.
if moduleName == "":
print ("\nERROR: Module name is required! Use the -n or --name option.\n")
printUsage()
sys.exit()
#Create a heading comment for the file header.
headingComment = getHeadingComment(assemblyFileName)
#Create the :_content-type: tag.
contentTypeTag = getContentType(moduleType)
#The outputModuleId is a component of the outputFileName and moduleID.
outputModuleId = getModuleID(moduleName)
moduleId = "[id=\"" + outputModuleId + ctx + "\"]"
if fileNameFormat == "fcc":
outputFileName = createFccFileName(moduleDestinationPath, moduleType, componentType, outputModuleId, locale, fileExtension)
elif fileNameFormat == "ocp":
outputFileName = createOcpFileName(moduleDestinationPath, componentType, outputModuleId, fileExtension)
elif fileNameFormat == "":
printUsage()
sys.exit()
includeText = getIncludeDirective(outputFileName)
#Test if oldName exists, and rename the module if it does.
if oldName !="":
# The inputModuleId is derived from the old name and to parse get inputFileName and old moduleID during rename operations.
inputModuleId = getModuleID(oldName)
if fileNameFormat == "fcc":
inputFileName = createFccFileName(moduleDestinationPath, moduleType, componentType, inputModuleId, locale, fileExtension)
elif fileNameFormat == "ocp":
inputFileName = createOcpFileName(moduleDestinationPath, componentType, inputModuleId, fileExtension)
elif fileNameFormat == "":
printUsage()
sys.exit()
inputFile = open (inputFileName, 'r')
moduleData = inputFile.read()
inputFile.close()
moduleMod = moduleData.replace(inputModuleId, outputModuleId)
moduleData = moduleMod.replace(oldName, moduleName)
outFile = open(outputFileName, 'w') #output file to write to.
outFile.write(moduleData)
outFile.close()
os.remove(inputFileName)
print (outputFileName)
if assemblyFileName != "":
assemblyFile = open(assemblyFileName, 'r')
assemblyData = assemblyFile.read()
assemblyFile.close()
inputIncludeText = getIncludeDirective(inputFileName)
modifiedAssemblyData = assemblyData.replace(inputIncludeText, includeText)
assemblyFile = open(assemblyFileName, 'w')
assemblyFile.write(modifiedAssemblyData)
assemblyFile.close()
else:
outFile = open(outputFileName, 'w') #output file to write to.
outFile.write(headingComment + "\n\n")
outFile.write(contentTypeTag + "\n")
outFile.write(moduleId + "\n")
outFile.write("= " + moduleName + "\n")
if assemblyFileName != "":
assemblyFile = open(assemblyFileName, 'a')
assemblyFile.write("\n" + includeText + "\n")
print (outputFileName)
except OSError as e:
print(str(e))
except getopt.GetoptError:
#printUsage()
sys.exit(2)
#Takes a title case string name of a module, and coverts it to a
#lowercase dash(-) delimited module ID for anchor tags and file names.
def getModuleID(name):
lowercaseModuleName = name.lower()
return lowercaseModuleName.replace(' ', '-')
#Takes destination path, module type, component type, module ID, locale and extension and creates a file name.
def createFccFileName(dpath, mtype, ctype, modId, locale, ext):
return dpath + mtype + "_" + ctype + "_" + modId + "_" + locale + ext
#Takes destination path, module type, component type, module ID, locale and extension and creates a file name.
def createOcpFileName(dpath, ctype, modId, ext):
return dpath + ctype + "-" + modId + ext
#Takes an assembly file name and gets a heading comment.
def getHeadingComment(assemblyFileName):
return "// This is included in the following assemblies:\n//\n// " + assemblyFileName
#Takes a file name and returns an include directive for the assembly file.
def getIncludeDirective(fileName):
return "include::" + fileName + "[leveloffset=+1]"
def getContentType(ctype):
if ctype == "proc":
contentType = "PROCEDURE"
elif ctype == "con":
contentType = "CONCEPT"
elif ctype == "ref":
contentType = "REFERENCE"
elif ctype == "assembly":
contentType = "ASSEMBLY"
return ":_content-type: " + contentType
def printUsage():
print ("\n\n=================\naddModule.py Help\n=================\n\n\tThe addModule.py helper program will create a new module in the flexible customer\n\tcontent/modular format. This helper program MUST run in the same directory as the\n\tassembly files. \n\n\tTo APPEND an include statement to an assembly, use the -a or --assembly option.\n\tThe assembly file SHOULD exist already; however, you may create a file without\n\tthe required formatting on-the-fly. To override the default component type in\n\taddModule.conf, use the -c or --component option.")
print ("\nUSAGE: \n\t$ python3 addModule.py -n '<moduleName>' [options] \n\nOPTIONS:\n")
print ("\t-n '<moduleName>' OR --name '<moduleName>' REQUIRED")
print ("\t-r '<oldModuleName>' OR --rename '<oldModuleName>' OPTIONAL")
print ("\t-f <fcc|ocp> OR --format <fcc|ocp> OPTIONAL. DEFAULT = fcc")
print ("\t-t <proc|con|ref|assembly> OR --type <proc|con|ref|assembly> OPTIONAL. DEFAULT = proc")
print ("\t-a <assemblyFile> OR --assembly <assemblyFile> OPTIONAL")
print ("\t-c <componentName> OR --component <componentName> OPTIONAL. For default see addModule.conf.")
print ("\t-d <moduleDestinationPath> OR --destination <moduleDestinationPath> The destination path for the module.")
print ("\n\t OPTIONAL. DEFAULT = modules/.\n")
if __name__ == "__main__":
main(sys.argv[1:])