Package qubx :: Module xmlt
[hide private]
[frames] | no frames]

Module xmlt

source code

XML tools; so far just conversion between object attributes and xml.

Copyright 2007-2011 Research Foundation State University of New York This file is part of QUB Express.

QUB Express is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

QUB Express is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License, named LICENSE.txt, in the QUB Express program directory. If not, see <http://www.gnu.org/licenses/>.

Classes [hide private]
  Field
Field(name, default, accept=str, format=str): an attribute for <properties>.
Functions [hide private]
 
XMLPrettyPrint(doc, f=None) source code
 
PropertiesToXML(obj, doc, ns, fields)
Returns a DOM element representing specified fields of obj.
source code
 
XMLToProperties(properties, obj, fields=None)
Sets attributes of obj from xml.dom element <properties>.
source code
Variables [hide private]
  __package__ = 'qubx'
  e = 2.71828182846
  pi = 3.14159265359
  rdbk = 'Expired.'
  rdbreak = 'supporting'
  rdname = 'qubfast.txt'
Function Details [hide private]

PropertiesToXML(obj, doc, ns, fields)

source code 

Returns a DOM element representing specified fields of obj.

  • obj: any python object
  • doc: the destination xml.dom Document
  • ns: doc's xml.dom namespace
  • fields: a list of qubx.xmlt.Field(name, default, accept=str, format=str)

example:

>>> obj = object()
>>> obj.name = 'x'
>>> obj.length = obj.volume = 0.0
>>> fields = [Field(name='name', accept=str, default='untitled'),
...           Field(name='length', accept=float, format=str, default=2.3),
...           Field(name='volume', accept=float, format='%9g', default=2.3)]
>>> print PropertiesToXML(obj, doc, ns, fields)
<properties>
  <property name='name'>x</property>
  <property name='length'>0.0</property>
  <property name='volume'>0.0</property>
</properties>

XMLToProperties(properties, obj, fields=None)

source code 

Sets attributes of obj from xml.dom element <properties>.

Parameters:
  • properties - an xml.dom element with children <property name=''>value</property>
  • obj - the receiver of obj.name = value
  • fields - optional list of qubx.xmlt.Field(name, default, accept=str, format=str):
     if fields is None:
         obj.name = string value for all <property> elements
     else:
         only <property>s listed in fields are copied;
         obj.name = default if no <property> or failed "accept" conversion
    
     example:
    
     >>> obj = object()
     >>> fields = [Field(name='name', accept=str, default='untitled'),
     ...           Field(name='length', accept=float, format=str, default=2.3),
     ...           Field(name='volume', accept=float, format='%9g', default=2.3)]
     >>> XMLToProperties(properties_element, obj, fields)
     >>> print obj.name, obj.length, obj.volume
     x, 0.0, 0.0