Skip to main content

xml-preferences reads and writes preferences infomation from XML files.

Project description

Module xml_preferences

xml_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.

Classes

class xml_preferences.XmlPreferences

__init__( scheme )

The scheme is an instance of xml_preferences.Scheme.

load( filename )

Reads the preferences from the filename and 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 string text and 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.PreferencesNode data_node hierarchy into the file used to load() the preferences.

saveAs( data_node, filename )

Write the xml_preferences.PreferencesNode data_node hierarchy into fileaname.

saveToString( data_node )

Write the xml_preferences.PreferencesNode data_node hierarchy into a string and return the string.

saveToFile( data_node, f )

Write the xml_preferences.PreferencesNode data_node hierarchy into the file object f.

class xml_preferences.Scheme

__init__( document_root )

The document_root is 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 object f.

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 )

The SchemeNode represents on XML element with the name element_name.

Any attributes that element has are listed in all_attribute_info, which is a tuple/list of names or name,type pairs. For example:

all_attribute_info=(‘description’, (‘count’, int))

The SchemeNode can represent an in three ways:

  1. The element will only appear once.

  2. The element has element_plurality set to True and can appear many times and will be stored as a list of elements in its parent.

  3. The element has a key_attribute and can appear many times and will be stored as a dictionary in its parent.

The collection_name defaults to the element_name. The collection_name is passed to the parent setChildNodeList or setChildNodeMap functions.

store_as defaults to the element_name and is used to name the python variable that this node is store in its in parent object.

When default is 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 as default_attributes.

dumpScheme( f, indent=0 )

Write a dump of the scheme into the file object f.

This is useful for debugging issues with scheme design.

addSchemeChild( scheme_node ) __lshift__( scheme_node )

Add scheme_node as a child element of this SchemeNode. The << operator is useful in making the scheme definition readable.

class xml_preferences.PreferencesNode

For 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 from PreferencesNode to 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 the key. The default implemention is:

getattr( self, collection_name )[ key ] = node

getAttr( self, name )

Called to get the value of the name attribute. The default implemention is:

return getattr( self, name )

getChildNode( self, name )

Called to get the value of the name child node. The default implemention is:

return getattr( self, name )

getChildNodeList( self, collection_name )

Called to get a list of values of the collection_name child 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 the collection_name child 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 object f.

Set indent to the number of spaces to indent the dumped text.

Useful when debugging.

Example

See test_xml_preferences.py for example use.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xml-preferences-1.1.6.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

xml_preferences-1.1.6-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file xml-preferences-1.1.6.tar.gz.

File metadata

  • Download URL: xml-preferences-1.1.6.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for xml-preferences-1.1.6.tar.gz
Algorithm Hash digest
SHA256 e679392fde186a7eec675dad66f3042b7d60bb428e462ff9313f5535d4967acd
MD5 7e006ac86f019c3b08b63dc7abd1c3d1
BLAKE2b-256 9e7c5f1a0551ff529331cef3cb300add32963637248c612bda54feb074fe0b78

See more details on using hashes here.

File details

Details for the file xml_preferences-1.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for xml_preferences-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 87bf0dc333969e03a8ca807f416a58ce57315334132992b8dfec1fcbb2c455b0
MD5 46676cb9e8d87615d1693710a749bbad
BLAKE2b-256 083431e16ad7d4a503de555a4a808542c685953aba0f7c416a3465ac19974d05

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page