CMS 3D CMS Logo

Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes

XML2Python::DataNode Class Reference

List of all members.

Public Member Functions

def __contains__
def __contains__
def __getattr__
def __getattr__
def __getitem__
def __getitem__
def __init__
def __init__
def __len__
def __len__
def __nonzero__
def __nonzero__
def __repr__
def __repr__
def __str__
def __str__
def attributes
def attributes
def isiterable
def stringify

Static Public Attributes

int spaces = 4

Private Member Functions

def _add_xml_attr
def _add_xml_attr
def _outputValues

Private Attributes

 _attrs
 _data
 _ncDict

Detailed Description

Definition at line 10 of file XML2Python.py.


Constructor & Destructor Documentation

def XML2Python::DataNode::__init__ (   self,
  kwargs 
)

Definition at line 14 of file XML2Python.py.

00015                                  :
00016         self._attrs = {}     # XML attributes and child elements
00017         self._data  = None   # child text data
00018         self._ncDict = kwargs.get ('nameChangeDict', {})
00019 

def XML2Python::DataNode::__init__ (   self,
  kwargs 
)

Definition at line 11 of file XML2Python.py.

00012                                  :
00013         self._attrs = {}     # XML attributes and child elements
00014         self._data  = None   # child text data
00015         self._ncDict = kwargs.get ('nameChangeDict', {})


Member Function Documentation

def XML2Python::DataNode::__contains__ (   self,
  name 
)

Definition at line 32 of file XML2Python.py.

00033                                  :
00034         return self._attrs.has_key(name)
00035 

def XML2Python::DataNode::__contains__ (   self,
  name 
)

Definition at line 26 of file XML2Python.py.

00027                                  :
00028         return self._attrs.has_key(name)

def XML2Python::DataNode::__getattr__ (   self,
  name 
)

Definition at line 40 of file XML2Python.py.

00041                                 :
00042         if name.startswith('__'):
00043             # need to do this for Python special methods???
00044             raise AttributeError (name)
00045         return self._attrs.get (name, None)
00046 

def XML2Python::DataNode::__getattr__ (   self,
  name 
)

Definition at line 32 of file XML2Python.py.

00033                                 :
00034         if name.startswith('__'):
00035             # need to do this for Python special methods???
00036             raise AttributeError (name)
00037         return self._attrs.get (name, None)

def XML2Python::DataNode::__getitem__ (   self,
  key 
)

Definition at line 25 of file XML2Python.py.

00026                                :
00027         if isinstance (key, basestring):
00028             return self._attrs.get(key,None)
00029         else:
00030             return [self][key]
00031 

def XML2Python::DataNode::__getitem__ (   self,
  key 
)

Definition at line 20 of file XML2Python.py.

00021                                :
00022         if isinstance (key, basestring):
00023             return self._attrs.get(key,None)
00024         else:
00025             return [self][key]

def XML2Python::DataNode::__len__ (   self)

Definition at line 20 of file XML2Python.py.

00021                       :
00022         # treat single element as a list of 1
00023         return 1
00024 

def XML2Python::DataNode::__len__ (   self)

Definition at line 16 of file XML2Python.py.

00017                       :
00018         # treat single element as a list of 1
00019         return 1

def XML2Python::DataNode::__nonzero__ (   self)

Definition at line 36 of file XML2Python.py.

00037                           :
00038         return bool (self._attrs or self._data)
00039 

def XML2Python::DataNode::__nonzero__ (   self)

Definition at line 29 of file XML2Python.py.

00030                           :
00031         return bool (self._attrs or self._data)

def XML2Python::DataNode::__repr__ (   self)

Definition at line 66 of file XML2Python.py.

00067                        :
00068         items = sorted (self._attrs.items())
00069         if self._data:
00070             items.append(('data', self._data))
00071         return u'{%s}' % ', '.join([u'%s:%s' % (k,repr(v)) for k,v in items])
00072 

def XML2Python::DataNode::__repr__ (   self)

Definition at line 55 of file XML2Python.py.

00056                        :
00057         items = sorted (self._attrs.items())
00058         if self._data:
00059             items.append(('data', self._data))
00060         return u'{%s}' % ', '.join([u'%s:%s' % (k,repr(v)) for k,v in items])

def XML2Python::DataNode::__str__ (   self)

Definition at line 52 of file XML2Python.py.

00053                       :
00054         return self._data or ''

def XML2Python::DataNode::__str__ (   self)

Definition at line 62 of file XML2Python.py.

00063                       :
00064         return self.stringify()
00065 

def XML2Python::DataNode::_add_xml_attr (   self,
  name,
  value 
) [private]

Definition at line 38 of file XML2Python.py.

00039                                          :
00040         change = self._ncDict.get (name)
00041         if change:
00042             name = change
00043         if name in self._attrs:
00044             # multiple attribute of the same name are represented by a list
00045             children = self._attrs[name]
00046             if not isinstance(children, list):
00047                 children = [children]
00048                 self._attrs[name] = children
00049             children.append(value)
00050         else:
00051             self._attrs[name] = value

def XML2Python::DataNode::_add_xml_attr (   self,
  name,
  value 
) [private]

Definition at line 47 of file XML2Python.py.

00048                                          :
00049         change = self._ncDict.get (name)
00050         if change:
00051             name = change
00052         if name in self._attrs:
00053             # multiple attribute of the same name are represented by a list
00054             children = self._attrs[name]
00055             if not isinstance(children, list):
00056                 children = [children]
00057                 self._attrs[name] = children
00058             children.append(value)
00059         else:
00060             self._attrs[name] = value
00061 

def XML2Python::DataNode::_outputValues (   obj,
  name,
  offset 
) [private]

Definition at line 83 of file XML2Python.py.

00084                                          :
00085         retval = ' ' * offset
00086         if name:
00087             retval += '%s: ' % name
00088             offset += len (name) + DataNode.spaces
00089         # if this is a list
00090         if isinstance (obj, list):
00091             first = True
00092             for value in obj:
00093                 print "value", value, value.__class__.__name__
00094                 if first:
00095                     tempoffset = offset
00096                     first = False
00097                     retval += '[\n ' + ' ' * offset
00098                 else:
00099                     retval += ',\n ' + ' ' * offset
00100                     tempoffset = offset
00101                 if isinstance (value, DataNode):
00102                     retval += value.stringify (offset=tempoffset)
00103                     print "  calling stringify for %s" % value
00104                 elif DataNode.isiterable (value):
00105                     retval += DataNode._outputValues (value, '', offset)
00106                 else:
00107                     retval += "%s" % value
00108             retval += '\n' + ' ' * (offset - 2) +']\n'
00109             return retval
00110         retval += pprint.pformat(obj,
00111                                  indent= offset,
00112                                  width=1)
00113         return retval
00114 

def XML2Python::DataNode::attributes (   self)

Definition at line 73 of file XML2Python.py.

00074                          :
00075         return self._attrs
00076 

def XML2Python::DataNode::attributes (   self)

Definition at line 61 of file XML2Python.py.

00062                          :
00063         return self._attrs
00064 

def XML2Python::DataNode::isiterable (   obj)

Definition at line 78 of file XML2Python.py.

00079                         :
00080         return getattr (obj, '__iter__', False)
00081 

def XML2Python::DataNode::stringify (   self,
  name = '',
  offset = 0 
)

Definition at line 115 of file XML2Python.py.

00116                                                :
00117         # is this just data and nothing below
00118         if self._data and not len (self._attrs):
00119             return _outputValues (self._data, name, offset)
00120             retval = ' ' * offset
00121             if name:
00122                 retval += '%s : %s\n' % \
00123                           (name,
00124                            pprint.pformat (self._data,
00125                                           indent= offset+DataNode.spaces,
00126                                           width=1) )
00127             else:
00128                 retval += pprint.pformat (self._data,
00129                                           indent=offset+DataNode.spaces,
00130                                           width=1)
00131             return retval
00132         # this has attributes
00133         retval = ''
00134         if name:
00135             retval += '\n' + ' ' * offset
00136             retval += '%s: ' % name
00137         first = True
00138         for key, value in sorted (self._attrs.iteritems()):
00139             if first:
00140                 retval += '{ \n'
00141                 tempspace = offset + 3
00142                 first = False
00143             else:
00144                 retval += ',\n'
00145                 tempspace = offset + 3
00146             if isinstance (value, DataNode):
00147                 retval += value.stringify (key, tempspace)
00148             else:
00149                 retval += DataNode._outputValues (value, key, tempspace)
00150         # this has data too
00151         if self._data:
00152             retval += ',\n'
00153             tempspace = offset + 3
00154             retval += DataNode._ouptputValues (self._data, name, tempspace)
00155         retval += '\n ' + ' ' * offset + '}'
00156         return retval 
00157         
00158 


Member Data Documentation

Definition at line 14 of file XML2Python.py.

Definition at line 14 of file XML2Python.py.

Definition at line 14 of file XML2Python.py.

int XML2Python::DataNode::spaces = 4 [static]

Definition at line 12 of file XML2Python.py.