Package: xml-preferences
Module xml_preferencesxml_preferences uses a scheme to create a hierarchy of objects that
represent the data stored in an XML files. CHanges to the objects can
later to saved back into an XML file.The inspiration for this module is to store preferences for applications.Classesclass xml_preferences.XmlPreferences__init__( scheme )Theschemeis an instance of xml_preferences.Scheme.load( filename )Reads the preferences from thefilenameand returns the resulting
tree of xml_preferences.PreferencesNode objects.In case of an error xml_preferences.ParseError is raise.loadString( text )Reads the preferences from the stringtextand returns the resulting
tree of xml_preferences.PreferencesNode objects.In case of an error xml_preferences.ParseError is raise.save( data_node )Write the xml_preferences.PreferencesNodedata_nodehierarchy
into the file used to load() the preferences.saveAs( data_node, filename )Write the xml_preferences.PreferencesNodedata_nodehierarchy
intofileaname.saveToString( data_node )Write the xml_preferences.PreferencesNodedata_nodehierarchy
into a string and return the string.saveToFile( data_node, f )Write the xml_preferences.PreferencesNodedata_nodehierarchy
into the file objectf.class xml_preferences.Scheme__init__( document_root )Thedocument_rootis an instance of xml_preferences.SchemeNode
and represent the top level XML document element.dumpScheme( f )Write a dump of the scheme into the file objectf.This is useful for debugging issues with scheme design.class xml_preferences.SchemeNode__init__( factory, element_name, all_attribute_info=None, element_plurality=False, key_attribute=None, collection_name=None, store_as=None, default=True, default_attributes=None )TheSchemeNoderepresents on XML element with the nameelement_name.Any attributes that element has are listed inall_attribute_info, which is a tuple/list of names or name,type pairs.
For example:all_attribute_info=(‘description’, (‘count’, int))TheSchemeNodecan represent an in three ways:The element will only appear once.The element haselement_pluralityset to True and can appear many times and will be stored as a list of elements in its parent.The element has akey_attributeand can appear many times and will be stored as a dictionary in its parent.Thecollection_namedefaults to the element_name. Thecollection_nameis passed to the parent
setChildNodeList or setChildNodeMap functions.store_asdefaults to theelement_nameand is used to name the python variable that this node is store in its in parent object.Whendefaultis True and there is not XML that matches this SchemeNode a default value will be stored in the parent object.When the node is defaulted the attributes of the node can be set from a dictionary pass asdefault_attributes.dumpScheme( f, indent=0 )Write a dump of the scheme into the file objectf.This is useful for debugging issues with scheme design.addSchemeChild( scheme_node )
__lshift__( scheme_node )Addscheme_nodeas a child element of this SchemeNode.
The << operator is useful in making the scheme definition readable.class xml_preferences.PreferencesNodeFor typical use all the set and get functions provide all the necessary features.
That can be overriden to create special behaviour. It is assumed that all
attribures are initised to a suitable value in __init__.__init__()Derive fromPreferencesNodeto initialise the variables used to hold the parsed XML preferences.finaliseNode( self )Called after all attributes and child nodes have been set on this node.Use this call to default any missing preferences.setAttr( self, name, value )Called to save the value of an attribute. The default implemention is:setattr( self, name, value )setChildNode( self, name, node )Called to save the value of singleton child element. The default implemention is:setattr( self, name, node )setChildNodeList( self, collection_name, node )Called to save the value of the next element to added to a list. The default implemention is:getattr( self, collection_name ).append( node )setChildNodeMap( self, collection_name, key, node )Called to save the value of the next element into a dict using thekey. The default implemention is:getattr( self, collection_name )[ key ] = nodegetAttr( self, name )Called to get the value of thenameattribute. The default implemention is:return getattr( self, name )getChildNode( self, name )Called to get the value of thenamechild node. The default implemention is:return getattr( self, name )getChildNodeList( self, collection_name )Called to get a list of values of thecollection_namechild nodes, which are assumed to be stored in a list. The default implemention is:return getattr( self, name )getChildNodeMap( self, collection_name )Called to get a list value of thecollection_namechild nodes, which are assumed to be stored in a dict. The default implemention is:return getattr( self, name ).values()dumpNode( self, f, indent=0 )Write a dump of the PreferencesNode hierarchy into file objectf.Setindentto the number of spaces to indent the dumped text.Useful when debugging.ExampleSee test_xml_preferences.py for example use.