Back to index

nordugrid-arc-nox  1.1.0~rc6
Public Member Functions | Private Member Functions | Private Attributes
arcom.xmltree.XMLTree Class Reference

List of all members.

Public Member Functions

def __init__
def add_to_node
def pretty_xml
def __str__
def get
def get_trees
def get_value
def add_tree
def get_values
def get_dict
def get_dicts

Private Member Functions

def _getname
def _dump
def _add_to_node
def _pretty_xml
def _traverse
def _dict

Private Attributes

 _data

Detailed Description

Definition at line 195 of file xmltree.py.


Constructor & Destructor Documentation

def arcom.xmltree.XMLTree.__init__ (   self,
  from_node = None,
  from_string = '',
  from_tree = None,
  rewrite = {},
  forget_namespace = False,
  xmlnode_class = None 
)
Constructor of the XMLTree class

XMLTree(from_node = None, from_string = '', from_tree = None, rewrite = {}, forget_namespace = False)

'from_tree' could be tree structure or an XMLTree object
'from_string' could be an XML string
'from_node' could be an XMLNode
'rewrite' is a dictionary, if an XML node has a name which is a key in this dictionary,
    then it will be renamed as the value of that key
'forget_namespace' is a boolean, if it is true, the XMLTree will not contain the namespace prefixes

'from_tree' has the highest priority, if it is not None,
    then the other two is ignored.
If 'from_tree' is None but from_string is given, then from_node is ignored.
If only 'from_node' is given, then it will be the choosen one.
In this case you may simply use:
    tree = XMLTree(node)

Definition at line 196 of file xmltree.py.

00196 
00197     def __init__(self, from_node = None, from_string = '', from_tree = None, rewrite = {}, forget_namespace = False, xmlnode_class = None):
00198         """ Constructor of the XMLTree class
00199 
00200         XMLTree(from_node = None, from_string = '', from_tree = None, rewrite = {}, forget_namespace = False)
00201 
00202         'from_tree' could be tree structure or an XMLTree object
00203         'from_string' could be an XML string
00204         'from_node' could be an XMLNode
00205         'rewrite' is a dictionary, if an XML node has a name which is a key in this dictionary,
00206             then it will be renamed as the value of that key
00207         'forget_namespace' is a boolean, if it is true, the XMLTree will not contain the namespace prefixes
00208 
00209         'from_tree' has the highest priority, if it is not None,
00210             then the other two is ignored.
00211         If 'from_tree' is None but from_string is given, then from_node is ignored.
00212         If only 'from_node' is given, then it will be the choosen one.
00213         In this case you may simply use:
00214             tree = XMLTree(node)
00215         """
00216         if from_tree:
00217             # if a tree structure is given, set the internal variable with it
00218             # if this is an XMLTree object, get just the data from it
00219             if isinstance(from_tree,XMLTree):
00220                 self._data = from_tree._data
00221             else: 
00222                 self._data = from_tree
00223         else:
00224             if from_node:
00225                 # if no from_tree is given, and we have an XMLNode, just save it
00226                 x = from_node
00227             else:
00228                 # if no from_tree and from_node is given, try to parse the string
00229                 if not xmlnode_class:
00230                     from arc import XMLNode
00231                     xmlnode_class = XMLNode
00232                 x = xmlnode_class(from_string)
00233             # set the internal tree structure to (<name of the root node>, <rest of the document>)
00234             # where <rest of the document> is a list of the child nodes of the root node
00235             self._data = (self._getname(x, rewrite, forget_namespace), self._dump(x, rewrite, forget_namespace))


Member Function Documentation

Definition at line 300 of file xmltree.py.

00300 
00301     def __str__(self):
00302         return str(self._data)

def arcom.xmltree.XMLTree._add_to_node (   self,
  data,
  node 
) [private]

Definition at line 268 of file xmltree.py.

00268 
00269     def _add_to_node(self, data, node):
00270         # recursively add the tree structure to the node
00271         for element in data:
00272             # we want to avoid empty tags in XML
00273             if element[0]:
00274                 # for each child in the tree create a child in the XMLNode
00275                 child_node = node.NewChild(element[0])
00276                 # if the node has children:
00277                 if isinstance(element[1],list):
00278                     self._add_to_node(element[1], child_node)
00279                 else: # if it has no child, create a string from it
00280                     child_node.Set(str(element[1]))

Here is the call graph for this function:

Here is the caller graph for this function:

def arcom.xmltree.XMLTree._dict (   self,
  value,
  keys 
) [private]

Definition at line 404 of file xmltree.py.

00404 
00405     def _dict(self, value, keys):
00406         # helper method for changing keys
00407         if keys:
00408             # if keys is given use only the keys which is in it
00409             # and translete them to new keys (the values of the 'keys' dictionary)
00410             return dict([(keys[k],v) for (k,v) in value if k in keys.keys()])
00411         else: # if keys is empty, use all the data
00412             return dict(value)
    

Here is the caller graph for this function:

def arcom.xmltree.XMLTree._dump (   self,
  node,
  rewrite = {},
  forget_namespace = False 
) [private]

Definition at line 244 of file xmltree.py.

00244 
00245     def _dump(self, node, rewrite = {}, forget_namespace = False):
00246         # recursive method for converting an XMLNode to XMLTree structure
00247         size = node.Size() # get the number of children of the node
00248         if size == 0: # if it has no child, get the string
00249             return str(node)
00250         children = [] # collect the children
00251         for i in range(size):
00252             children.append(node.Child(i))
00253         # call itself recursively for each children
00254         return [(self._getname(n, rewrite, forget_namespace), self._dump(n, rewrite, forget_namespace)) for n in children ]

Here is the call graph for this function:

Here is the caller graph for this function:

def arcom.xmltree.XMLTree._getname (   self,
  node,
  rewrite = {},
  forget_namespace = False 
) [private]

Definition at line 236 of file xmltree.py.

00236 
00237     def _getname(self, node, rewrite = {}, forget_namespace = False):
00238         # gets the name of an XMLNode, with namespace prefix if it has one
00239         if not forget_namespace and node.Prefix():
00240             name = node.FullName()
00241         else: # and without namespace prefix if it has no prefix
00242             name = node.Name()
00243         return rewrite.get(name,name)

Here is the caller graph for this function:

def arcom.xmltree.XMLTree._pretty_xml (   self,
  data,
  indent,
  level,
  prefix 
) [private]

Definition at line 285 of file xmltree.py.

00285 
00286     def _pretty_xml(self, data, indent, level, prefix ):
00287         out = []
00288         for element in data:
00289             if element[0]:
00290                 if isinstance(element[1], list):
00291                     out.append(
00292                         prefix + indent * level + '<%s>\n' % element[0] +
00293                             self._pretty_xml(element[1], indent, level+1, prefix) + '\n' +
00294                         prefix + indent * level +'</%s>' % element[0]
00295                     )
00296                 else:
00297                     out.append(prefix + indent * level + '<%s>%s</%s>' % (element[0], element[1], element[0]))
00298         return '\n'.join(out)
00299             

Here is the call graph for this function:

Here is the caller graph for this function:

def arcom.xmltree.XMLTree._traverse (   self,
  path,
  data 
) [private]

Definition at line 303 of file xmltree.py.

00303 
00304     def _traverse(self, path, data):
00305         # helping function for recursively traverse the tree
00306         # 'path' is a list of the node names, e.g. ['root','key1']
00307         # 'data' is the data of a tree-node,
00308         # e.g. ('root', [('key1', 'value'), ('key2', 'value')])
00309         # if the first element of the path and the name of the node is equal
00310         #   or if the element of the path is empty, it matches all node names
00311         # if not, then we have no match here, return an empty list
00312         if path[0] != data[0] and path[0] != '':
00313             return []
00314         # if there are no more path-elements, then we are done
00315         # we've just found what we looking for
00316         if len(path) == 1:
00317             return [data]
00318         # if there are more path-elements, but this is a string node
00319         # then no luck, we cannot proceed, return an empty list
00320         if isinstance(data[1],str):
00321             return []
00322         # if there are more path-elements, and this node has children
00323         ret = []
00324         for d in data[1]:
00325             # let's recurively ask all child if they have any matches
00326             # and collect the matches
00327             ret.extend( self._traverse(path[1:], d) )
00328         # return the matches
00329         return ret

Here is the call graph for this function:

Here is the caller graph for this function:

def arcom.xmltree.XMLTree.add_to_node (   self,
  node,
  path = None 
)
Adding a tree structure to an XMLNode.

add_to_node(node, path = None)

'node' is the XMLNode we want to add to
'path' selects the part of the XMLTree we want to add

Definition at line 255 of file xmltree.py.

00255 
00256     def add_to_node(self, node, path = None):
00257         """ Adding a tree structure to an XMLNode.
00258 
00259         add_to_node(node, path = None)
00260         
00261         'node' is the XMLNode we want to add to
00262         'path' selects the part of the XMLTree we want to add
00263         """
00264         # selects the part we want
00265         data = self.get(path)
00266         # call the recursive helping method
00267         self._add_to_node(data, node)

Here is the call graph for this function:

def arcom.xmltree.XMLTree.add_tree (   self,
  tree,
  path = None 
)
Add a new subtree to a path.

add_tree(tree, path = None)

Definition at line 379 of file xmltree.py.

00379 
00380     def add_tree(self, tree, path = None):
00381         """ Add a new subtree to a path.
00382 
00383         add_tree(tree, path = None)
00384         """
00385         # if this is a real XMLTree object, get just the data from it
00386         if isinstance(tree,XMLTree):
00387             tree = tree._data
00388         # get the first node selected by the path and append the new subtree to it
00389         self.get(path)[0][1].append(tree)
    

Here is the call graph for this function:

def arcom.xmltree.XMLTree.get (   self,
  path = None 
)
Returns the parts of the XMLTree which match the path.

get(path = None)

if 'path' is not given, it defaults to the root node

Definition at line 330 of file xmltree.py.

00330 
00331     def get(self, path = None):
00332         """ Returns the parts of the XMLTree which match the path.
00333 
00334         get(path = None)
00335 
00336         if 'path' is not given, it defaults to the root node
00337         """
00338         if path: # if path is given
00339             # if it is not starts with a slash
00340             if not path.startswith('/'):
00341                 raise Exception, 'invalid path (%s)' % path
00342             # remove the starting slash
00343             path = path[1:]
00344             # split the path to a list of strings
00345             path = path.split('/')
00346         else: # if path is not given
00347             # set it to the root node
00348             path = [self._data[0]]
00349         # gets the parts which are selected by this path
00350         return self._traverse(path, self._data)

Here is the call graph for this function:

Here is the caller graph for this function:

def arcom.xmltree.XMLTree.get_dict (   self,
  path = None,
  keys = {} 
)
Returns a dictionary from the first node the path matches.

get_dict(path, keys = {})

'keys' is a dictionary which filters and translate the keys
    e.g. if keys is {'hash:line':'line'}, it will only return
    the 'hash:line' nodes, and will call them 'line'

Definition at line 413 of file xmltree.py.

00413 
00414     def get_dict(self, path = None, keys = {}):
00415         """ Returns a dictionary from the first node the path matches.
00416 
00417         get_dict(path, keys = {})
00418 
00419         'keys' is a dictionary which filters and translate the keys
00420             e.g. if keys is {'hash:line':'line'}, it will only return
00421             the 'hash:line' nodes, and will call them 'line'
00422         """
00423         return self._dict(self.get_value(path,[]),keys)

Here is the call graph for this function:

def arcom.xmltree.XMLTree.get_dicts (   self,
  path = None,
  keys = {} 
)
Returns a list of dictionaries from all the nodes the path matches.

get_dicts(path, keys = {})

'keys' is a dictionary which filters and translate the keys
    e.g. if keys is {'hash:line':'line'}, it will only return
    the 'hash:line' nodes, and will call them 'line'

Definition at line 424 of file xmltree.py.

00424 
00425     def get_dicts(self, path = None, keys = {}):
00426         """ Returns a list of dictionaries from all the nodes the path matches.
00427 
00428         get_dicts(path, keys = {})
00429 
00430         'keys' is a dictionary which filters and translate the keys
00431             e.g. if keys is {'hash:line':'line'}, it will only return
00432             the 'hash:line' nodes, and will call them 'line'
00433         """
00434         return [self._dict(v,keys) for v in self.get_values(path)]

Here is the call graph for this function:

def arcom.xmltree.XMLTree.get_trees (   self,
  path = None 
)
Returns XMLTree object for each subtree which match the path.

get_tress(path = None)

Definition at line 351 of file xmltree.py.

00351 
00352     def get_trees(self, path = None):
00353         """ Returns XMLTree object for each subtree which match the path.
00354 
00355         get_tress(path = None)
00356         """
00357         # get the parts match the path and convert them to XMLTree
00358         return [XMLTree(from_tree = t) for t in self.get(path)]

Here is the call graph for this function:

def arcom.xmltree.XMLTree.get_value (   self,
  path = None,
  args 
)
Returns the value of the selected part.

get_value(path = None, [default])

Returns the value of the node first matched the path.
This is one level deeper than the value returned by the 'get' method.
If there is no such node, and a default is given,
it will return the default.

Definition at line 359 of file xmltree.py.

00359 
00360     def get_value(self, path = None, *args):
00361         """ Returns the value of the selected part.
00362 
00363         get_value(path = None, [default])
00364 
00365         Returns the value of the node first matched the path.
00366         This is one level deeper than the value returned by the 'get' method.
00367         If there is no such node, and a default is given,
00368         it will return the default.
00369         """
00370         try:
00371             # use the get method then get the value of the first result
00372             return self.get(path)[0][1]
00373         except:
00374             # there was an error
00375             if args: # if any more argumentum is given
00376                 # the first will be the default
00377                 return args[0]
00378             raise

Here is the call graph for this function:

Here is the caller graph for this function:

def arcom.xmltree.XMLTree.get_values (   self,
  path = None 
)
Get all the values selected by a path.

get_values(path = None)

Like get_value but gets all values not just the first
This has no default value.

Definition at line 390 of file xmltree.py.

00390 
00391     def get_values(self, path = None):
00392         """ Get all the values selected by a path.
00393 
00394         get_values(path = None)
00395 
00396         Like get_value but gets all values not just the first
00397         This has no default value.
00398         """
00399         try:
00400             # get just the value of each node
00401             return [d[1] for d in self.get(path)]
00402         except:
00403             return []

Here is the call graph for this function:

Here is the caller graph for this function:

def arcom.xmltree.XMLTree.pretty_xml (   self,
  indent = ' ',
  path = None,
  prefix = '' 
)

Definition at line 281 of file xmltree.py.

00281 
00282     def pretty_xml(self, indent = ' ', path = None, prefix = ''):
00283         data = self.get(path)
00284         return self._pretty_xml(data, indent, level = 0, prefix = prefix)

Here is the call graph for this function:


Member Data Documentation

Definition at line 219 of file xmltree.py.


The documentation for this class was generated from the following file: