def showInEditor(self): from ui.CodeUIItem import CodeUIItem from ui.CodeUIEdgeItem import CodeUIEdgeItem from db.DBManager import DBManager itemList = self.selectedItems() if not itemList: return item = itemList[0] if isinstance(item, CodeUIItem): # entity = item.getEntity() # if not entity: # return db = DBManager.instance().getDB() uname = item.getUniqueName() if not db: return # refs = entity.refs('definein') # if not refs: # refs = entity.refs('declarein') # if not refs: # refs = entity.refs('callby') # if not refs: # refs = entity.refs('useby') # if not refs: # return refs = db.searchRef(uname, 'definein') if not refs: refs = db.searchRef(uname, 'declarein') if not refs: refs = db.searchRef(uname, 'callby') if not refs: refs = db.searchRef(uname, 'useby') if not refs: return ref = refs[0] fileEnt = ref.file() line = ref.line() column = ref.column() fileName = fileEnt.longname() elif isinstance(item, CodeUIEdgeItem): line = item.line column = item.column fileName = item.file from db.DBManager import DBManager socket = DBManager.instance().getSocket() socket.remoteCall('goToPage', [fileName, line, column])
def onCloseDB(self): print('close db------------------') from db.DBManager import DBManager dbPath = DBManager.instance().getDB().getDBPath() if not dbPath: return file = open(dbPath + '.config', 'w') codeItemList = list(self.itemDict.keys()) edgeItemList = list(self.edgeDict.keys()) edgeDataList = [] for edgeKey, edgeData in self.edgeDataDict.items(): edgeDataList.append([edgeKey[0], edgeKey[1], edgeData]) # 修改scheme边的数据格式,以便存成json格式 import copy scheme = copy.deepcopy(self.scheme) for schemeName, schemeData in scheme.items(): edgeList = [] for edgeKey, edgeData in schemeData.get('edge',{}).items(): edgeList.append([edgeKey[0], edgeKey[1]]) schemeData['edge'] = edgeList jsonDict = {'stopItem':self.stopItem, 'codeItem':codeItemList, 'codeData':self.itemDataDict, 'edgeItem':edgeItemList, 'edgeData':edgeDataList, 'scheme':scheme} jsonStr = JSONEncoder().encode(jsonDict) file.write(jsonStr) file.close()
def _buildRef(self): from db.DBManager import DBManager dbObj = DBManager.instance().getDB() for uname, symbol in self.symbolDict.items(): entityList, refList = dbObj.searchRefEntity(uname, 'call', '*', True) for ent in entityList: tarUname = ent.uniquename() self.callRef[(uname, tarUname)] = RefData(RefData.REF_CALL)
def _buildRef(self): from db.DBManager import DBManager dbObj = DBManager.instance().getDB() for uname, symbol in self.symbolDict.items(): entityList, refList = dbObj.searchRefEntity( uname, 'call', '*', True) for ent in entityList: tarUname = ent.uniquename() self.callRef[(uname, tarUname)] = RefData(RefData.REF_CALL)
def buildScene(self): from db.DBManager import DBManager dbObj = DBManager.instance().getDB() self.callRef = {} self.symbolRoot, self.symbolDict = dbObj.buildSymbolTree() if not self.symbolRoot or not self.symbolDict: return self._buildRef() self._buildUI()
def showScheme(self, name, selectScheme = True): if name not in self.scheme: return False self.acquireLock() selectedNode = [] selectedEdge = [] if not selectScheme: for uname, node in self.itemDict.items(): if node.isSelected(): selectedNode.append(uname) for uname, edge in self.edgeDict.items(): if edge.isSelected(): selectedEdge.append(uname) from db.DBManager import DBManager dbObj = DBManager.instance().getDB() codeItemList = self.scheme[name].get('node',[]) for uname in codeItemList: res, item = self.addCodeItem(uname) self.clearSelection() for uname in codeItemList: item = self.itemDict.get(uname) if item and selectScheme: item.setSelected(True) edgeItemDict = self.scheme[name].get('edge',{}) for edgePair, _ in edgeItemDict.items(): # 自定义边一定能够创建 edgeData = self.edgeDataDict.get(edgePair, {}) if edgeData.get('customEdge', False): self._doAddCodeEdgeItem(edgePair[0], edgePair[1], {'customEdge':True}) else: refObj = dbObj.searchRefObj(edgePair[0], edgePair[1]) if refObj: self._doAddCodeEdgeItem(edgePair[0], edgePair[1], {'dbRef':refObj}) edgeItem = self.edgeDict.get(edgePair) if edgeItem and selectScheme: edgeItem.setSelected(True) if not selectScheme: for uname in selectedNode: node = self.itemDict.get(uname) if node: node.setSelected(True) for uname in selectedEdge: edge = self.edgeDict.get(uname) if edge: edge.setSelected(True) self.releaseLock()
def onOpenDB(self): print('open db-----------------') from db.DBManager import DBManager dbObj = DBManager.instance().getDB() dbPath = dbObj.getDBPath() if not dbPath: return configPath = dbPath + '.config' jsonStr = '' if os.path.exists(configPath): file = open(dbPath + '.config') jsonStr = file.read() file.close() if jsonStr: sceneData = JSONDecoder().decode(jsonStr) self.lock.acquire() # stop item self.stopItem = sceneData.get('stopItem',{}) self.itemDataDict = sceneData.get('codeData',{}) # latest layout codeItemList = sceneData.get('codeItem',[]) for uname in codeItemList: self.addCodeItem(uname) for edgeData in sceneData.get('edgeData', []): self.edgeDataDict[(edgeData[0], edgeData[1])] = edgeData[2] edgeItemList = sceneData.get('edgeItem',[]) for edgePair in edgeItemList: if self.edgeDataDict.get((edgePair[0], edgePair[1]), {}).get('customEdge', False): self._doAddCodeEdgeItem(edgePair[0], edgePair[1], {'customEdge':True}) else: refObj = dbObj.searchRefObj(edgePair[0], edgePair[1]) if refObj: self._doAddCodeEdgeItem(edgePair[0], edgePair[1], {'dbRef':refObj}) if self.itemDict: self.selectOneItem(list(self.itemDict.values())[0]) # scheme schemeDict = sceneData.get('scheme',{}) for name, schemeData in schemeDict.items(): edgeList = schemeData.get('edge',[]) edgeDict = {} for edgeData in edgeList: edgeDict[(edgeData[0], edgeData[1])] = {} schemeData['edge'] = edgeDict self.scheme = schemeDict self.lock.release() else: print('no config file: ' + configPath)
def _addRefs(self, refStr, entStr, inverseEdge = False, maxCount = -1): from db.DBManager import DBManager from ui.CodeUIItem import CodeUIItem dbObj = DBManager.instance().getDB() scene = self itemList = self.selectedItems() refNameList = [] for item in itemList: if not isinstance(item, CodeUIItem): continue uniqueName = item.getUniqueName() entList, refList = dbObj.searchRefEntity(uniqueName, refStr, entStr) # add to candidate candidateList = [] for ithEnt, entObj in enumerate(entList): entName = entObj.uniquename() refObj = refList[ithEnt] # get lines metricRes = entObj.metric(('CountLine',)) metricLine = metricRes.get('CountLine',1) line = metricLine if metricLine else 0 candidateList.append([entName, refObj, line]) # sort candidate if maxCount > 0: candidateList.sort(key = lambda element: element[2], reverse=True) addedList = [] for ithRef, candidate in enumerate(candidateList): entName = candidate[0] refObj = candidate[1] res, refItem = scene._doAddCodeItem(entName) if res: addedList.append(entName) if inverseEdge: scene._doAddCodeEdgeItem(uniqueName, entName, {'dbRef':refObj}) else: scene._doAddCodeEdgeItem(entName, uniqueName, {'dbRef':refObj}) if len(addedList) >= maxCount > 0: break refNameList += addedList return refNameList
def _addCallPaths(self, srcName, tarName): from db.DBManager import DBManager from ui.CodeUIItem import CodeUIItem dbObj = DBManager.instance().getDB() if not dbObj: return [] srcItem = self.itemDict.get(srcName, None) tarItem = self.itemDict.get(tarName, None) if not srcItem or not tarItem or not isinstance(srcItem, CodeUIItem) or not isinstance(tarItem, CodeUIItem) or\ not srcItem.isFunction() or not tarItem.isFunction(): return [] entList, refList = dbObj.searchCallPaths(srcName, tarName) for entName in entList: self._doAddCodeItem(entName) for refObj in refList: self._doAddCodeEdgeItem(refObj[0], refObj[1], {'dbRef':refObj[2]}) return entList
def addSimilarCodeItem(self): itemList = self.selectedItems() if not itemList: return item = itemList[0] from ui.CodeUIItem import CodeUIItem from ui.CodeUIEdgeItem import CodeUIEdgeItem from db.DBManager import DBManager if not isinstance(item, CodeUIItem): return db = DBManager.instance().getDB() name = item.name uname = item.getUniqueName() if not db or not name or not item.isFunction(): return ents = db.search(name, 'function') bestEntList = [] if not ents: return for ent in ents: if ent and ent.name() == name: bestEntList.append(ent) for ent in bestEntList: entUname = ent.uniquename() res, entItem = self.addCodeItem(entUname) if self.edgeDict.get((uname, entUname)) or self.edgeDict.get((entUname, uname)): continue if uname == entUname: continue if not entItem.customData.get('hasDef'): self.addCustomEdge(entUname, uname) else: self.addCustomEdge(uname, entUname)
def __init__(self, uniqueName, parent = None, scene = None): super(CodeUIItem, self).__init__(parent, scene) self.setFlag(QtGui.QGraphicsItem.ItemIsMovable) self.setFlag(QtGui.QGraphicsItem.ItemIsSelectable) self.setFlag(QtGui.QGraphicsItem.ItemIsFocusable) self.setAcceptDrops(True) self.setAcceptHoverEvents(True) self.uniqueName = uniqueName from db.DBManager import DBManager from UIManager import UIManager scene = UIManager.instance().getScene() dbObj = DBManager.instance().getDB() entity = dbObj.searchFromUniqueName(self.uniqueName) self.name = '' self.displayName = '' self.lines = 0 self.kindName = '' self.kind = ITEM_UNKNOWN self.titleFont = QtGui.QFont('tahoma', 8) self.fontSize = QtCore.QSize() self.commentSize = QtCore.QSize() self.lineHeight = 0 self.isConnectedToFocusNode = False if entity: self.setToolTip(entity.longname()) self.name = entity.name() self.buildDisplayName(self.name) comment = scene.itemDataDict.get(self.uniqueName, {}).get('comment','') self.buildCommentSize(comment) self.kindName = entity.kindname() metricRes = entity.metric(('CountLine',)) metricLine = metricRes.get('CountLine',1) if metricLine: self.lines = metricLine kindStr = self.kindName.lower() # 自定义数据 self.customData = {} if kindStr.find('function') != -1 or kindStr.find('method') != -1: self.kind = ITEM_FUNCTION # 找出调用者和被调用者数目 callerList = dbObj.searchRefEntity(self.uniqueName, 'callby','function, method', True)[0] calleeList = dbObj.searchRefEntity(self.uniqueName, 'call','function, method', True)[0] self.customData['nCaller'] = len(callerList) self.customData['nCallee'] = len(calleeList) self.customData['callerR'] = self.getCallerRadius(len(callerList)) self.customData['calleeR'] = self.getCallerRadius(len(calleeList)) elif kindStr.find('attribute') != -1 or kindStr.find('variable') != -1 or kindStr.find('object') != -1: self.kind = ITEM_VARIABLE self.color = QtGui.QColor(255,198,217) elif kindStr.find('class') != -1 or kindStr.find('struct') != -1: self.kind = ITEM_CLASS self.color = QtGui.QColor(154,177,209) else: self.kind = ITEM_UNKNOWN self.color = QtGui.QColor(195,195,195) if self.kind == ITEM_FUNCTION or self.kind == ITEM_VARIABLE: if not entity: self.color = QtGui.QColor(190,228,73) else: defineList, defineRefList = dbObj.searchRefEntity(uniqueName, 'definein') name = '' hasDefinition = True if not defineList: defineList, defineRefList = dbObj.searchRefEntity(uniqueName, 'declarein') hasDefinition = False # if 'pure' in self.kindName: # hasDefinition = False self.customData['hasDef'] = hasDefinition if defineList: declareEnt = defineList[0] if declareEnt.kindname().lower().find('class') != -1 or \ declareEnt.kindname().lower().find('struct') != -1: name = declareEnt.name() self.customData['className'] = name self.color = name2color(name) elif self.kind == ITEM_CLASS: self.color = name2color(self.name) self.displayScore = 0 self.targetPos = self.pos() # 用于动画目标 self.isHover = False self.selectCounter = 0 self.selectTimeStamp = 0
def getEntity(self): from db.DBManager import DBManager return DBManager.instance().getDB().searchFromUniqueName(self.uniqueName)
def showMainUI(self): if not self.mainUI: UIManager.uiMgr.mainUI = mainwindow.MainUI() self.mainUI.show() def getScene(self): return self.scene def getSymbolScene(self): return self.symScene def getUISetting(self): return self.uiSetting class UISettings(object): def __init__(self): pass if __name__ == "__main__": app = QtGui.QApplication(sys.argv) from db.DBManager import DBManager dbObj = DBManager.instance() uiObj = UIManager.instance() uiObj.showUI() sys.exit(app.exec_())
def onSearch(self): searchWord = self.inputEdit.text() searchKind = self.kindEdit.text() searchFile = self.fileEdit.text() searchLine = self.lineBox.value() print('-----------------------------------------------------------') print('search word', searchWord) print('search', searchFile) db = DBManager.instance().getDB() if not db: return ents = db.search(searchWord, searchKind) self.resultList.clear() bestEntList = [] if not ents: return for ent in ents: if ent and searchWord in ent.longname(): bestEntList.append(ent) if searchFile and len(ents) < 100: entList = bestEntList bestEntList = [] bestEntDist = [] for ent in entList: # refs = ent.refs() refs = db.searchRef(ent.uniquename()) if not refs: continue fileNameSet = set() lineDist = 10000000 for ref in refs: if not ref: continue fileEnt = ref.file() line = ref.line() column = ref.column() fileNameSet.add(fileEnt.longname()) if fileEnt.longname() == searchFile: lineDist = min(lineDist, math.fabs(line - searchLine)) for filename in fileNameSet: print('filename', filename) if searchFile in fileNameSet and ent.name() in searchWord: print('in filename', ent.longname(), ent.name(), lineDist) bestEntList.append(ent) bestEntDist.append(lineDist) if searchLine > -1: minDist = 10000000 bestEnt = None for i, ent in enumerate(bestEntList): if bestEntDist[i] < minDist: minDist = bestEntDist[i] bestEnt = bestEntList[i] bestEntList = [bestEnt] bestItem = None for i, ent in enumerate(ents): if i > 200: break resItem = ResultItem(ent) if len(bestEntList) > 0 and ent == bestEntList[0]: bestItem = resItem self.resultList.addItem(resItem) if bestItem: self.resultList.setCurrentItem(bestItem)
def __init__(self, uniqueName, parent = None, scene = None): super(CodeUIItem, self).__init__(parent) self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable) self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable) self.setFlag(QtWidgets.QGraphicsItem.ItemIsFocusable) self.setAcceptDrops(True) self.setAcceptHoverEvents(True) self.uniqueName = uniqueName from db.DBManager import DBManager from UIManager import UIManager scene = UIManager.instance().getScene() dbObj = DBManager.instance().getDB() entity = dbObj.searchFromUniqueName(self.uniqueName) self.name = '' self.displayName = '' self.lines = 0 self.kindName = '' self.kind = ITEM_UNKNOWN self.titleFont = QtGui.QFont('tahoma', 8) self.fontSize = QtCore.QSize() self.commentSize = QtCore.QSize() self.lineHeight = 0 self.isConnectedToFocusNode = False if entity: self.setToolTip(entity.longname()) self.name = entity.name() self.buildDisplayName(self.name) comment = scene.itemDataDict.get(self.uniqueName, {}).get('comment','') self.buildCommentSize(comment) self.kindName = entity.kindname() metricRes = entity.metric(('CountLine',)) metricLine = metricRes.get('CountLine',1) if metricLine: self.lines = metricLine kindStr = self.kindName.lower() # 自定义数据 self.customData = {} if kindStr.find('function') != -1 or kindStr.find('method') != -1: self.kind = ITEM_FUNCTION # 找出调用者和被调用者数目 callerList = dbObj.searchRefEntity(self.uniqueName, 'callby','function, method', True)[0] calleeList = dbObj.searchRefEntity(self.uniqueName, 'call','function, method', True)[0] self.customData['nCaller'] = len(callerList) self.customData['nCallee'] = len(calleeList) self.customData['callerR'] = self.getCallerRadius(len(callerList)) self.customData['calleeR'] = self.getCallerRadius(len(calleeList)) elif kindStr.find('attribute') != -1 or kindStr.find('variable') != -1 or kindStr.find('object') != -1: self.kind = ITEM_VARIABLE self.color = QtGui.QColor(255,198,217) elif kindStr.find('class') != -1 or kindStr.find('struct') != -1: self.kind = ITEM_CLASS self.color = QtGui.QColor(154,177,209) else: self.kind = ITEM_UNKNOWN self.color = QtGui.QColor(195,195,195) if self.kind == ITEM_FUNCTION or self.kind == ITEM_VARIABLE: if not entity: self.color = QtGui.QColor(190,228,73) else: defineList, defineRefList = dbObj.searchRefEntity(uniqueName, 'definein') name = '' hasDefinition = True if not defineList: defineList, defineRefList = dbObj.searchRefEntity(uniqueName, 'declarein') hasDefinition = False # if 'pure' in self.kindName: # hasDefinition = False self.customData['hasDef'] = hasDefinition if defineList: declareEnt = defineList[0] if declareEnt.kindname().lower().find('class') != -1 or \ declareEnt.kindname().lower().find('struct') != -1: name = declareEnt.name() self.customData['className'] = name self.color = name2color(name) elif self.kind == ITEM_CLASS: self.color = name2color(self.name) self.displayScore = 0 self.targetPos = self.pos() # 用于动画目标 self.isHover = False self.selectCounter = 0 self.selectTimeStamp = 0