Author: remi
Date: 2009-03-23 10:18:00 +0100 (Mon, 23 Mar 2009)
New Revision: 4174
Modified:
software_suite_v2/tuxware/tuxdroidserver/trunk/util/xml/XmlSerializer.py
Log:
* added doc comments.
Modified:
software_suite_v2/tuxware/tuxdroidserver/trunk/util/xml/XmlSerializer.py
===================================================================
--- software_suite_v2/tuxware/tuxdroidserver/trunk/util/xml/XmlSerializer.py
2009-03-22 21:56:34 UTC (rev 4173)
+++ software_suite_v2/tuxware/tuxdroidserver/trunk/util/xml/XmlSerializer.py
2009-03-23 09:18:00 UTC (rev 4174)
@@ -14,18 +14,19 @@
from xml.sax import make_parser
#
------------------------------------------------------------------------------
-#
+# Xml parser handler to make a dictionary from a xml content.
#
------------------------------------------------------------------------------
class DictionaryHandler(ContentHandler):
+ """Xml parser handler to make a dictionary from a xml content.
"""
- """
#
--------------------------------------------------------------------------
- #
+ # Constructor of the class.
#
--------------------------------------------------------------------------
def __init__(self, encoding = "utf-8"):
+ """Constructor of the class.
+ @param encoding: Encoding of the xml content.
"""
- """
self.__currentNodeName = ''
self.__parentNodeName = ''
self.__nodesCounter = 0
@@ -37,19 +38,22 @@
self.__encoding = encoding
#
--------------------------------------------------------------------------
- #
+ # Get the resulting dictionary.
#
--------------------------------------------------------------------------
def getDictionary(self):
+ """Get the resulting dictionary.
+ @return: A dictionary.
"""
- """
return self.__dictionary
#
--------------------------------------------------------------------------
- #
+ # Start event of a xml element parsing.
#
--------------------------------------------------------------------------
def startElement(self, nodeName, nodeAttributes):
+ """Start event of a xml element parsing.
+ @param nodeName: Xml node name.
+ @param nodeAttributes: Xml node attributes.
"""
- """
if self.__nodesCounter == 0:
self.__parentNodeName = nodeName
self.__dictionary[nodeName] = [dict(nodeAttributes), '', []]
@@ -70,22 +74,24 @@
self.__nodesCounter += 1
#
--------------------------------------------------------------------------
- #
+ # End event of a xml element parsing.
#
--------------------------------------------------------------------------
def endElement(self, nodeName):
+ """End event of a xml element parsing.
+ @param nodeName: Xml node name.
"""
- """
self.__nodeNameStack.remove(nodeName)
for item in self.__nodeDictsStack:
if item.has_key(nodeName):
self.__nodeDictsStack.remove(item)
#
--------------------------------------------------------------------------
- #
+ # Event on text content parsing.
#
--------------------------------------------------------------------------
def characters(self, content):
+ """Event on text content parsing.
+ @param content: Text content.
"""
- """
content = content.encode(self.__encoding).strip()
if content:
nodeDict = self.__parentNodeDict[self.__parentNodeName][2][-1]
@@ -94,37 +100,39 @@
content))
#
------------------------------------------------------------------------------
-#
+# Xml parser to make a xml content from a dictionary.
#
------------------------------------------------------------------------------
class XmlDictionaryParser(XMLReader):
+ """Xml parser to make a xml content from a dictionary.
"""
- """
#
--------------------------------------------------------------------------
- #
+ # Constructor of the class.
#
--------------------------------------------------------------------------
def __init__(self):
+ """Constructor of the class.
"""
- """
XMLReader.__init__(self)
self.__indent = 0
#
--------------------------------------------------------------------------
- #
+ # Start the parsing.
#
--------------------------------------------------------------------------
def parse(self, dictionary):
+ """Start the parsing.
+ @param dictionary: Input dictionary.
"""
- """
self._cont_handler.startDocument()
self.__parse(dictionary)
self._cont_handler.endDocument()
#
--------------------------------------------------------------------------
- #
+ # Recursive parsing method.
#
--------------------------------------------------------------------------
def __parse(self, dictionary):
+ """Recursive parsing method.
+ @param dictionary: Input dictionary.
"""
- """
for name, data in dictionary.items():
# Print formating
self._cont_handler.ignorableWhitespace(" " * self.__indent * 4)
@@ -148,18 +156,20 @@
self._cont_handler.ignorableWhitespace('\n')
#
------------------------------------------------------------------------------
-#
+# Class to serialize python dictionaries to xml contents.
#
------------------------------------------------------------------------------
class XmlSerializer(object):
+ """Class to serialize python dictionaries to xml contents.
"""
- """
#
--------------------------------------------------------------------------
- #
+ # Deserialize a xml file to a dictionary.
#
--------------------------------------------------------------------------
def deserialize(xmlFileName):
+ """Deserialize a xml file to a dictionary.
+ @param xmlFileName: Input xml file.
+ @return: The resulting dictionary.
"""
- """
# Get the xml encoding
encoding = "utf-8"
try:
@@ -183,13 +193,18 @@
return dictionaryHandler.getDictionary()
except:
return None
-
+
#
--------------------------------------------------------------------------
- #
+ # Deserialize a xml file to a dictionary. (Simpless dictionary srtucture)
#
--------------------------------------------------------------------------
def deserializeEx(xmlFileName):
+ """Deserialize a xml file to a dictionary.
+ (Simpless dictionary srtucture)
+ @param xmlFileName: Input xml file.
+ @return: The resulting dictionary.
+ This function is more usefull than the "deserialize" one, because the
+ structure is less complexe. But the resulting xml can't be
re-serialized
"""
- """
dictionary = XmlSerializer.deserialize(xmlFileName)
if dictionary == None:
return None
@@ -219,11 +234,13 @@
return recNodes(dictionary[key])
#
--------------------------------------------------------------------------
- #
+ # Serialize a dictionary to a xml content.
#
--------------------------------------------------------------------------
def serialize(dictionary):
+ """Serialize a dictionary to a xml content.
+ @param dictionary: Dictionary to serialize.
+ @return: The resulting xml content.
"""
- """
stringContent = cStringIO.StringIO()
try:
parser = XmlDictionaryParser()
------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn