]> gitweb.michael.orlitzky.com - dead/pictar.git/blob - Mime/XmlDataParser.py
Updated XmlDataParser to use the Python 2.5 ElementTree interface.
[dead/pictar.git] / Mime / XmlDataParser.py
1 import os
2 from SupportedType import SupportedType
3 from xml.etree.ElementTree import ElementTree
4
5 class XmlDataParser:
6 """
7 This class parses all of the information in the config file, and
8 makes it available through various methods.
9 """
10
11 def __init__(self, xml_file_path):
12 """
13 We build a list of supported mimetypes upon instantiation.
14 """
15 self.__supported_types = []
16
17 xml_doc = ElementTree()
18 xml_doc.parse(xml_file_path)
19
20 supported_type_elements = xml_doc.findall('supported_type')
21
22 # For each supported_type element, we build a SupportedType
23 # object and add it to our __supported_types list.
24
25 for current_node in supported_type_elements:
26 name = self.GetChildrenText(current_node, 'name')[0]
27 descriptions = self.GetChildrenText(current_node, 'description')
28 extensions = self.GetChildrenText(current_node, 'extension')
29 template_files = self.GetChildrenText(current_node, 'template_file')
30 template_data = self.GetChildrenText(current_node, 'template_data')
31
32 current_type = SupportedType(name = name, extensions = extensions)
33
34 if (len(descriptions) > 0):
35 current_type.description = descriptions[0]
36
37 if (len(template_files) > 0):
38 # Allow for relative paths in the template field
39 root_dir = os.path.dirname(xml_file_path)
40 current_type.template_file = os.path.join(root_dir, template_files[0])
41
42 if (len(template_data) > 0):
43 current_type.template_data = template_data[0]
44
45 self.__supported_types.append(current_type)
46
47 return
48
49
50
51 def GetAllExtensions(self):
52 all_extensions = []
53
54 for current_type in self.__supported_types:
55 for extension in current_type.extensions:
56 all_extensions.append(extension)
57
58 return all_extensions
59
60
61
62 def GetTemplateByExtension(self, extension):
63 """
64 Get the (x)html template which corresponds to a mimetype
65 having the supplied extension.
66 """
67 for current_type in self.__supported_types:
68 for current_extension in current_type.extensions:
69 if (current_extension.lower() == extension.lower()):
70 return current_type.GetTemplate()
71
72 return None
73
74
75
76 def GetNameByExtension(self, extension):
77 """
78 Get the name of the mimetype to which a particular extension
79 belongs.
80 """
81 for current_type in self.__supported_types:
82 for current_extension in current_type.extensions:
83 if (current_extension.lower() == extension.lower()):
84 return current_type.name
85
86 return None
87
88
89
90 def GetChildrenText(self, node, child_tag_name):
91 """
92 Find the text of all children of node having child_tag_name.
93 For example, if we were passed a supported_type for node, we
94 might want to find the text of all extension elements.
95 """
96
97 children_text = []
98 children = node.findall(child_tag_name)
99
100 for child_node in children:
101 children_text.append(child_node.text)
102
103 return children_text