HappyDoc Generated Documentation Class: Multiloader

. / Demo / multiloader2.py / Multiloader 

Multiloader: The General XML Object Loader & Saver

Multiloader provides the code required to load objects from XML data files, and write them back into the data files after you're done processing them. These objects need to have certain callbacks, which multiloader uses to identify them and how they are to be structured inside the XML file.

Object Format

All objects that are read in or out using the Multiloader must have the following callback functions present:

  • XMLName() -- Returns the XML name of the object.
  • XMLFields() -- Returns a dictionary mapping data members of the object into their associated XML element names.
  • XMLAttributes() -- Returns a dictionary mapping XML element names into lists of tuples. Each tuple represents an attribute that the related element can be given. Each tuple has as its first element the XML name of the attribute, and as its second element the name of the data member of the object where it can store and look up the attribute data.
  • XMLEmbedding() -- Returns a dictionary mapping XML object names to constructors, for objects that can be embedded within the object.

Each class used by Multiloader, including classes embedded inside other classes through XMLEmbedding(), must have all four of these callback functions defined. Embedded objects will be passed a reference to their superobject as the super parameter of their constructors; if this fails, they will be constructed using the default constructors. Furthermore, all objects used by Multiloader must have empty (or keyword only) constructors.

Loading

Once you have created a Multiloader instance, you must load in the data, using the load() function. Once this has been done, you can retrieve the data either by using getObjects(), by using getLookupTable(), or by doing a search with lookup().

After each object is loaded in, its finish() method will be called, if available. This method should do any type conversions or special gimmicks that need to be done once an object is loaded. For example, numeric data members will probably be loaded in as strings, so this function should convert them back to numbers.

Saving

In addition to reading data in using a Multiloader, you can also write data out. There are two functions that do this: save() and saveList(). One takes a structured dictionary as a parameter (just like the one returned by getObjects()) and the other takes a list of objects.

Remember that all of the objects to be written out must have the four callbacks mentioned above defined. If they don't, Multiloader will have no idea how to convert them to XML.

Known Issues
  1. When determining the information about class types using _TypeInfo, a sample instance of each class is created, which may be undesirable, particularly if the constructor for the class does anything important, or if something is keeping track of all the instances of the class that are constructed.
  2. Won't write out ' or for blank elements; instead, does and ', which is valid XML but doesn't look as cool.
Methods   
  load 
load (
        self,
        file,
        objectConstructors,
        lookupField=None,
        )

Load the objects from a file.

This function instructs the Multiloader to load in the objects from the specified XML file. This function must be passed the filename of the XML data file and a list of constructors for the Python objects. It can also be passed a particular field to build a search dictionary against. This lookupField should be the name of the Python data member, not the XML element name.

  saveList 
saveList (
        self,
        filename,
        objects,
        )

Save a list of Python objects into an XML file.

This function converts an arbitrary list of objects into XML, and writes them out to the specified filename. These objects must have the proper callbacks.

  getObjects 
getObjects ( self )

Get the loaded Python objects.

This function returns a dictionary of lists, indexed by the XML name of the objects inside them. Each list contains all of the objects of that type that were read in by the Multiloader on the top level. Embedded objects are stored properly inside these top level objects.

  lookup 
lookup (
        self,
        fieldEntry,
        inClass=None,
        )

Search through the loaded Python objects.

This function searches through the loaded objects for entries that have a particular entry (fieldEntry) in a particular Python data member (the member specified as lookupField to the load function). It returns a dictionary indexed by XML object names, each mapping to a list of objects which have the data fieldEntry in their data member lookupField.

When specifying inClass to be the name of a particular class, the function will merely return a list of objects that have the matching entry.

  save 
save (
        self,
        filename,
        dict,
        )

Save a structured dictionary of Python objects into an XML file.

This function converts a structured dictionary of objects into XML, and writes them out to the specified file. The dictionary must be in the same format as the one returned by the getObjects() function. The objects must have the proper callbacks.

Note: This function essentially turns the dictionary into a list and calls saveList(), so if you've got a list you should use saveList() rather than doing any work to structure it.

  getLookupTable 
getLookupTable ( self )

Get the table used for searches.

This function returns the table created for lookups. This table is represented as a dictionary indexed by XML object name; each XML object name is mapped to another dictionary, which maps the element data stored in the object data member lookupField (passed to the Multiloader upon loading) to the objects that contains that exact element data.

  loadString 
loadString (
        self,
        data,
        objectConstructors,
        lookupField=None,
        )

Load the objects from an XML data string.

This function instructs the Multiloader to load in the objects from a string storing XML data. Aside from the filename being replaced with the data string, the parameters for this function are the same as those for the regular load() function.


This document was automatically generated Thu Nov 15 14:11:40 2007 by HappyDoc version 3.0.a1