X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fpictar.git;a=blobdiff_plain;f=Mime%2FXmlDataParser.py;fp=Mime%2FXmlDataParser.py;h=6d703a6b70811b4728449413cc2323b021c13c71;hp=4cd3bec7f36474f4c806423dcde257b2dd2967cb;hb=a913a82677784cb48487a37e05ffb04c98dee0ad;hpb=c21bdc3222daa73da76c057ee00106e04ace4c77 diff --git a/Mime/XmlDataParser.py b/Mime/XmlDataParser.py index 4cd3bec..6d703a6 100644 --- a/Mime/XmlDataParser.py +++ b/Mime/XmlDataParser.py @@ -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 - - - -