anjana_sreekumar@infosys.com | 991c206 | 2020-01-08 11:42:57 +0530 | [diff] [blame^] | 1 | # Copyright 2019-present Infosys Limited |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | import re |
| 5 | from template import Template |
| 6 | import os |
| 7 | from openpyxl import load_workbook |
| 8 | |
| 9 | libData = {} |
| 10 | progData = {} |
| 11 | template = Template() |
| 12 | wb = load_workbook('dataModel/prototypeV8.xlsx') |
| 13 | class xlUtils: |
| 14 | def __init__(self): |
| 15 | pass |
| 16 | |
| 17 | |
| 18 | def extractField(field): |
| 19 | varName = '' |
| 20 | varType = '' |
| 21 | encodeCondition = '' |
| 22 | decodeCondition = '' |
| 23 | validation = '' |
| 24 | count = '' |
| 25 | mVarName = re.search('^(\w+)',str(field)) |
| 26 | mVarType = re.search(':Type\[(\w+)\]',str(field)) |
| 27 | mEnCond = re.search(':Condition\[([^\:]*)\]',str(field)) |
| 28 | mDeCond = re.search(':DecodeCondition\[([^\:]*)]',str(field)) |
| 29 | mValid = re.search(':Validation\[([^\:]+)\]',str(field)) |
| 30 | mCount = re.search(':Count\[([^\:]*)]',str(field)) |
| 31 | if mVarName: |
| 32 | varName = mVarName.group(1) |
| 33 | |
| 34 | if mVarType: |
| 35 | varType = mVarType.group(1) |
| 36 | |
| 37 | if mEnCond: |
| 38 | encodeCondition = mEnCond.group(1) |
| 39 | |
| 40 | if mDeCond: |
| 41 | decodeCondition = mDeCond.group(1) |
| 42 | else: |
| 43 | if mEnCond: |
| 44 | decodeCondition = mEnCond.group(1) |
| 45 | |
| 46 | |
| 47 | if mValid: |
| 48 | validation = mValid.group(1) |
| 49 | dataVarName = 'data.' + varName |
| 50 | validation = re.sub(r'\$.',dataVarName,validation) |
| 51 | |
| 52 | if mCount: |
| 53 | count = mCount.group(1) |
| 54 | |
| 55 | return (varName,varType,encodeCondition,decodeCondition,validation,count) |
| 56 | |
| 57 | def lengthToType(byteLength): |
| 58 | lTdict = {1:'Uint8',2:'Uint16',3:'Uint32',4:'Uint32',5:'Uint64',6:'Uint64',7:'Uint64',8:'Uint64',65535:'Uint8',9:'Uint8',12:'Uint8'} |
| 59 | return lTdict[byteLength] |
| 60 | |
| 61 | def getByteLength(sheet,i): |
| 62 | length = 0 |
| 63 | lengthStr = '' |
| 64 | cell_value_C = xlUtils.getCellValue(sheet,i,'C') |
| 65 | octets = str(cell_value_C) |
| 66 | |
| 67 | mTo = re.search('to',str(octets)) |
| 68 | mVar = re.search('^variable\[(.*)]',str(octets)) |
| 69 | mToD = re.search('^(\d+) to (\d+)',str(octets)) |
| 70 | mToWD = re.search('^(\w) to (\w)\+(\d+)',str(octets)) |
| 71 | mToWWD = re.search('^(\w)\+(\d+) to (\w)\+(\d+)',str(octets)) |
| 72 | |
| 73 | if not mTo: |
| 74 | if mVar: |
| 75 | lenStr = mVar.group(1) |
| 76 | length = 65535 |
| 77 | lengthStr = lenStr |
| 78 | |
| 79 | elif xlUtils.getBitLength(sheet,i,'D') == 8: |
| 80 | length = 1 |
| 81 | |
| 82 | else: |
| 83 | if mToD: |
| 84 | toD = mToD.group(1) |
| 85 | toD2 = mToD.group(2) |
| 86 | length = int(toD2) - int(toD) + 1 |
| 87 | |
| 88 | elif mToWD: |
| 89 | ToWD = mToWD.group(3) |
| 90 | length = int(ToWD) + 1 |
| 91 | |
| 92 | elif mToWWD: |
| 93 | ToWWD2 = mToWWD.group(2) |
| 94 | ToWWD4 = mToWWD.group(4) |
| 95 | length = int(ToWWD4) - int(ToWWD2) + 1 |
| 96 | return (length,lengthStr) |
| 97 | |
| 98 | def getBitLength(sheet,i,j): |
| 99 | length = 0 |
| 100 | fieldName = xlUtils.getCellValue(sheet,i,j) |
| 101 | mcolDtoK = re.search('[D-K]',j) |
| 102 | if fieldName != None and mcolDtoK: |
| 103 | for k in map(chr, range( ord(j), ord('L'))): |
| 104 | fName = xlUtils.getCellValue(sheet,i,k) |
| 105 | |
| 106 | if length == 0: |
| 107 | length = length + 1 |
| 108 | |
| 109 | elif fName == None: |
| 110 | length = length + 1 |
| 111 | |
| 112 | else: |
| 113 | break |
| 114 | return length |
| 115 | |
| 116 | |
| 117 | def getCellValue(sheet,row,column): |
| 118 | colNum = 0 |
| 119 | chartoint = {'A' : 1,'B' : 2, 'C' : 3, 'D' : 4, 'E' : 5, 'F' : 6, 'G' : 7, |
| 120 | 'H' : 8, 'I' : 9, 'J' : 10, 'K' : 11, 'L' : 12, 'M' : 13, 'N' : 14, |
| 121 | 'O' : 15, 'P' : 16, 'Q' : 17, 'R' : 18, 'S' : 19, 'T' : 20, 'U' : 21, |
| 122 | 'V' : 22, 'W' : 23, 'X' : 24, 'Y' : 25, 'Z' : 26} |
| 123 | |
| 124 | colNum = chartoint[column] |
| 125 | cellvalue = sheet.cell(row=row, column=colNum).value |
| 126 | return cellvalue |
| 127 | |
| 128 | def getVarNameFromString(localString,typeName): |
| 129 | |
| 130 | localString = localString.lower() |
| 131 | varName = '' |
| 132 | localString = re.sub(r'\(.*\)'," ",localString) |
| 133 | localString=localString.replace('/'," ") |
| 134 | localString = re.sub('-'," ",localString) |
| 135 | tokens = [x.capitalize() for x in localString.split(" ")] |
| 136 | varName = varName.join(tokens) |
| 137 | varName = varName[0].upper() + varName[1:] |
| 138 | if typeName == 1: |
| 139 | return varName |
| 140 | else: |
| 141 | return varName[0].lower() + varName[1:] |
| 142 | |
| 143 | def templateProcess(templateData,ttFileNameCpp,ttFileNameH,outputDirCpp,outputDirH): |
| 144 | |
| 145 | |
| 146 | template = Template() |
| 147 | |
| 148 | if not os.path.exists(outputDirCpp): |
| 149 | os.makedirs(outputDirCpp) |
| 150 | outputFileNameCpp = templateData['fileName'] + '.cpp' |
| 151 | |
| 152 | template.__init__({'OUTPUT' : outputFileNameCpp, 'OUTPUT_PATH' : outputDirCpp}) |
| 153 | template.process(ttFileNameCpp, {'tempdata' : templateData}) |
| 154 | |
| 155 | |
| 156 | if not os.path.exists(outputDirH): |
| 157 | os.makedirs(outputDirH) |
| 158 | outputFileNameH = templateData['fileName'] + '.h' |
| 159 | template.__init__({'OUTPUT' : outputFileNameH, 'OUTPUT_PATH' : outputDirH}) |
| 160 | template.process(ttFileNameH, {'tempdata' : templateData}) |
| 161 | |
| 162 | def addToMakeSo(libName,objFile,srcFile): |
| 163 | |
| 164 | fileData = {} |
| 165 | fileData['objFile'] = objFile |
| 166 | fileData['sourceFile'] = srcFile |
| 167 | if libName not in libData: |
| 168 | lib = libName |
| 169 | lib = re.sub(r'.so','',lib) |
| 170 | libData[libName] = {} |
| 171 | libData[libName]['libName'] = lib |
| 172 | libData[libName]['fileList'] = [] |
| 173 | if fileData not in libData[libName]['fileList']: |
| 174 | libData[libName]['fileList'].append(fileData) |
| 175 | |
| 176 | def addToMakeExe(progName,objFile,srcFile): |
| 177 | fileData = {} |
| 178 | fileData['objFile'] = objFile |
| 179 | fileData['sourceFile'] = srcFile |
| 180 | |
| 181 | if progName not in progData: |
| 182 | prog = progName |
| 183 | prog = re.sub(r'.exe','',prog) |
| 184 | progData[progName] = {} |
| 185 | progData[progName]['progName'] = prog |
| 186 | |
| 187 | progData[progName]['fileList'] = [] |
| 188 | progData[progName]['fileList'].append(fileData) |
| 189 | |
| 190 | def addSoToMakeExe(progName,soName): |
| 191 | if progName not in progData: |
| 192 | prog = progName |
| 193 | prog = re.sub(r'.exe','',prog) |
| 194 | progData[progName] = {} |
| 195 | progData[progName]['progName'] = prog |
| 196 | |
| 197 | progData[progName]['soList'] = [] |
| 198 | progData[progName]['soList'].append(soName) |
| 199 | |
| 200 | def generateMakeFile(): |
| 201 | makeFileData = {} |
| 202 | |
| 203 | for libr in libData.keys(): |
| 204 | makeFileData['libList'] = [] |
| 205 | makeFileData['libList'].append(libData[libr]) |
| 206 | for prog in progData.keys(): |
| 207 | makeFileData['progList']=[] |
| 208 | makeFileData['progList'].append(progData[prog]) |
| 209 | |
| 210 | |
| 211 | ttFileName = 'tts/makefiletemplate.tt' |
| 212 | |
| 213 | fileName = 'Makefile' |
| 214 | outputDir='../../src/gtpV2Codec' |
| 215 | |
| 216 | template.__init__({'OUTPUT' : fileName, 'OUTPUT_PATH' : outputDir}) |
| 217 | template.process(ttFileName, {'makefiledata': makeFileData}) |
| 218 | |
| 219 | xlUtils() |