def registerRule(name, classType): if MergeRuleFactory.hasRule( name ): ErrorHandler.error("MergeRuleFactory registerRule already has %s" % name) pass MergeRuleFactory.rules[name] = classType pass
def finalise(self): if self._onFinalise() is False: ErrorHandler.error("Parser finalise`s failure on %s", self.__repr__()) return False pass return True pass
def initialise(self): if self._onInitialise() is False: ErrorHandler.error(" Parser initialise`s failure on %s" % self.__repr__()) return False pass return True pass
def initLogging(self, logDir): logger = Logger() logger.initialise(logDir) ErrorHandler.importantMessage("Log created %s" % logger.getFileName() ) self.errorListener.setLogger(logger) return True pass
def getRule(name, params): if MergeRuleFactory.hasRule(name) is False: ErrorHandler.error("MergeRuleFactory getRule unknown rule %s" %name) return None pass rule = MergeRuleFactory.createRule(name) rule.onParams(params) return rule pass
def _initActionBoth(self, action, query, firstDomRoot, secondDomRoot ): firstResult = query.execute(firstDomRoot) if firstResult is None: ErrorHandler.warning( "xml query failed %s on %s" % (query, self.firstXml) ) return False pass secondResult = query.execute(secondDomRoot) if secondResult is None: ErrorHandler.warning( "xml query failed %s on %s" % (query, self.secondXml) ) return False pass if len(secondResult) != len(firstResult): ErrorHandler.warning( "xml query %s results have incompatible length %i != %i" % (query, len(secondResult),len(firstResult) ) ) return False pass if len(secondResult) != 1: ErrorHandler.warning( "xml query %s only single row result supported , you got %i" % (query, len(secondResult) ) ) return False pass action["Params"]["FirstXmlInput"] = firstResult[0] action["Params"]["SecondXmlInput"] = secondResult[0] return True pass
def _initActionFirst(self, action, query, firstDomRoot ): firstResult = query.execute(firstDomRoot) if firstResult is None: ErrorHandler.warning( "xml query failed %s on %s" % (query, self.firstXml) ) return False pass if len(firstResult) != 1: ErrorHandler.warning( "xml query %s only single row result supported , you got %i" % (query, len(firstResult) ) ) return False pass action["Params"]["FirstXmlInput"] = firstResult[0] return True pass
def merge(self, node1, node2): #TODO VERY SLOW, IMPROVE SPEED isParent1 = node1.hasChildren() isParent2 = node2.hasChildren() if isParent1 != isParent2: ErrorHandler.warning("MergeRuleSectionIntelligence:: nodes in different tree has different structure with same tagName new %s, old %s" % (node1,node2) ) return pass if isParent1 is True: self._mergeSection(node1, node2) pass else: self._mergeNodes(node1, node2) pass pass
def prepareResult(self, result): children = result for i in range(self.backStep): parents = [] for child in children: if child.hasParent() is False: ErrorHandler.error("GrapQuery error backStep is incorrect. %i", self.backStep) pass parent = child.getParent() parents.append(parent) pass children = parents pass return children pass
def _onParams( self, params ): blackList = params.pop("BlackList", None) self.blackList = [] if blackList is None: return pass for query in blackList: result = query.execute(self.secondXmlInput) if result is None: ErrorHandler.warning("init black list error. graph query doesn`t work %s" % query) continue pass self.blackList.extend(result) pass pass
def openDocument(self, pathToXml): document = None try: document = parse(pathToXml) pass except IOError as e: message = "Parser.parseToDom IOError %s in %s " % ( str(e) ,self ) ErrorHandler.warning(message) return None pass except xml.parsers.expat.ExpatError as e: message = "Parser.parseToDom xml.parsers.expat.ExpatError %s in %s " % (str(e) , self ) ErrorHandler.warning(message) return None pass return document pass
def merge(self): firstDomRoot = self.firstXml.getRootNode() secondDomRoot = self.secondXml.getRootNode() for action in self.actions: query = action["Query"] if action["Policy"] == QUERY_POLICY_BOTH: if self._initActionBoth(action, query, firstDomRoot, secondDomRoot) is False: continue pass pass elif action["Policy"] == QUERY_POLICY_ONLY_NEW: if self._initActionFirst(action, query, firstDomRoot) is False: continue pass pass else: ErrorHandler.error("Merge Query Policy not Support %s" % str(action["Policy"]) ) continue pass rule = MergeRuleFactory.getRule( action["RuleName"], action["Params"] ) if rule is None: ErrorHandler.warning( "Merge rule %s don`t exist" % action["RuleName"]) continue pass if rule.run() is False: ErrorHandler.warning( "Merge rule return False %s %s" % (action["RuleName"], query) ) pass pass pass
def run( self ): ErrorHandler.message( "%s is running" % str(self) ) self._success = self._onRun() return self._success pass
def merge(newConfigPath, oldConfigPath, destinationConfigPath, rulesPythonScript, isLogging, logDir, errorReporting, writeBackup): #register rules MergeRuleFactory.initialise() MergeRuleFactory.registerRule("CopyValue", MergeRuleSingleShot) MergeRuleFactory.registerRule("DeleteSection", MergeRuleDeleteNode) MergeRuleFactory.registerRule("CopySection", MergeRuleSectionIntelligence) #register XmlMerger merger = XmlMerger() if merger.initErrorReporting(errorReporting) is False: print "Can`t init error reporting" return False pass if isLogging is True: if logDir is None: logDir = FileSystem.getCurrentDirectory() pass if FileSystem.isDirectory(logDir) is False: ErrorHandler.error("Directory for logs not exist %s " % logDir) return False pass if merger.initLogging(logDir) is False: ErrorHandler.error("can `t init Logger ") return False pass if merger.initialise(newConfigPath, oldConfigPath) is False: ErrorHandler.error("can `t initialise") return False pass #add actions #BE CAREFUL TUPLE WITH ONE ELEMENT = (element,) not (element) because in that case we shall have sequence (e,l,e,m,e,n,t) :-) #merger.addAction( "SingleShot", GraphQuery(("general","systemtotaluptime")) ) #merger.addAction( "SectionIntelligence", GraphQuery(("myvideos",)) ) #merger.addAction( "SectionIntelligence", GraphQuery(("mymusic",)), BlackList = ["scanning"] ) #merger.addAction( "SectionIntelligence", GraphQuery(("viewstates",)) ) content = FileSystem.fileGetContents(rulesPythonScript) execfile( rulesPythonScript, dict(merger = merger , GraphQuery = GraphQuery , QUERY_POLICY_ONLY_NEW = QUERY_POLICY_ONLY_NEW , QUERY_POLICY_BOTH = QUERY_POLICY_BOTH) ) merger.merge() if writeBackup is True: backupPath = FileSystem.setFileExtension(oldConfigPath,"bak.xml") FileSystem.copyFile(oldConfigPath, backupPath) pass if merger.save(destinationConfigPath) is False: ErrorHandler.error("Error saving result %s. Please check directory write permissions." % destinationConfigPath) return False pass return True pass
def initErrorReporting(self, errorReporting): ErrorHandler.addListener(self.errorListener) ErrorHandler.setErrorReporting(errorReporting) return True pass