def __init__(self, db_table): """Initializing """ self.db_manager = DBManager() self.db_table = db_table self.size = 0 self.features = pd.DataFrame() self.extracted_data = dict() self.common_keys = dict()
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)
class SignInBiz(object): def __init__(self): self.dbhelper = DBManager() def checkUser(self, userName, passWord): sql = 'select user_id,user_name,password from tc_user where user_name=%s and password=%s' dict = self.dbhelper.executeQuery_one(sql, (userName, passWord)) if dict != {}: return dict['user_id'] return 0
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 save_to_db(self, data, address): try: dbman = DBManager(self.properties) dbman.connect() dbman.insert_raw_job(address, data) dbman.disconnect() except mdb.Error, e: print "Exception: %d: %s" % (e.args[0], e.args[1])
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
class ProjectManager(object): def __init__(self): self.mydb = DBManager() def getObjectAll(self): sql="select * from tc_project" dict = self.mydb.executeQuery_all(sql) return dict def getTotalRecordCount(self,projectName=None): '''获取总记录数''' sqlBuilder = [] sqlBuilder.append("select * from tc_project where 1=1") if projectName is not None: sqlBuilder.append(" and pro_name like '%s'" %('%'+projectName+'%') ) recordCount = self.mydb.executeQuery_count(sql=''.join(sqlBuilder)) return recordCount def getObjectByCondition(self,projectName=None,index=0,pageRecord=10): '''带条件查询''' sqlBuilder = [] sqlBuilder.append("select * from tc_project where 1=1") if projectName is not None: sqlBuilder.append(" and pro_name like '%s'" %('%'+projectName+'%')) sqlBuilder.append(" order by update_time desc limit %d,%d" %(index,pageRecord)) # print(''.join(sqlBuilder)) rsData = self.mydb.executeQuery_all(sql=''.join(sqlBuilder)) if rsData: return rsData return () def createProject(self,projectName=None,remark=None,creator=None): '''创建项目''' sql="insert into tc_project (pro_name,update_time,creator,remark) values(%s,%s,%s,%s)" now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') params = (projectName,now_time,creator,remark) count = self.mydb.insert_params(sql,params) return count def deleteByCondition(self,projectId=0): sql="delete from tc_project where pro_id=%s" count = self.mydb.delete_params(sql,(projectId)) return count def updateByCondition(self,projectId=0,projectName=None,remark=None,creator=None): sql="update tc_project set pro_name=%s,remark=%s,update_time=%s,creator=%s where pro_id=%s" now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') params=(projectName,remark,now_time,creator,projectId) count = self.mydb.update_params(sql,params) return count
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 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): self.mydb = DBManager()
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
class InterfaceManager(object): def __init__(self): self.mydb = DBManager() def getTotalRecordCount(self, id=None): '''获取总记录数''' sqlBuilder = [] sqlBuilder.append("select * from tc_interface where 1=1") if id is not None: sqlBuilder.append(" and projectId=%s" % id) recordCount = self.mydb.executeQuery_count(''.join(sqlBuilder)) LOG.info(''.join(sqlBuilder)) return recordCount def getProjectName(self): '''获取项目名称和id''' sql = "select pro_id,pro_name from tc_project" rsData = self.mydb.executeQuery_all(sql) return rsData def getInterFaceByCondition(self, id=-1, index=0, pageRecord=20): '''根据条件查询接口信息,并分页''' sqlBuilder = [] sqlBuilder.append( "select id,faceName,address,requestType,dataType,contentType,caseCount,createTime,projectName,projectId,userId from tc_interface where 1=1" ) if id != 0: sqlBuilder.append(" and projectId=%d" % int(id)) sqlBuilder.append(" order by createTime desc limit %d,%d" % (index, pageRecord)) rsData = self.mydb.executeQuery_all(''.join(sqlBuilder)) LOG.info(''.join(sqlBuilder)) return rsData def addInterface(self, Interface=None): '''插入数据''' sqlBuilder = [] sqlBuilder.append( "insert into tc_interface (faceName,address,requestType,dataType,contentType,createTime,projectName,projectId,userId)" ) sqlBuilder.append(" values(%s,%s,%s,%s,%s,%s,%s,%s,%s)") params = (Interface.faceName, Interface.address, Interface.requestType, Interface.dataType, Interface.contentType, Interface.createTime, Interface.projectName, Interface.projectId, Interface.userId) LOG.info(''.join(sqlBuilder)) count = self.mydb.insert_params(sql=''.join(sqlBuilder), params=params) return count def updateInterface(self, Interface=None): '''修改数据''' sqlBuilder = [] sqlBuilder.append( "update tc_interface set faceName=%s,address=%s,requestType=%s,dataType=%s,contentType=%s,createTime=%s,caseCount=%s,projectName=%s,projectId=%s,userId=%s" ) sqlBuilder.append(" where id=%s") params = (Interface.faceName, Interface.address, Interface.requestType, Interface.dataType, Interface.contentType, Interface.createTime, Interface.caseCount, Interface.projectName, Interface.projectId, Interface.userId, Interface.faceId) LOG.info(''.join(sqlBuilder)) LOG.info(params) count = self.mydb.update_params(sql=''.join(sqlBuilder), params=params) return count def deleteByCondition(self, id): '''删除数据''' sql = "delete from tc_interface where id=%s" count = self.mydb.delete_params(sql, (id)) return count
def getEntity(self): from db.DBManager import DBManager return DBManager.instance().getDB().searchFromUniqueName(self.uniqueName)
def __init__(self): self.dbhelper = DBManager()
class Extractor: """ """ def __init__(self, db_table): """Initializing """ self.db_manager = DBManager() self.db_table = db_table self.size = 0 self.features = pd.DataFrame() self.extracted_data = dict() self.common_keys = dict() def extract_features(self, lookup): """ Extract new features from DB. Mapping each feature with his data (from DB). Handling missing data. Clean (if choose it) sparse features by THRESHOLD Normalize features data (LabelEncoder) Close DB connection by DBManager :return: """ self.build(lookup) self.extract() self.data_handling(clean_sparse=False) self.db_manager.db_close() def build(self, entities): """ :param entities: :return: """ try: self.size = len(entities) self.prepare_database() for entity in entities: if entity: self.fetch( Utils.normalize_name(Utils.normalize_uri(entity))) except Error as e: print("Error while build features", e) def fetch(self, entity): """ Fetch from database the entity data (if exists) :param entity: :return: """ # return a list of items result = self.db_manager.db_fetch(self.db_table, entity) if result and result is not None and len(result) > 0: for item in result: if 'name' in item.keys() and item['name']: self.extracted_data[item['name']] = item if item['data']: self.prepare_common_keys(item['data']) def prepare_common_keys(self, item): """ :param item: :return: """ for key in item.keys(): if key in self.common_keys: self.common_keys[key] = self.common_keys[key] + 1 else: self.common_keys[key] = 1 def extract(self): """ :return: """ for feature in self.common_keys.keys(): values = [] for i in range(0, self.size): values.append(None) for i, name in enumerate(self.extracted_data): entity = self.extracted_data.get(name) if feature in entity['data'].keys(): values[i] = entity['data'][feature] self.features[feature] = values def data_handling(self, clean_sparse=True): """ Clean sparse features (if it choose) Handling missing data Fit & Transform data (Normalize) :param clean_sparse: :return: """ if clean_sparse: self.clean_sparse_features() le = preprocessing.LabelEncoder() for feature, feature_data in self.features.items(): feature_type = Utils.find_feature_type(self.features[feature]) values = Utils.init_values(feature_type, self.size) for i, value in enumerate(feature_data): if feature_type == list: values[i] = 0 if value is None else len(value) else: if type(value) == list: value = value[0] values[i] = "" if value is None else value.replace( "\"", "") feature_type = Utils.find_feature_type(values) values = Utils.missing_data(feature_type, values) values = le.fit_transform(values) self.features[feature] = values def clean_sparse_features(self): """ Clean sparse features by THRESHOLD :return: """ # Common features sorted var = { k: v for k, v in reversed( sorted(self.common_keys.items(), key=lambda item: item[1])) } for feature in self.features.keys(): values_count = self.features[feature].value_counts() if round(len(values_count) / len(self.features[feature]), 2) < THRESHOLD: self.features.drop(feature, 1, inplace=True) def prepare_database(self): """ Check if DB is connected, else establish a connection :return: """ if self.db_manager and self.db_manager is not None and self.db_manager.is_connected( ): return True elif self.db_manager is not None and not self.db_manager.is_connected( ): self.db_manager.db_connect() return self.db_manager.is_connected() return False
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_())
class Parser: """ Parser Freebase triples into DB tables according to entity type """ def __init__(self): """ Initializing """ self.db_manager = DBManager() def init_database(self): """ :return: """ if self.db_manager is not None and not self.db_manager.is_connected(): self.db_manager.db_connect() self.db_manager.db_init() return self.db_manager.is_connected() return False def read_data(self, file): """ :param file: :return: """ try: iTotal = 0 current_mid = "" current_topic = dict() with gzip.open(file, 'rt') as f: for line in f: subject, predicate, object = Utils.parse_triple(line) if subject == current_mid: if predicate not in current_topic: current_topic[predicate] = [object] else: current_topic[predicate].append(object) elif current_mid: self.prepare_to_save(subject, current_topic) current_topic.clear() current_mid = subject iTotal = iTotal + 1 if 0 == (iTotal % 1000000): print("iTotal: ", iTotal) print() except Error as e: print("Error while reading file", e) def prepare_to_save(self, subject, current_topic): """ :param subject: :param current_topic: :return: """ if '/type/object/type' in current_topic: for iType in current_topic['/type/object/type']: for allowed_type_key, allowed_type_table in ALLOWED_ENTITIES.items( ): if re.search(allowed_type_key, iType): # Save to DB if self.prepare_database(): self.save_to_database(allowed_type_table, subject, current_topic) break def prepare_database(self): """ :return: """ if self.db_manager and self.db_manager is not None and self.db_manager.is_connected( ): return True elif self.db_manager is not None and not self.db_manager.is_connected( ): self.db_manager.db_connect() return self.db_manager.is_connected() return False def save_to_database(self, table, subject, current_topic): """ :param table: :param subject: :param current_topic: :return: """ current_topic = Utils.handle_duplicate(current_topic) current_topic = Utils.handle_language(current_topic) if current_topic is None or len(current_topic) == 0: return name = '' if '/type/object/name' in current_topic.keys(): if isinstance(current_topic['/type/object/name'], list): name = current_topic['/type/object/name'][0] else: name = current_topic['/type/object/name'] current_topic.pop('/type/object/name') elif 'label' in current_topic.keys(): if isinstance(current_topic['label'], list): name = current_topic['label'][0] else: name = current_topic['label'] current_topic.pop('label') if name and current_topic is not None and len(current_topic) > 0: name = Utils.clean_lang(name) self.db_manager.db_insert(table, name, subject, json.dumps(current_topic))
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 __init__(self): """ Initializing """ self.db_manager = DBManager()