def registerRule(name, classType):
        if MergeRuleFactory.hasRule( name ):
            ErrorHandler.error("MergeRuleFactory registerRule already has %s" % name)
            pass

        MergeRuleFactory.rules[name] = classType
        pass
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    def finalise(self):
        if self._onFinalise() is False:
            ErrorHandler.error("Parser finalise`s failure on  %s", self.__repr__())
            return False
            pass

        return True
        pass
Exemplo n.º 4
0
    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 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
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
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