def applyValueChange(self): if not os.path.exists('value_change.py'): print('no value_change.py file exists') return import value_change changeRule = value_change.changeRule for entry in changeRule: for fn in entry['files']: fullname = os.path.join(self.levelDir, fn) if not os.path.exists(fullname): continue levelData = loadJsonData(fullname) nodesData = levelData['nodes'] hasUpdates = False for node in nodesData: if node['type'] != entry['node_type']: continue args = node['args'] attrType = entry['attr_type'] if attrType in args: handleFunc = getattr(value_change, entry['function']) args[attrType] = handleFunc(args[attrType]) hasUpdates = True if hasUpdates: dumpJsonData(fullname, levelData) print('update ', fn)
def attachMetaToFile(self, levelFn): """ 给levelFn文件添加meta字段 """ levelData = loadJsonData(os.path.join(self.levelDir, levelFn)) if levelData.get('mets', None) is not None: return print('attach meta to file', levelFn) usedNodeKeys = set([node['type'] for node in levelData['nodes']]) metas = [] for nodeDef in self.nodesDefData: if nodeDef['name'][-1] in usedNodeKeys: metas.append(nodeDef) levelData['meta'] = metas # oldLevelData = loadJsonData(os.path.join(self.levelDir, levelFn)) # dumpJsonData(os.path.join(self.levelDir, '%s_old.json' % pureName(levelFn)), # oldLevelData) dumpJsonData(os.path.join(self.levelDir, levelFn), levelData)
def saveTemplate(self): dumpJsonData('meta/template.json', self.templateData)
def upgradeLevelFile(self, levelFn, changeRuleEntry, module): """ 更新某个给定的level文件, :param levelFn: 节点文件名称 :param nodeKeyOnlyInOld: 需要删除的节点key :param attrsUpdatePlans: 相同节点key,的节点属性更新计划 :return: """ logger = logging.getLogger('type_change') levelData = loadJsonData(os.path.join(self.levelDir, levelFn)) levelMeta = levelData['meta'] nodeKeyOnlyInOld, keysCommon = self.check(levelMeta, self.nodesDefData) attrsUpdatePlans = self.attributeUpdatePlans(keysCommon, levelMeta, self.nodesDefData) if len(attrsUpdatePlans) == 0 and len(nodeKeyOnlyInOld) == 0: return id2Type = dict([(node['id'], node['type']) for node in levelData['nodes']]) id2Pos = dict([(node['id'], node['pos']) for node in levelData['nodes']]) hasUpdates = False hasTypeChange = False if len(nodeKeyOnlyInOld) != 0: # 删除已经不用的节点,及其附带的边 self.removeNodesAndEdges(nodeKeyOnlyInOld, levelData) hasUpdates = True newNodesData = [] filterEdgesData = [] # 对新老共有的节点,增删属性 for node in levelData['nodes']: nodeKey = node['type'] if nodeKey in attrsUpdatePlans: deletes = attrsUpdatePlans[nodeKey].get('deletes', []) addPlans = attrsUpdatePlans[nodeKey].get('adds', []) updatePlans = attrsUpdatePlans[nodeKey].get('updates', []) args = node['args'] updatedArgs = {} for argKey in list(args.keys()): if argKey in deletes: hasUpdates = True continue updatedArgs[argKey] = args[argKey] for addPlan in addPlans: if 'default' in addPlan: hasUpdates = True updatedArgs[addPlan['attrId']] = addPlan['default'] for updatePlan in updatePlans: updateTypes = updatePlan['update_types'] argKey = updatePlan['attrId'] for updateType in updateTypes: if updateType == 'add_default': # 如果对应属性没有赋值,且没有入边,将这个默认值加到level数据中 if argKey not in args and \ not self.hasInEdge(levelData['edges'], node['id'], argKey): updatedArgs[argKey] = updatePlan['new_default'] elif updateType == 'delete_default': if argKey in updatedArgs and updatedArgs[ argKey] == updatePlan['old_default']: del updatedArgs[argKey] elif updateType == 'update_default': if argKey in args and args[argKey] == updatePlan[ 'old_default']: updatedArgs[argKey] = updatePlan['new_default'] elif updateType == 'type_change': assert changeRuleEntry is not None, 'no change rule for %s' % levelFn changeAttrType = changeRuleEntry['attr_type'] print('change attr', changeAttrType) if changeAttrType in args: # 被改变的数据具有参数值 handleFunc = getattr( module, changeRuleEntry['function']) # args[changeAttrType] = handleFunc(args[changeAttrType]) updatedArgs[changeAttrType] = handleFunc( args[changeAttrType]) hasTypeChange = True elif self.hasInEdge(levelData['edges'], node['id'], changeAttrType): # 检查该属性是否有附带的边 # 有附带的边, 删除该边 filterEdgesData.append({ 'toId': node['id'], 'toItemId': changeAttrType }) hasUpdates = True node['args'] = updatedArgs newNodesData.append(node) # 对于已经删除的属性构成的边,要删除所有附带边 newEdgesData = [] for edge in levelData['edges']: # 检验出边 startNodeKey = id2Type[edge['start']] startItemKey = edge['startItemId'] if startNodeKey in attrsUpdatePlans: deletes = attrsUpdatePlans[startNodeKey].get('deletes', []) if startItemKey in deletes: hasUpdates = True continue # 检验入边 endNodeKey = id2Type[edge['end']] endItemKey = edge['endItemId'] if endNodeKey in attrsUpdatePlans: deletes = attrsUpdatePlans[endNodeKey].get('deletes', []) if endItemKey in deletes: hasUpdates = True continue isFiltered = False for feData in filterEdgesData: if feData['toId'] == edge['end'] and feData[ 'toItemId'] == edge['endItemId']: startPos, endPos = id2Pos[edge['start']], id2Pos[ edge['end']] msg = 'delete edge of %s, %s|%s (%.1f,%.1f) --> %s|%s (%.1f, %.1f)' % ( levelFn, edge['startNodeName'], edge['startItemName'], startPos['x'], startPos['y'], edge['endNodeName'], edge['endItemName'], endPos['x'], endPos['y']) isFiltered = True logger.info(msg) break if not isFiltered: newEdgesData.append(edge) if not hasUpdates: return levelData['nodes'] = newNodesData levelData['edges'] = newEdgesData if hasUpdates: metas = [] usedNodeKeys = set([node['type'] for node in levelData['nodes']]) for nodeDef in self.nodesDefData: if nodeDef['name'][-1] in usedNodeKeys: metas.append(nodeDef) levelData['meta'] = metas # 将老文件进行备份,命名为XX_old.json # oldLevelData = loadJsonData(os.path.join(self.levelDir, levelFn)) # dumpJsonData(os.path.join(self.levelDir, '%s_old.json' % pureName(levelFn)), oldLevelData) # 将levelData写入到文件中 dumpJsonData(os.path.join(self.levelDir, levelFn), levelData) print('done', levelFn)
def save_prefs(prefs): dumpJsonData(os.path.join('meta', 'prefs.json'), prefs)