-
-"""
-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 = "",
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
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')
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
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
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()
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
-
-
-
-
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 = []
def GetItemData(self, item_paths):
- # Build the pictar markup
+ """ Build the pictar markup. """
item_data = ""
current_item_number = 0
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,