Generation of dictionary with python
Use following python template:
#!/usr/bin/python
import os
import filecmp, shutil, tempfile
NAMESPACE = "jachong"
DATA_SECTION_BEGIN = "[DATA]"
WARNING = """
//////
////// THIS CODE WAS GENERATED BY "%s"
////// DO NOT EDIT MANUALLY!!!
//////
""" % os.path.basename(__file__)
NAMESPACE_BEGIN = """
namespace %s {
""" % NAMESPACE
NAMESPACE_END = """
}; ///:~ namespace %s
""" % NAMESPACE
class Model:
def __init__(self):
self.name = ""
self.level = 0
self.cost = 0
class Generator:
def __init__(self):
self.currentDir = os.path.dirname(os.path.abspath(__file__))
self.data = []
def main(self):
updatedFiles = []
csvFileName = "data.csv"
dicFileName = "dic.h"
self.parseCSV(open(os.path.join(self.currentDir, csvFileName), "r"))
updatedFiles.append(self.createDic(dicFileName))
updatedFiles = [f for f in updatedFiles if f is not None]
print ("Updated files: ", updatedFiles)
def parseCSV(self, csvf):
mode = "out"
model = Model()
isData = False
for line in csvf:
if isData:
line = line.strip()
if Utils.isCommentLine(line):
print line
continue
else:
line = line.strip()
if line.startswith(DATA_SECTION_BEGIN):
isData = True
else
print line
continue
class Utils:
@staticmethod
def isCommentLine(str):
# empty line or line comment
if str == "" or str.startswith("#"):
return True
# doxygen comments
elif str.startswith("/*!") or str.startswith("*"):
return True
else:
return False
if __name__ == "__main__":
g = Generator()
g.main()