Пример #1
0
    def initConfigModule(self, module, configName, configType):
        """
			Build the XML path from the root to the module.
			@type configName : string
			@param configName : the name of the configuration datastore ('running', 'candidate' or 'startup') 
			@type configType : string
			@param configType : the type of the configuration datastore ('state' or 'config') 
		"""

        configuration = self.configurations[configName][configType]

        self.prefixes = self.moduleManager.getPrefixes()

        # Check if module node exists
        ctx = Context(configuration, processorNss=self.prefixes)
        selectednodes = Evaluate("/", ctx)

        root = selectednodes[0]
        #		if len(selectednodes) == 1:
        #			return
        if module.path != "/":
            path = module.path
            tab = path.split('/')

            j = len(tab) - 1
            prefix, name = tab[j].split(':')

            child = configuration.createElementNS(self.prefixes[prefix], name)

            #while j > 0:
            while j > 1:
                xpathRequest = ""
                for i in range(1, j):
                    xpathRequest = xpathRequest + "/" + tab[i]

                ctx = Context(configuration, processorNss=self.prefixes)
                selectednodes = Evaluate(xpathRequest, ctx)

                if len(selectednodes) == 0:
                    # The node does not exist: create it now:
                    prefix, name = tab[j - 1].split(':')
                    new = configuration.createElementNS(
                        self.prefixes[prefix], name)
                    new.appendChild(child)
                    child = new
                    j = j - 1
                else:
                    selectednodes[0].appendChild(child)
                    j = 0
            root.appendChild(child)
Пример #2
0
    def __init__(self, asteriskRootNode, namespace):

        self.namespace = namespace

        NS = {}
        NS['voip'] = self.namespace
        NS['netconf'] = C.NETCONF_XMLNS
        NS['xc'] = C.NETCONF_XMLNS

        self.ctx = Context(asteriskRootNode, processorNss=NS)
        self.root = asteriskRootNode
Пример #3
0
Файл: test.py Проект: zggl/lxir
 def _get_result(self):
     doc = NonvalidatingReader.parseString(self.output,
                                           self.URI_P % self.name)
     ctxt = Context(doc, processorNss=NSS)
     nodes = Evaluate("//mml:math", context=ctxt)
     assert len(nodes) == 1
     node = nodes[0]
     node.removeAttributeNS(None, u'begin-id')
     node.removeAttributeNS(None, u'end-id')
     o = StringIO.StringIO()
     Print(node, stream=o)
     return o.getvalue()
Пример #4
0
    def updateConfig(self, configName, configType):
        """
			Replace the module root nodes when it is unfresh in self.runningConfig.
			@type configName : string
			@param configName : the name of the configuration datastore ('running', 'candidate' or 'startup') 
			@type configType : string
			@param configType : the type of the configuration datastore ('state' or 'config') 
		"""

        configuration = self.configurations[configName][configType]

        self.prefixes = self.moduleManager.getPrefixes()

        for module in self.moduleManager.getModules():

            if (not module.isConfigFresh(configName, configType)):
                try:
                    if configType == "config":
                        moduleReply = module.getConfig(configName)
                    elif configType == "state":
                        moduleReply = module.get(configName)

                    if moduleReply.isError():
                        msg = "Error: DatastoreManager:updateConfig(): Module %s: %s" % (
                            module.name, moduleReply.error_message)
                        LogManager.getInstance().logError(msg)
                    else:
                        module.updateConfigTime(configName, configType)
                        newNode = moduleReply.getXMLNodeReply()
                        ctx = Context(configuration,
                                      processorNss=self.prefixes)
                        nodes = Evaluate(module.path, ctx)

                        if len(nodes) == 1:
                            parentNode = nodes[0]
                            if parentNode.hasChildNodes():
                                find = False
                                for oldNode in parentNode.childNodes:
                                    if oldNode.tagName == newNode.tagName:
                                        parentNode.replaceChild(
                                            newNode, oldNode)
                                        find = True
                                if not find:
                                    parentNode.appendChild(newNode)
                                #oldNode.appendChild(newNode)
                            else:
                                parentNode.appendChild(newNode)

                except Exception, exp:
                    LogManager.getInstance().logError("Module %s: %s" %
                                                      (module.name, str(exp)))
Пример #5
0
def findEditOp():
    result = {}
    NS_Netconf = "urn:ietf:params:xml:ns:netconf:base:1.0"
    NS_YencaP = "urn:loria:madynes:ensuite:yencap:1.0"
    NS_RIP_Module = "urn:loria:madynes:ensuite:yencap:module:RIP:1.0"

    NSS = {u"xc": NS_Netconf, u"yp": NS_YencaP, u"rip": NS_RIP_Module}
    ctx = Context(doc, processorNss=NSS)
    nodes = Evaluate(u"//*[@xc:operation]", ctx)

    print nodes

    for node in nodes:
        result[node] = node.getAttributeNS(NS_Netconf, "operation")

    return result
Пример #6
0
    def selectNodes(self):

        nodes = []
        # Select the nodes
        for xpathRequest in self.xpathRequestList:
            try:
                ctx = Context(self.initialNode, processorNss=self.NSS)
                selectednodes = Evaluate(xpathRequest, ctx)
                for node in selectednodes:
                    if node.nodeType == Node.DOCUMENT_NODE:
                        node = node.documentElement
                    # Append the node if not already in the list
                    if node not in nodes:
                        nodes.append(node)

            except Exception, exp:
                print str(exp)
Пример #7
0
	def checkAccessEditConfig(self, config, session):
		scopes = self.getActiveScopes(session, "w")
		
		# Building the list of authorized nodes
		authorized_nodes = []
		for scope in scopes:
			try:
				ctx = Context(config.documentElement, processorNss = self.prefixes)
				l = Evaluate(scope, ctx)
				for node in l:
					# Il faudrait verifier que 
					#	- ce noeud n'est pas deja dans la liste pour eviter les doublons
					#	- ce n'est pas un fils d'un noeud deja present dans la liste. Car si les droits d'acces en lecture sont donnes pour un parent, ca n'apprend rien de plus d'ajouter ce noeud
					#	- si ce noeud est un parent d'un noeud de la liste, il faut degager l'enfant car il ne sert plus a rien.
					# A FINIR DONC...
					authorized_nodes.append(node)
			except Exception,exp:
				print str(exp)
Пример #8
0
    def checkNestingValidity(self, prefixes):

        prefixes["xc"] = C.NETCONF_XMLNS
        xpathRequest = "//*[@xc:operation]"
        ctx = Context(self.config, processorNss=prefixes)
        selectedNodes = Evaluate(xpathRequest, ctx)

        if self.defaultOperation == "replace":
            if len(selectedNodes) != 0:
                #print "When default operation is replace, no other operation attribute is allowed."
                moduleReply = ModuleReply(
                    error_type=ModuleReply.APPLICATION,
                    error_tag=ModuleReply.UNKNOWN_ELEMENT,
                    error_severity=ModuleReply.ERROR,
                    error_message=
                    "When default operation is replace, no other operation attribute is allowed."
                )
                moduleReply.addErrorInfo("bad-element", selectedNodes[0])
                self.operationReply.setError()
                self.operationReply.setNode(moduleReply.getXMLNodeReply())
                return
            else:
                return

        elif self.defaultOperation == "none":
            if len(selectedNodes) == 0:
                #print "When default operation is none, at least one operation attribute must appear."
                moduleReply = ModuleReply(
                    error_type=ModuleReply.APPLICATION,
                    error_tag=ModuleReply.MISSING_ELEMENT,
                    error_severity=ModuleReply.ERROR,
                    error_message=
                    "When default operation is none, at least one operation attribute must appear."
                )
                self.operationReply.setError()
                self.operationReply.setNode(moduleReply.getXMLNodeReply())
                return
            else:
                # Check the internal nestings, independently of default operation.
                self.checkInternalNestingValidity(selectedNodes)

        elif self.defaultOperation == "merge":
            # Check the internal nestings, independently of default operation.
            valid = self.checkInternalNestingValidity(selectedNodes)
Пример #9
0
    def selectNodes(self):

        nodes = []
        # Select the nodes
        for xpathRequest in self.xpathRequestList:
            try:
                # add by manu 04/01
                # add 1,1 None parameter to ensure NSS is
                # well understanded by Context()
                ctx = Context(self.initialNode, 1, 1, None, self.NSS)
                selectednodes = Evaluate(xpathRequest, ctx)
                for node in selectednodes:
                    if node.nodeType == Node.DOCUMENT_NODE:
                        node = node.documentElement
                    # Append the node if not already in the list
                    if node not in nodes:
                        nodes.append(node)

            except Exception, exp:
                print str(exp)
Пример #10
0
    def removeModule(self, module, configName, configType):
        """
			Remove the data of a specific module from the XML datastore.
			@type module : Module
			@param module : the module to remove
			@type configName : string
			@param configName : the name of the configuration datastore ('running', 'candidate' or 'startup') 
			@type configType : string
			@param configType : the type of the configuration datastore ('state' or 'config')
		"""

        configuration = self.configurations[configName][configType]

        self.prefixes = self.moduleManager.getPrefixes()

        ctx = Context(configuration, processorNss=self.prefixes)
        selectednodes = Evaluate(module.path, ctx)

        for selectednode in selectednodes:
            selectednode.parentNode.removeChild(selectednode)
Пример #11
0
 def xml_xpath(self, query):
     """
     Execute an XPath query with this object standing in for the
     context node.  The namespace mappings are taken from the root
     binding object.  There are no variable bindings.  Most XPath
     is supported.
     query - a unicode object expressing an XPath
     The return value depends on the XPath expression (expr)
     - If expr reults in an XPath string, the return value is a
     Python Unicode object
     - If expr reults in an XPath number, the return value is a
     Python float
     - If expr reults in an XPath boolean, the return value is a
     Python bool object
     - If expr reults in an XPath node set, the return value is a
     Python list (always a list, even if empty, or a node with just
     one entry)
     """
     ctx = Context(self, processorNss=self.rootNode.xmlns_prefixes)
     result = Evaluate(query, context=ctx)
     return result
Пример #12
0
def fetch(params, debug=None, nopost=None):
    if (not params.has_key('resultxpath')):
        return None

    xhtml = send(params, debug, nopost)
    if debug: print >> sys.stderr, 'checking results'
    # make it a 4suite document
    doc = NonvalidatingReader.parseString(xhtml, params['action'])
    context = Context(doc, processorNss={"h": XHTML_NS})
    #Compute the XPath against the context
    results = Compile(params['resultxpath'])
    results = results.evaluate(context)
    res = []
    for a in results:
        tf = cStringIO.StringIO()
        PrettyPrint(a, tf)
        t = tf.getvalue()
        res.append(t)
        tf.close()
    results = res
    if debug: print >> sys.stderr, 'done', params['action']
    return (results, xhtml)
Пример #13
0
 def lookupAsSet(self, node, context=None):
     """Returns the patterns that match the node (as a set [dictionary])"""
     if node.nodeType == Node.ELEMENT_NODE:
         #lookup result is a dict for elements
         narrowed_namecheck = self._lookup_table.get(Node.ELEMENT_NODE, {})
         narrowed = narrowed_namecheck.get(
             (node.namespaceURI, node.localName), [])
         #lookup of (ns,local) None is for the element wildcard case '*'
         narrowed.extend(narrowed_namecheck.get(None, []))
     else:
         #lookup result is a list  for non-elements
         narrowed = self._lookup_table.get(node.nodeType, [])
     if node.nodeType not in [Node.DOCUMENT_NODE, Node.ATTRIBUTE_NODE]:
         #lookup of nodeType None is for the wildcard case 'node()'
         narrowed.extend(self._lookup_table.get(None, []))
     if not narrowed: return {}
     matched_patterns = {}
     if not context: context = Context(node.ownerDocument, 1, 1)
     for p in narrowed:
         if p[0].match(context, node, p[1]):
             matched_patterns[p[2]] = None
     return matched_patterns
Пример #14
0
 def lookup(self, node, context=None):
     """Return the patterns that match the node (as a list)"""
     if node.nodeType == Node.ELEMENT_NODE:
         #lookup result is a dict for elements
         narrowed_namecheck = self._lookup_table.get(Node.ELEMENT_NODE, {})
         narrowed = narrowed_namecheck.get(
             (node.namespaceURI, node.localName), [])
         #lookup of (ns,local) None is for the element wildcard case '*'
         narrowed.extend(narrowed_namecheck.get(None, []))
     else:
         #lookup result is a list  for non-elements
         narrowed = self._lookup_table.get(node.nodeType, [])
     if node.nodeType not in [Node.DOCUMENT_NODE, Node.ATTRIBUTE_NODE]:
         #lookup of nodeType None is for the wildcard case 'node()'
         narrowed.extend(self._lookup_table.get(None, []))
     if not narrowed: return []
     if not context:
         from Ft.Xml.XPath.Context import Context
         context = Context(node.ownerDocument,
                           1,
                           1,
                           processorNss=self.namespaces)
     return [p[2] for p in narrowed if p[0].match(context, node, p[1])]
Пример #15
0
 def _makeMathML(self, formula):
     prefix = os.path.join(self.base_path,
                           "img" + str(self.index) + "_lxir")
     remove(prefix + ".tex", True)
     remove(prefix + ".aux", True)
     remove(prefix + ".log", True)
     remove(prefix + ".dvi", True)
     remove(prefix + ".xhtml", True)
     self.genLaTeXSource(formula, prefix + ".tex", True)
     self.system(self._getLaTeXCmd() + prefix + ".tex", prefix + ".dvi",
                 True)
     self.system("lxir " + prefix + ".dvi > " + prefix + ".xhtml",
                 prefix + ".xhtml")
     doc = NonvalidatingReader.parseUri(prefix + ".xhtml")
     ctxt = Context(doc, processorNss=NSS)
     label = self._getLabel(ctxt)
     nodes = Evaluate("//mm:math", context=ctxt)
     if len(nodes) == 1:
         formula = nodes[0]
         if formula:
             return formula, label
     elif len(nodes) == 3 and formula.find('eqnarray'):
         formula = nodes[0]
         if formula:
             return formula, label
     else:
         nodes = Evaluate("//xhtml:span[@class='msub' or @class='msup']",
                          context=ctxt)
         if len(nodes) == 1:
             formula = nodes[0]
             if formula:
                 print "Found simple math expression for formula %d" % self.index
                 return formula, label
         else:
             print "Generation of MathML for formula %d produced %d output(s)" % (
                 self.index, len(nodes))
	def editConfig(self,defaultoperation,testoption,erroroption,target,confignode,targetnode=None):
		"""
			Apply a ifconfig request from the confignode to the targetnode. delete and create attributes are supported only for iface element. For example, it is not possible to delete the name or the IP address of a network interface. 
			
			@type defaultoperation: MERGE_OPERATION | REPLACE_OPERATION | NONE_OPERATION 
			@param defaultoperation : as specified in NETCONF protocol
			@type testoption : SET | TEST_AND_SET 
			@param testoption : as specified in NETCONF protocol
			@type erroroption : STOP_ON_ERROR | IGNORE_ERROR | ROLL_BACK_ON_ERROR 
			@param erroroption : as specified in NETCONF protocol
			@type target : RUNNING_TARGET | CANDIDATE_TARGET | STARTUP_TARGET
			@param target : as specified in NETCONF protocol
			@type targetnode : string
			@param targetnode : if the target is RUNNING_TARGET or STARTUP_TARGET it will be ignored otherwise should be the node of the CANDIDATE_TARGET that this module should procees
			@rtype: ModuleReply
			@return: It returns a success or error message.
			** Relates to the netconf edit-config operation
        """
		
		moduleReply = ModuleReply(
		error_type = ModuleReply.APPLICATION,
		error_tag = ModuleReply.OPERATION_FAILED,
		error_severity = ModuleReply.ERROR,
		error_message = "Sorry, edit-config is temporarily no more supported for Interfaces.")
		
		# First ,find what to change from edit-config
		# the interface name is the key and can not be changed
		NS= 'urn:ietf:params:xml:ns:netconf:base:1.0'
		NSS = {u'xc': NS }
		ctx = Context (confignode, processorNss = NSS)
		liste = Evaluate("//*[@xc:operation]", ctx)
		if len(liste)>=2:
			moduleReply = ModuleReply(
			error_type = ModuleReply.APPLICATION,
			error_tag = ModuleReply.OPERATION_FAILED,
			error_severity = ModuleReply.ERROR,
			error_message = "Only one operation attribute is allowed.")
			return moduleReply
		elif len(liste)==1:
			opNode = liste[0]
			#for att in liste[0].attributes.values():
			for att in opNode.attributes:
				ns, name = att
				value = opNode.getAttributeNS(ns, str(name))
				if name == C.OPERATION and ns == C.NETCONF_XMLNS:
					opValue = value
					
		else:
			opValue = defaultoperation
			opNode = confignode

		if (not opValue in [C.MERGE, C.REPLACE, C.CREATE, C.DELETE]):
			moduleReply = ModuleReply(
			error_type = ModuleReply.APPLICATION,
			error_tag = ModuleReply.OPERATION_FAILED,
			error_severity = ModuleReply.ERROR,
			error_message = "Operation value is not allowed. Must be one of (merge, replace, create, delete).")
			return moduleReply

		macrocommand = MacroCommand()
		
		# On peut optimiser en creant mergeInterface(interfaceNode)
		# On peut optimiser en creant replaceInterface(interfaceNode)
		# Chacune renvoie un tableau de commande qu'on ajouter au tableau global

		if opNode != None:
			nodeName = string.strip(str(opNode.tagName))
			if (nodeName == "interfaces"):
				if (opValue == C.MERGE):
					for node in opNode.childNodes:
						if (node.nodeType==Node.ELEMENT_NODE):
							if (node.tagName == "interface"):
								ifname = None
								mtu = None
								netmask = None
								addr = None
								broadcast = None
								for n in node.childNodes:
									if (n.nodeType==Node.ELEMENT_NODE):
										if (n.tagName == "name"):
											ifname = string.strip(str(n.childNodes[0].nodeValue))
										elif (node.tagName == "mtu"):
											mtu = int(string.strip(str(n.childNodes[0].nodeValue)))
		
								if ifname != None:
									if mtu !=None:
										macrocommand.add(SetMtu(ifname,mtu))
									if broadcast !=None:
										macrocommand.add(SetBroadcast(ifname,broadcast))
									if netmask !=None:
										macrocommand.add(SetNetmask(ifname,netmask))
									if addr != None:
										macrocommand.add(SetAddr(ifname,addr))

				#elif (opValue == C.CREATE):
				#elif (opValue == C.DELETE):
				#...

			elif (nodeName == "interface"):
				ifname = None
				mtu = None
				netmask = None
				addr = None
				broadcast = None
				address = None
				if (opValue == C.MERGE):
					for node in opNode.childNodes:
						if (node.nodeType==Node.ELEMENT_NODE):
							if (node.tagName == "name"):
								ifname = string.strip(str(node.childNodes[0].nodeValue))
							elif (node.tagName == "mtu"):
								mtu = int(string.strip(str(node.childNodes[0].nodeValue)))
							elif (node.tagName == "ipv4"):
								for n in node.childNodes:
									if (n.nodeType==Node.ELEMENT_NODE):
										if (n.tagName == "broadcast"):
											broadcast = string.strip(str(n.childNodes[0].nodeValue))
										elif (n.tagName == "netmask"):
											netmask = string.strip(str(n.childNodes[0].nodeValue))
										elif (n.tagName == "address-v4"):
											address = string.strip(str(n.childNodes[0].nodeValue))
							# ... addr v6...
				
				if ifname != None:
					if mtu != None:
						macrocommand.add(SetMtu(ifname,mtu))
					if broadcast !=None:
						macrocommand.add(SetBroadcast(ifname,broadcast))
					if netmask !=None:
						macrocommand.add(SetNetmask(ifname,netmask))
					if address != None:
						macrocommand.add(SetAddr(ifname,address))

			elif (nodeName == "mtu"):
				ifname = None
				mtu = None
				if (opValue == C.DELETE or opValue == C.CREATE):
					moduleReply = ModuleReply(
					error_type=ModuleReply.PROTOCOL,
					error_tag=ModuleReply.OPERATION_FAILED,
					error_severity=ModuleReply.ERROR,
					error_message="Sorry, mtu can not be deleted or created. Only interface element can.")
					return moduleReply
				elif (opValue == C.MERGE or opValue == C.REPLACE):
					# first find ifName
					for k in opNode.parent.childNodes:
						if (k.nodeType==Node.ELEMENT_NODE):
							if (k.tagName == "name"):
								ifname = string.strip(str(k.childNodes[0].nodeValue))
					# Then get the new mtu value
					mtu = int(string.strip(str(opNode.childNodes[0].nodeValue)))

				if ifname != None:
					# Build the command and add it to the macro command
					macrocommand.add(SetMtu(ifname,mtu))

			elif (nodeName == "broadcast"):
				ifname = None
				broadcast = None
				if (opValue == C.DELETE or opValue == C.CREATE):
					moduleReply = ModuleReply(
					error_type=ModuleReply.PROTOCOL,
					error_tag=ModuleReply.OPERATION_FAILED,
					error_severity=ModuleReply.ERROR,
					error_message="Sorry, broadcast can not be deleted or created. Only interface element can.")
					return moduleReply
				elif (opValue == C.MERGE or opValue == C.REPLACE):
					# first find ifName
					for k in opNode.parent.parent.childNodes:
						if (k.nodeType==Node.ELEMENT_NODE):
							if (k.tagName == "name"):
								ifname = string.strip(str(k.childNodes[0].nodeValue))
					# Then get the new mtu value
					broadcast = int(string.strip(str(opNode.childNodes[0].nodeValue)))

				if ifname != None:
					# Build the command and add it to the macro command
					macrocommand.add(SetBroadcast(ifname,broadcast))

			elif (nodeName == "netmask"):
				ifname = None
				netmask = None
				if (opValue == C.DELETE or opValue == C.CREATE):
					moduleReply = ModuleReply(
					error_type=ModuleReply.PROTOCOL,
					error_tag=ModuleReply.OPERATION_FAILED,
					error_severity=ModuleReply.ERROR,
					error_message="Sorry, netmask can not be deleted or created. Only interface element can.")
					return moduleReply
				elif (opValue == C.MERGE or opValue == C.REPLACE):
					# first find ifName
					for k in opNode.parent.parent.childNodes:
						if (k.nodeType==Node.ELEMENT_NODE):
							if (k.tagName == "name"):
								ifname = string.strip(str(k.childNodes[0].nodeValue))
					# Then get the new mtu value
					netmask = int(string.strip(str(opNode.childNodes[0].nodeValue)))

				if ifname != None:
					# Build the command and add it to the macro command
					macrocommand.add(SetNetmask(ifname,netmask))

			elif (nodeName == "address-v4"):
				ifname = None
				address = None
				if (opValue == C.DELETE or opValue == C.CREATE):
					moduleReply = ModuleReply(
					error_type=ModuleReply.PROTOCOL,
					error_tag=ModuleReply.OPERATION_FAILED,
					error_severity=ModuleReply.ERROR,
					error_message="Sorry, address-v4 can not be deleted or created. Only interface element can.")
					return moduleReply
				elif (opValue == C.MERGE or opValue == C.REPLACE):
					# first find ifName
					for k in opNode.parent.parent.childNodes:
						if (k.nodeType==Node.ELEMENT_NODE):
							if (k.tagName == "name"):
								ifname = string.strip(str(k.childNodes[0].nodeValue))
					# Then get the new mtu value
					address = int(string.strip(str(opNode.childNodes[0].nodeValue)))

				if ifname != None:
					# Build the command and add it to the macro command
					macrocommand.add(SetAddr(ifname,address))

			# elif ...

		# Prepare the commands that must be executed and add them to a macro command
		#macrocommand = MacroCommand()
		#macrocommand.add(SetMtu("eth0",1200))
		#macrocommand.add(SetBroadcast("eth0","152.81.15.255"))
		#macrocommand.add(SetNetmask("eth0","255.255.240.0"))
		#macrocommand.add(SetAddr("eth0","152.81.8.136"))
		
		# Then, make the changes by executing the macro command
		try:
			macrocommand.execute()
			moduleReply = ModuleReply()
		except Exception,exp:
			moduleReply = ModuleReply(
			error_type=ModuleReply.PROTOCOL,
			error_tag=ModuleReply.OPERATION_FAILED,
			error_severity=ModuleReply.ERROR,
			error_message="Interfaces_Module.py ERROR: "+str(exp))
Пример #17
0
def insert_math_images(file):
    file = os.path.abspath(file)
    doc = NonvalidatingReader.parseUri(file)
    ctxt = Context(doc, processorNss=NSS)

    # Check that verbatim math is used
    for node in Evaluate("//xhtml:span[@class='verbatimmath']/@lxir:value",
                         context=ctxt):
        verbatimmath = node.value
    assert verbatimmath == u'true', "Need verbatim math mode for math conversion"

    # Check that the document class is known
    latexClass = None
    symbols = []
    for node in Evaluate(
            "//xhtml:span[@class='ClassOrPackageUsed']/@lxir:name",
            context=ctxt):
        if latexClasses.has_key(node.value):
            latexClass = latexClasses[node.value]
        elif node.value in symbolPackages:
            symbols.append(node.value[:-4])
    assert latexClass, "Unknown document class used"

    # Get All macro text
    macros = []
    for node in Evaluate("//xhtml:span[@class='macro']//text()", context=ctxt):
        macros.append(node.nodeValue)

    gen = ImageGenerator(file, latexClass, macros, symbols)

    # Convert All math images
    for node in Evaluate("//xhtml:span[@class='formule']", context=ctxt):
        c = Context(node, processorNss=NSS)
        formula = ""
        for t in Evaluate("xhtml:span[@class='text']/text()", context=c):
            formula += t.nodeValue
        formula = formula.strip()
        if not len(formula):
            print "empty formula found in document"
            image, mathml, label = None, None, None
        else:
            if formula[0] != "$":
                p = node.parentNode
                env = p.getAttributeNS(None, 'class')
                assert env, "No env found for equation"
                if env[-5:] == "-star":
                    env = env[:-5] + "*"
                formula = "\\begin{" + env + "}\n" + formula + "\n\\end{" + env + "}"
            image, mathml, label = gen.makeImage(formula)
        # remove the empty text node(s)
        for t in Evaluate("xhtml:span[@class='text']", context=c):
            t.parentNode.removeChild(t)

        if image:
            img = node.ownerDocument.createElementNS(XHTML_NAMESPACE, "img")
            img.setAttributeNS(XHTML_NAMESPACE, "src", image)
            img.setAttributeNS(XHTML_NAMESPACE, "alt", formula)
            node.appendChild(img)

        if mathml:
            if mathml.tagName != 'math':
                # here, we have the case : a$_b$
                # mathml is: <span class='msub'><span class='mi'/><span>b</span></span>
                # original xml is : ... <span>a</span><span class="formula"> ...</span>
                # and node is the formula
                # p is <span>a</span>
                p = get_prev_span_node(node)
                newNode = node.parentNode.insertBefore(mathml.cloneNode(True),
                                                       node)
                newNode.firstChild.appendChild(p)
                node.parentNode.removeChild(node)
                print "Formula '%s' replaced by simple form (%s)." % (
                    formula, newNode.tagName + '.' +
                    newNode.getAttributeNS(None, u'class'))
            else:
                node.appendChild(node.ownerDocument.importNode(mathml, True))

        if label:
            node.appendChild(node.ownerDocument.importNode(label, True))
    base, ext = os.path.splitext(file)
    output = base + "_images" + ext
    o = open(output, "w")
    Print(doc, stream=o)
    o.close()
Пример #18
0
    def execute(self):
        """
			Execute the edit-config operation.
		"""
        if self.operationReply.isError():
            return self.operationReply

        sessionId = sessionManager.getInstance().getLockOwnerSessionId(
            self.target)

        if sessionId >= 0 and sessionId != self.session.getSessionId():
            # Somebody else locked the target.
            moduleReply = ModuleReply(
                error_type=ModuleReply.PROTOCOL,
                error_tag=ModuleReply.IN_USE,
                error_severity=ModuleReply.ERROR,
                error_message=
                "Operation failed, target is locked by another session.")
            moduleReply.addErrorInfo("session-id", sessionId)
            self.operationReply.setError()
            self.operationReply.setNode(moduleReply.getXMLNodeReply())
            return self.operationReply

        # Check access here
        if self.rbacManager.isActive():
            operation_allowed = self.rbacManager.checkAccessEditConfig(
                self.config, self.session)

            if (not operation_allowed):
                moduleReply = ModuleReply(error_type=ModuleReply.PROTOCOL,
                                          error_tag=ModuleReply.ACCESS_DENIED,
                                          error_severity=ModuleReply.ERROR,
                                          error_message="Access denied.")
                self.operationReply.setError()
                self.operationReply.setNode(moduleReply.getXMLNodeReply())
                return self.operationReply

        # Execute edit-config
        #if (self.target == C.RUNNING or self.target == C.URL):
        if (self.target in [C.RUNNING, C.STARTUP, C.CANDIDATE, C.URL]):
            moduleReply = None
            for module in self.moduleManager.getModules():
                currentPath = module.path
                # Finding the nodes (related to the current module)
                # in the received configuration

                ctx = Context(self.config,
                              processorNss=self.moduleManager.getPrefixes())
                nodeList = Evaluate(currentPath, ctx)

                # Distributing the nodes to the current module
                if len(nodeList) > 1:
                    moduleReply = ModuleReply(
                        error_type=ModuleReply.APPLICATION,
                        error_tag=ModuleReply.UNKNOWN_ELEMENT,
                        error_severity=ModuleReply.ERROR,
                        error_message=
                        "More than one node found in the received data for the same module"
                        + module.name)
                    self.operationReply.setError()
                    self.operationReply.setNode(moduleReply.getXMLNodeReply())
                    return self.operationReply

                elif len(nodeList) == 1:
                    defaultOperation = self.defaultOperation
                    node = nodeList[0]
                    # Search for the first parent node of 'node' having xc:operation attribute and set the default value to its value:
                    tmpNode = node.parentNode
                    boolean = False
                    while tmpNode != None and tmpNode.nodeType != Node.DOCUMENT_NODE and not boolean:
                        for att in tmpNode.attributes:
                            ns, name = att
                            if name == 'operation' and ns == C.NETCONF_XMLNS:
                                # We found the first parent node having an operation attribute.
                                # This is the new default op for this module.
                                defaultOperation = tmpNode.getAttributeNS(
                                    ns, str(name))
                                # Stop the loop.
                                boolean = True
                        tmpNode = tmpNode.parentNode

                    # Send the operation to the module.
                    moduleReply = module.editConfig(defaultOperation,
                                                    self.testOption,
                                                    self.errorOption,
                                                    self.target, node)

                    # Update the cache according to the new config.
                    module.resetConfigTime(self.target)

                    if (moduleReply.isError()):
                        if self.errorOption == C.STOP_ON_ERROR:
                            self.operationReply.setError()
                            self.operationReply.setNode(
                                moduleReply.getXMLNodeReply())
                            return self.operationReply
                        elif self.errorOption == C.CONTINUE_ON_ERROR:
                            pass
                        elif self.errorOption == C.ROLLBACK_ON_ERROR:
                            # ROLLBACK IS EXPERIMENTAL
                            for previousmodule in self.moduleManager.getModules(
                            ):
                                if previousmodule is module:
                                    moduleReply = ModuleReply(
                                        error_type=ModuleReply.APPLICATION,
                                        error_tag=ModuleReply.UNKNOWN_ELEMENT,
                                        error_severity=ModuleReply.ERROR,
                                        error_message=
                                        "%s module failed and the whole operation has been rolled back."
                                        % module.name)
                                    self.operationReply.setError()
                                    self.operationReply.setNode(
                                        moduleReply.getXMLNodeReply())
                                    return self.operationReply
                                else:
                                    moduleReply = previousmodule.rollBack()
                                    if moduleReply.isError():
                                        self.operationReply.setError()
                                        self.operationReply.setNode(
                                            moduleReply.getXMLNodeReply())
                                        return self.operationReply

            if (moduleReply == None):
                moduleReply = ModuleReply(
                    error_type=ModuleReply.APPLICATION,
                    error_tag=ModuleReply.UNKNOWN_ELEMENT,
                    error_severity=ModuleReply.ERROR,
                    error_message="Not any module matched")
                self.operationReply.setError()

            self.operationReply.setNode(moduleReply.getXMLNodeReply())
            return self.operationReply
	def execute(self):
		"""
			Execute the validate operation.
		"""
		if self.operationReply.isError():
			return self.operationReply
		
		# Execute validate
		if (self.target in [C.CANDIDATE, C.STARTUP, C.RUNNING]):
			moduleReply = None
			for module in self.moduleManager.getModules():
				
				moduleReply = module.validate(self.target)
				
    				if moduleReply.isError():
        				self.operationReply.setNode(moduleReply.getXMLNodeReply())
					return self.operationReply
			
			self.operationReply.setNode(moduleReply.getXMLNodeReply())
			return self.operationReply
				
		elif (self.target == C.CONFIG):
			moduleReply = None
			for module in self.moduleManager.getModules():
				currentPath = module.path
				# Finding the nodes (related to the current module)
				# in the received configuration
				ctx = Context(self.config, processorNss = self.moduleManager.getPrefixes())
				nodeList = Evaluate(currentPath, ctx)

				# Distributing the nodes to the current module
				if len(nodeList)>1:
					moduleReply = ModuleReply(
					error_type = ModuleReply.APPLICATION,
					error_tag = ModuleReply.UNKNOWN_ELEMENT,
					error_severity = ModuleReply.ERROR,
					error_message = "More than one node found in the received data for the same module" + module.name)
					self.operationReply.setError()
					self.operationReply.setNode(moduleReply.getXMLNodeReply())
					return self.operationReply

				elif len(nodeList)==1:
					#Get the configuration to validate.
					node = nodeList[0]
					
					# Send the operation to the module.
					moduleReply = module.validate(self.target, self.config)

					if (moduleReply.isError()):
						self.operationReply.setError()
						self.operationReply.setNode(moduleReply.getXMLNodeReply())
						return self.operationReply
	

			if (moduleReply == None):
				moduleReply = ModuleReply(
				error_type = ModuleReply.APPLICATION,
				error_tag = ModuleReply.UNKNOWN_ELEMENT,
				error_severity = ModuleReply.ERROR,
				error_message = "Not any module matched")
				self.operationReply.setError()

			self.operationReply.setNode(moduleReply.getXMLNodeReply())
			return self.operationReply

		#If somebody has the strange idea to try this:
		elif (self.target == C.URL):
			xmlreply = ModuleReply(
			error_type=ModuleReply.APPLICATION,
			error_tag=ModuleReply.OPERATION_NOT_SUPPORTED,
			error_severity=ModuleReply.ERROR,
			error_message="OPERATION-NOT-SUPPORTED")
			
			self.operationReply.setNode(xmlReply.getXMLNodeReply())
			return self.operationReply
            sourceDoc = self.sourceInbound

        if self.target in [C.RUNNING, C.CANDIDATE, C.STARTUP]:
            if self.target == C.CANDIDATE:
                self.datastoreManager.initializeAll(self.target)

            for module in self.moduleManager.getModules():

                sourceNode = None

                if sourceDoc != None:
                    currentPath = module.path

                    # Finding the nodes (related to the current module) in the received configuration
                    try:
                        ctx = Context(sourceDoc,
                                      processorNss=self.moduleManager.prefixes)
                        nodeList = Evaluate(currentPath, ctx)

                    except Exception, exp:
                        moduleReply = ModuleReply(
                            error_type=ModuleReply.APPLICATION,
                            error_tag=ModuleReply.UNKNOWN_ELEMENT,
                            error_severity=ModuleReply.ERROR,
                            error_message=str(exp))
                        self.operationReply.setError()
                        self.operationReply.setNode(
                            moduleReply.getXMLNodeReply())
                        return self.operationReply

                    # Distributing the nodes to the current module:
                    if len(nodeList) > 1: