Updated XmlDataParser to use the Python 2.5 ElementTree interface. master
authorMichael Orlitzky <michael@orlitzky.com>
Tue, 6 Jan 2009 23:36:42 +0000 (18:36 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Tue, 6 Jan 2009 23:36:42 +0000 (18:36 -0500)
Changed some function comments to docstrings.
Removed the silly accessors from SupportedType and replaced them with public fields.

Mime/SupportedType.py
Mime/XmlDataParser.py
Pictar.py
TemplateParser.py

index 9ab2f3e217c2e31e572cf9c52f67d46aef7f7fd6..c0f2267686522c9aed2dea3bef27b29c2e5de1d2 100644 (file)
@@ -1,12 +1,10 @@
-
-"""
-Wrapper class for the 'filetype' elements in the config file. Nothing
-tricky here, except that the accessor for the template should
-intelligently determine which of the two fields (template_file,
-template_data) is used as the template source.
-"""
-
 class SupportedType:
+    """
+    Wrapper class for the 'filetype' elements in the config
+    file. Nothing tricky here, except that the accessor for the
+    template should intelligently determine which of the two fields
+    (template_file, template_data) is used as the template source.
+    """
 
     def __init__(self,
                  name = "",
@@ -15,62 +13,23 @@ class SupportedType:
                  template_file = "",
                  template_data = ""):
         
-        self.__name = name
-        self.__description = description
-        self.__extensions = extensions
-        self.__template_file = template_file
-        self.__template_data = template_data
+        self.name = name
+        self.description = description
+        self.extensions = extensions
+        self.template_file = template_file
+        self.template_data = template_data
 
         return
 
 
-
-    def GetName(self):
-        return self.__name
-
-
-    def GetDescription(self):
-        return self.__description
-
-    
-    def GetExtensions(self):
-        return self.__extensions
-
-    
     def GetTemplate(self):
-        if (self.__template_data == None or self.__template_data == ""):
+        if (self.template_data == None or self.template_data == ''):
             # The data is not given explicitly, so rely on the
             # template file to provide it.
-            template_file = open(self.__template_file, "r")
+            template_file = open(self.template_file, 'r')
             template = template_file.read()
             template_file.close()
             return template
         else:
-            return self.__template_data
-        
-
-
-    def SetName(self, name):
-        self.__name = name
-        return
-
-
-    def SetDescription(self, description):
-        self.__description = description
-        return
-
-
-    def SetExtensions(self, extensions):
-        self.__extensions = extensions
-        return
-
-    
-    def SetTemplateData(self, template_data):
-        self.__template_data = template_data
-        return
-
-    
-    def SetTemplateFile(self, template_file):
-        self.__template_file = template_file
-        return
+            return self.template_data
     
index 4cd3bec7f36474f4c806423dcde257b2dd2967cb..6d703a6b70811b4728449413cc2323b021c13c71 100644 (file)
@@ -1,22 +1,27 @@
 import os
 from SupportedType import SupportedType
-from xml.dom import Node
-from xml.dom.ext.reader.Sax import FromXmlFile
-
-"""
-This class parses all of the information in the config file, and makes
-it available through various methods.
-"""
+from xml.etree.ElementTree import ElementTree
 
 class XmlDataParser:
+    """
+    This class parses all of the information in the config file, and
+    makes it available through various methods.
+    """
     
     def __init__(self, xml_file_path):
+        """
+        We build a list of supported mimetypes upon instantiation.
+        """
         self.__supported_types = []
 
-        xml_doc = FromXmlFile(xml_file_path)
-
-        supported_type_elements = xml_doc.getElementsByTagName('supported_type')
+        xml_doc = ElementTree()
+        xml_doc.parse(xml_file_path)
         
+        supported_type_elements = xml_doc.findall('supported_type')
+
+        # For each supported_type element, we build a SupportedType
+        # object and add it to our __supported_types list.
+
         for current_node in supported_type_elements:
             name = self.GetChildrenText(current_node, 'name')[0]
             descriptions = self.GetChildrenText(current_node, 'description')
@@ -24,24 +29,19 @@ class XmlDataParser:
             template_files = self.GetChildrenText(current_node, 'template_file')        
             template_data = self.GetChildrenText(current_node, 'template_data')
             
-            current_type = SupportedType()
-            current_type.SetName(name)
-            current_type.SetExtensions(extensions)
+            current_type = SupportedType(name = name, extensions = extensions)
 
             if (len(descriptions) > 0):
-                current_type.SetDescription(descriptions[0])
-
+                current_type.description = descriptions[0]
                 
             if (len(template_files) > 0):
                 # Allow for relative paths in the template field
                 root_dir = os.path.dirname(xml_file_path)
-                template_file_path = os.path.join(root_dir, template_files[0])
-                current_type.SetTemplateFile(template_file_path)
-
+                current_type.template_file = os.path.join(root_dir, template_files[0])
                        
             if (len(template_data) > 0):
-                current_type.SetTemplateData(template_data[0])
-
+                current_type.template_data = template_data[0]
+            
             self.__supported_types.append(current_type)
 
         return
@@ -52,8 +52,7 @@ class XmlDataParser:
         all_extensions = []
         
         for current_type in self.__supported_types:
-            current_type_extensions = current_type.GetExtensions()
-            for extension in current_type_extensions:
+            for extension in current_type.extensions:
                 all_extensions.append(extension)
 
         return all_extensions
@@ -61,12 +60,12 @@ class XmlDataParser:
     
         
     def GetTemplateByExtension(self, extension):
-        # Get the (x)html template which corresponds to a mimetype
-        # having the supplied extension.
+        """
+        Get the (x)html template which corresponds to a mimetype
+        having the supplied extension.
+        """
         for current_type in self.__supported_types:
-            current_type_extensions = current_type.GetExtensions()
-
-            for current_extension in current_type_extensions:
+            for current_extension in current_type.extensions:
                 if (current_extension.lower() == extension.lower()):
                     return current_type.GetTemplate()
             
@@ -75,45 +74,30 @@ class XmlDataParser:
 
 
     def GetNameByExtension(self, extension):
-        # Get the name of the mimetype that a particular extension
-        # belongs to.
+        """
+        Get the name of the mimetype to which a particular extension
+        belongs.
+        """
         for current_type in self.__supported_types:
-            current_type_extensions = current_type.GetExtensions()
-
-            for current_extension in current_type_extensions:
+            for current_extension in current_type.extensions:
                 if (current_extension.lower() == extension.lower()):
-                    return current_type.GetName()
+                    return current_type.name
             
         return None
 
     
             
-    def GetChildNodesByTagName(self, node, tag_name):
-        found_children = []
-        
-        for child in node.childNodes:
-            if (child.nodeName == tag_name):
-                found_children.append(child)
-
-        return found_children
-
-
-
-    
     def GetChildrenText(self, node, child_tag_name):
-        children_text = []
+        """
+        Find the text of all children of node having child_tag_name.
+        For example, if we were passed a supported_type for node, we
+        might want to find the text of all extension elements.
+        """
         
-        children = self.GetChildNodesByTagName(node, child_tag_name)
+        children_text = []        
+        children = node.findall(child_tag_name)
 
         for child_node in children:
-            for sub_child in child_node.childNodes:
-                if (sub_child.nodeType == Node.TEXT_NODE):
-                    children_text.append(sub_child.nodeValue)
-                    break
-
+            children_text.append(child_node.text)
                 
         return children_text
-
-
-    
-
index 67a056fb42a08dc5e0e4ab5df947208c4e68fea7..d889c1e0a3e6cf91dbaa09cf396a576ebffbca37 100644 (file)
--- a/Pictar.py
+++ b/Pictar.py
@@ -31,9 +31,11 @@ class Pictar:
 
 
     def GetItemPaths(self):
-        # Create a list of the item paths based on which
-        # of the files in the target directory match at least
-        # one of the item regular expressions.
+        """
+        Create a list of the item paths based on which
+        of the files in the target directory match at least
+        one of the item regular expressions.
+        """
         target_dir = Configuration.GetAbsTargetDir()
         
         item_paths = []
@@ -52,7 +54,7 @@ class Pictar:
 
 
     def GetItemData(self, item_paths):
-        # Build the pictar markup
+        """ Build the pictar markup. """
         item_data = ""
         current_item_number = 0
         
@@ -76,7 +78,7 @@ class Pictar:
 
 
     def GetPageData(self, item_data):
-        # Fill in the template with our markup
+        """ Fill in the template with our markup. """
         stylesheet_file_name = Configuration.GetStyleSheetPath()
         page_template = self.template_parser.GetPageTemplate()
         page_data = page_template.substitute(stylesheet=stylesheet_file_name,
index fcf044cd22ae0a32d7a5854834d302cd66772e61..12da1d38a11342ddd87a8274c4fd04ec57148553 100644 (file)
@@ -5,12 +5,10 @@ from string import Template
 class TemplateParser:
 
     def GetPageTemplate(self):
-        # Read the page template file into a string
+        """ Read the page template file into a string. """
         page_template_path = Configuration.GetPageTemplatePath()
         page_template_file = open(page_template_path, "r")
         page_template = Template(page_template_file.read())
         page_template_file.close()
 
         return page_template
-
-