def getNodeByName(self, nodeList, searchDict, field_name): regBasic = r'^(int|string|float|xdr)' if re.search(regBasic, field_name): #print("getbasic") nodeData = { "field_type": field_name, "field_name": field_name, "field_length": 0 } return sdlNode(data=nodeData) if field_name in searchDict: index = searchDict[field_name] #print("getdict") #return copy.deepcopy(nodeList[index]) nodeTmp1 = copy.deepcopy(nodeList[index]) if nodeTmp1.data["field_type"] == "map": nodeData = { "field_type": "list", "field_name": "maplist", "field_length": 0 } nodeTmp2 = sdlNode(data=nodeData) nodeTmp2.insertNode(nodeTmp1) return nodeTmp2 return nodeTmp1 #print("getNode " + field_name + " None") return None
def getNodeByName(self, nodeList, searchDict, field_name): regBasic = r'^(int|string|float|xdr)' if re.search(regBasic, field_name): #print("getbasic" + field_name) ndata = nodeData(field_type=field_name, field_name=field_name, field_length=0) return sdlNode(data=ndata) if field_name in searchDict: index = searchDict[field_name] #print("getdict" + field_name) nodeTmp1 = copy.deepcopy(nodeList[index]) if nodeTmp1.data.field_type == "map": ndata = nodeData(field_type="list", field_name="maplist", field_length=0) nodeTmp2 = sdlNode(data=ndata) nodeTmp2.insertNode(nodeTmp1) return nodeTmp2 return nodeTmp1 #print("getNode " + field_name + " None") return None
def getNodeByName(self, nodeList, searchDict, field_name): regBasic = r'^(int|string|float|xdr)' if re.search(regBasic, field_name): #print("getbasic") return sdlNode(field_name=field_name, field_type=field_name, field_length=0) if field_name in searchDict: index = searchDict[field_name] #print("getdict") return copy.deepcopy(nodeList[index]) #print("getNode " + field_name + " None") return None
def analyseSdlContent(self): # '{' '}' '[' ']' '(' ')' ',' '\\' '\"' '\'' '\r' '\n' regstr = self.regkey + r'{({.*})}' mat = re.search(regstr, self.xdr_content) if not mat: return -1 content = mat.group(1) #print(content) nodeData = { "field_type": None, "field_name": self.root_name, "field_length": None, "field_value": None, "field_seq": None, "field_note": None } self.xdrNode = sdlNode(data=nodeData) #searchContext = "{" #numList = [m.start() for m in re.finditer(searchContext, content)] #print(numList) currPos = 0 currNode = self.xdrNode strLen = len(content) nodeData = { "field_type": None, "field_name": None, "field_length": None, "field_value": None, "field_seq": None, "field_note": None } tmpNode = sdlNode(data=nodeData) while currPos < strLen: currChar = content[currPos] flagNextNode = False if currChar == '{': tmpNode.data["field_type"] = "struct" tmpNode.data["field_name"] = "struct" flagNextNode = True elif currChar == '[': tmpNode.data["field_type"] = "list" tmpNode.data["field_name"] = "list" flagNextNode = True elif currChar == '(': tmpNode.data["field_type"] = "map" tmpNode.data["field_name"] = "map" flagNextNode = True elif currChar not in ('}', ']', ')', ','): i_s = self.getIndexForChar(content, '}', currPos) i_l = self.getIndexForChar(content, ']', currPos) i_m = self.getIndexForChar(content, ')', currPos) i_n = self.getIndexForChar(content, ',', currPos) typeTuple = (i_s, i_l, i_m, i_n) minTypeIndex, nextPos = self.getMinNumIndex(typeTuple) #print("currPos : " + str(currPos) + " nextPos : " + str(nextPos)) #print(currChar) if nextPos == -1: return -1 valueStr = content[currPos:nextPos] currPos = nextPos - 1 tmpNode.data["field_type"] = "value" tmpNode.data["field_name"] = "value" # '\\1' = r'\1' tmpNode.data["field_value"] = re.sub( r'\\([\\\{\}\[\]\(\),\'\"])', r'\1', valueStr).replace('\\r', '\r').replace('\\n', '\n') flagNextNode = False elif content[currPos] == ',' and content[currPos - 1] == ',': tmpNode.data["field_type"] = "value" tmpNode.data["field_name"] = "value" flagNextNode = False elif content[currPos] in ('}', ']', ')'): currNode = currNode.parent currPos = currPos + 1 continue else: currPos = currPos + 1 continue if not currNode: print("error") return -2 currNode.insertNode(tmpNode) if flagNextNode: currNode = tmpNode currPos = currPos + 1 nodeData = { "field_type": None, "field_name": None, "field_length": None, "field_value": None, "field_seq": None, "field_note": None } tmpNode = sdlNode(data=nodeData) #self.xdrNode.children[0].data["field_name"] = "MXdr::SXdr" self.getNodeSeq(self.xdrNode.children[0]) return 0
def analyseSdlDefine(self): sdlLineList = self.getSdlFile() #print("start") if not sdlLineList: return -1 #print("start") regStruct = r'^struct\s*([0-9a-zA-Z_]+)' regStructEnd = r'};?' regMap = r'^map\s+aimap\s*<\s*([0-9a-zA-Z_]+)\s*\,\s*([0-9a-zA-Z_]+)\s*>\s+([0-9a-zA-Z_]+)\s*;' regList = r'^list\s+vector\s*<\s*([0-9a-zA-Z_]+)\s*>\s+([0-9a-zA-Z_]+)\s*;' regBasic = r'^(int|string|float|xdr)<?([0-9]*)>?\s*([0-9a-zA-Z_]+)\s*;' regOther = r'^([0-9a-zA-Z_]+)\s*([0-9a-zA-Z_]+)\s*;' nodeList = [] searchDict = dict() flagStruct = False FlagStructEnd = True nodeData = { "field_type": None, "field_name": None, "field_length": None #"field_value" : None, #"field_seq" : None } sdlNodeTmp = sdlNode(data=nodeData) #print("sdlLineList length : " + str(len(sdlLineList))) for line in sdlLineList: line = re.sub(r'//.*|\n|\r', "", line).strip() #print("line length : " + str(len(line))) if len(line) == 0: #print("line 0") continue #print("line : " + line) if re.search(regStructEnd, line) and flagStruct and not FlagStructEnd: flagStruct = False FlagStructEnd = True #print("regStructEnd") elif re.search(regStruct, line): #print("regStruct") flagStruct = True FlagStructEnd = False mat = re.search(regStruct, line) sdlNodeTmp.data["field_name"] = mat.group(1) sdlNodeTmp.data["field_type"] = "struct" sdlNodeTmp.data["field_length"] = 0 elif re.search(regMap, line): #print("regMap") flagStruct = False mat = re.search(regMap, line) sdlNodeTmp.data["field_name"] = mat.group(3) sdlNodeTmp.data["field_type"] = "map" sdlNodeTmp.data["field_length"] = 0 key = mat.group(1) value = mat.group(2) #print("key : " + key + " value : " + value) nodeTmp1 = self.getNodeByName(nodeList, searchDict, key) if not nodeTmp1: return -2 sdlNodeTmp.insertNode(nodeTmp1) nodeTmp2 = self.getNodeByName(nodeList, searchDict, value) if not nodeTmp2: return -2 sdlNodeTmp.insertNode(nodeTmp2) elif re.search(regList, line): #print("regList") flagStruct = False mat = re.search(regList, line) sdlNodeTmp.data["field_name"] = mat.group(2) sdlNodeTmp.data["field_type"] = "list" sdlNodeTmp.data["field_length"] = 0 key = mat.group(1) nodeTmp = self.getNodeByName(nodeList, searchDict, key) if not nodeTmp: return -2 sdlNodeTmp.insertNode(nodeTmp) elif flagStruct: if re.search(regBasic, line): #print("regBasic") mat = re.search(regBasic, line) field_length = mat.group(2) if not field_length: field_length = 0 nodeData = { "field_type": mat.group(1), "field_name": mat.group(3).upper(), "field_length": field_length } nodeTmp = sdlNode(data=nodeData) sdlNodeTmp.insertNode(nodeTmp) elif re.search(regOther, line): #print("regOther") mat = re.search(regOther, line) key = mat.group(1) value = mat.group(2) nodeTmp = self.getNodeByName(nodeList, searchDict, key) if nodeTmp.data["field_type"] != "map": nodeTmp.data["field_name"] = value.upper() if not nodeTmp: return -2 sdlNodeTmp.insertNode(nodeTmp) else: #print("continue") continue #print("FlagStructEnd : " + str(FlagStructEnd) + " flagStruct : " + str(flagStruct)) if FlagStructEnd and not flagStruct and not sdlNodeTmp.Empty(): #print("insert") flagStruct = False FlagStructEnd = True nodeList.append(sdlNodeTmp) searchDict[sdlNodeTmp.data["field_name"]] = len(nodeList) - 1 nodeData = { "field_type": None, "field_name": None, "field_length": None } sdlNodeTmp = sdlNode(data=nodeData) self.root = self.getNodeByName(nodeList, searchDict, self.root_name) self.root.index = 0 return 0