<s> from core . netns import nodes <EOL> from xml . dom . minidom import Node <EOL> def addelementsfromlist ( dom , parent , iterable , name , attr_name ) : <EOL> '''<STR_LIT>''' <EOL> for item in iterable : <EOL> element = dom . createElement ( name ) <EOL> element . setAttribute ( attr_name , item ) <EOL> parent . appendChild ( element ) <EOL> def addtextelementsfromlist ( dom , parent , iterable , name , attrs ) : <EOL> '''<STR_LIT>''' <EOL> for item in iterable : <EOL> element = dom . createElement ( name ) <EOL> for k , v in attrs : <EOL> element . setAttribute ( k , v ) <EOL> parent . appendChild ( element ) <EOL> txt = dom . createTextNode ( item ) <EOL> element . appendChild ( txt ) <EOL> def addtextelementsfromtuples ( dom , parent , iterable , attrs = ( ) ) : <EOL> '''<STR_LIT>''' <EOL> for name , value in iterable : <EOL> element = dom . createElement ( name ) <EOL> for k , v in attrs : <EOL> element . setAttribute ( k , v ) <EOL> parent . appendChild ( element ) <EOL> txt = dom . createTextNode ( value ) <EOL> element . appendChild ( txt ) <EOL> def gettextelementstolist ( parent ) : <EOL> '''<STR_LIT>''' <EOL> r = [ ] <EOL> for n in parent . childNodes : <EOL> if n . nodeType != Node . ELEMENT_NODE : <EOL> continue <EOL> k = str ( n . nodeName ) <EOL> v = '<STR_LIT>' <EOL> for c in n . childNodes : <EOL> if c . nodeType != Node . TEXT_NODE : <EOL> continue <EOL> v = str ( c . nodeValue ) <EOL> break <EOL> r . append ( ( k , v ) ) <EOL> return r <EOL> def addparamtoparent ( dom , parent , name , value ) : <EOL> '''<STR_LIT>''' <EOL> if value is None : <EOL> return None <EOL> p = dom . createElement ( "<STR_LIT>" ) <EOL> parent . appendChild ( p ) <EOL> p . setAttribute ( "<STR_LIT:name>" , name ) <EOL> p . setAttribute ( "<STR_LIT:value>" , "<STR_LIT:%s>" % value ) <EOL> return p <EOL> def addtextparamtoparent ( dom , parent , name , value ) : <EOL> '''<STR_LIT>''' <EOL> if value is None : <EOL> return None <EOL> p = dom . createElement ( "<STR_LIT>" ) <EOL> parent . appendChild ( p ) <EOL> p . setAttribute ( "<STR_LIT:name>" , name ) <EOL> txt = dom . createTextNode ( value ) <EOL> p . appendChild ( txt ) <EOL> return p <EOL> def addparamlisttoparent ( dom , parent , name , values ) : <EOL> '''<STR_LIT>''' <EOL> if values is None : <EOL> return None <EOL> p = dom . createElement ( "<STR_LIT>" ) <EOL> if parent : <EOL> parent . appendChild ( p ) <EOL> p . setAttribute ( "<STR_LIT:name>" , name ) <EOL> for v in values : <EOL> item = dom . createElement ( "<STR_LIT>" ) <EOL> item . setAttribute ( "<STR_LIT:value>" , str ( v ) ) <EOL> p . appendChild ( item ) <EOL> return p <EOL> def getoneelement ( dom , name ) : <EOL> e = dom . getElementsByTagName ( name ) <EOL> if len ( e ) == <NUM_LIT:0> : <EOL> return None <EOL> return e [ <NUM_LIT:0> ] <EOL> def iterDescendants ( dom , max_depth = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> nodes = [ dom ] <EOL> depth = <NUM_LIT:0> <EOL> current_depth_nodes = <NUM_LIT:1> <EOL> next_depth_nodes = <NUM_LIT:0> <EOL> while nodes : <EOL> n = nodes . pop ( <NUM_LIT:0> ) <EOL> for child in n . childNodes : <EOL> if child . nodeType == Node . ELEMENT_NODE : <EOL> yield child <EOL> nodes . append ( child ) <EOL> next_depth_nodes += <NUM_LIT:1> <EOL> current_depth_nodes -= <NUM_LIT:1> <EOL> if current_depth_nodes == <NUM_LIT:0> : <EOL> depth += <NUM_LIT:1> <EOL> if max_depth > <NUM_LIT:0> and depth == max_depth : <EOL> return <EOL> current_depth_nodes = next_depth_nodes <EOL> next_depth_nodes = <NUM_LIT:0> <EOL> def iterMatchingDescendants ( dom , matchFunction , max_depth = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> for d in iterDescendants ( dom , max_depth ) : <EOL> if matchFunction ( d ) : <EOL> yield d <EOL> def iterDescendantsWithName ( dom , tagName , max_depth = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( tagName , basestring ) : <EOL> tagName = ( tagName , ) <EOL> def match ( d ) : <EOL> return d . tagName in tagName <EOL> return iterMatchingDescendants ( dom , match , max_depth ) <EOL> def iterDescendantsWithAttribute ( dom , tagName , attrName , attrValue , <EOL> max_depth = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( tagName , basestring ) : <EOL> tagName = ( tagName , ) <EOL> def match ( d ) : <EOL> return d . tagName in tagName and d . getAttribute ( attrName ) == attrValue <EOL> return iterMatchingDescendants ( dom , match , max_depth ) <EOL> def iterChildren ( dom , nodeType ) : <EOL> '''<STR_LIT>''' <EOL> for child in dom . childNodes : <EOL> if child . nodeType == nodeType : <EOL> yield child <EOL> def gettextchild ( dom ) : <EOL> '''<STR_LIT>''' <EOL> for child in iterChildren ( dom , Node . TEXT_NODE ) : <EOL> return str ( child . nodeValue ) <EOL> return None <EOL> def getChildTextTrim ( dom ) : <EOL> text = gettextchild ( dom ) <EOL> if text : <EOL> text = text . strip ( ) <EOL> return text <EOL> def getparamssetattrs ( dom , param_names , target ) : <EOL> '''<STR_LIT>''' <EOL> params = dom . getElementsByTagName ( "<STR_LIT>" ) <EOL> for param in params : <EOL> param_name = param . getAttribute ( "<STR_LIT:name>" ) <EOL> value = param . getAttribute ( "<STR_LIT:value>" ) <EOL> if value is None : <EOL> continue <EOL> if param_name in param_names : <EOL> setattr ( target , param_name , str ( value ) ) <EOL> def xmltypetonodeclass ( session , type ) : <EOL> '''<STR_LIT>''' <EOL> if hasattr ( nodes , type ) : <EOL> return eval ( "<STR_LIT>" % type ) <EOL> else : <EOL> return None <EOL> def iterChildrenWithName ( dom , tagName ) : <EOL> return iterDescendantsWithName ( dom , tagName , <NUM_LIT:1> ) <EOL> def iterChildrenWithAttribute ( dom , tagName , attrName , attrValue ) : <EOL> return iterDescendantsWithAttribute ( dom , tagName , attrName , attrValue , <NUM_LIT:1> ) <EOL> def getFirstChildByTagName ( dom , tagName ) : <EOL> '''<STR_LIT>''' <EOL> for child in iterChildrenWithName ( dom , tagName ) : <EOL> return child <EOL> return None <EOL> def getFirstChildTextByTagName ( dom , tagName ) : <EOL> '''<STR_LIT>''' <EOL> child = getFirstChildByTagName ( dom , tagName ) <EOL> if child : <EOL> return gettextchild ( child ) <EOL> return None <EOL> def getFirstChildTextTrimByTagName ( dom , tagName ) : <EOL> text = getFirstChildTextByTagName ( dom , tagName ) <EOL> if text : <EOL> text = text . strip ( ) <EOL> return text <EOL> def getFirstChildWithAttribute ( dom , tagName , attrName , attrValue ) : <EOL> '''<STR_LIT>''' <EOL> for child in iterChildrenWithAttribute ( dom , tagName , attrName , attrValue ) : <EOL> return child <EOL> return None <EOL> def getFirstChildTextWithAttribute ( dom , tagName , attrName , attrValue ) : <EOL> '''<STR_LIT>''' <EOL> child = getFirstChildWithAttribute ( dom , tagName , attrName , attrValue ) <EOL> if child : <EOL> return gettextchild ( child ) <EOL> return None <EOL> def getFirstChildTextTrimWithAttribute ( dom , tagName , attrName , attrValue ) : <EOL> text = getFirstChildTextWithAttribute ( dom , tagName , attrName , attrValue ) <EOL> if text : <EOL> text = text . strip ( ) <EOL> return text </s>
