]> gitweb.michael.orlitzky.com - dead/pictar.git/blob - Mime/XmlDataParser.py
4cd3bec7f36474f4c806423dcde257b2dd2967cb
[dead/pictar.git] / Mime / XmlDataParser.py
1 import os
2 from SupportedType import SupportedType
3 from xml.dom import Node
4 from xml.dom.ext.reader.Sax import FromXmlFile
5
6 """
7 This class parses all of the information in the config file, and makes
8 it available through various methods.
9 """
10
11 class XmlDataParser:
12
13 def __init__(self, xml_file_path):
14 self.__supported_types = []
15
16 xml_doc = FromXmlFile(xml_file_path)
17
18 supported_type_elements = xml_doc.getElementsByTagName('supported_type')
19
20 for current_node in supported_type_elements:
21 name = self.GetChildrenText(current_node, 'name')[0]
22 descriptions = self.GetChildrenText(current_node, 'description')
23 extensions = self.GetChildrenText(current_node, 'extension')
24 template_files = self.GetChildrenText(current_node, 'template_file')
25 template_data = self.GetChildrenText(current_node, 'template_data')
26
27 current_type = SupportedType()
28 current_type.SetName(name)
29 current_type.SetExtensions(extensions)
30
31 if (len(descriptions) > 0):
32 current_type.SetDescription(descriptions[0])
33
34
35 if (len(template_files) > 0):
36 # Allow for relative paths in the template field
37 root_dir = os.path.dirname(xml_file_path)
38 template_file_path = os.path.join(root_dir, template_files[0])
39 current_type.SetTemplateFile(template_file_path)
40
41
42 if (len(template_data) > 0):
43 current_type.SetTemplateData(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 current_type_extensions = current_type.GetExtensions()
56 for extension in current_type_extensions:
57 all_extensions.append(extension)
58
59 return all_extensions
60
61
62
63 def GetTemplateByExtension(self, extension):
64 # Get the (x)html template which corresponds to a mimetype
65 # having the supplied extension.
66 for current_type in self.__supported_types:
67 current_type_extensions = current_type.GetExtensions()
68
69 for current_extension in current_type_extensions:
70 if (current_extension.lower() == extension.lower()):
71 return current_type.GetTemplate()
72
73 return None
74
75
76
77 def GetNameByExtension(self, extension):
78 # Get the name of the mimetype that a particular extension
79 # belongs to.
80 for current_type in self.__supported_types:
81 current_type_extensions = current_type.GetExtensions()
82
83 for current_extension in current_type_extensions:
84 if (current_extension.lower() == extension.lower()):
85 return current_type.GetName()
86
87 return None
88
89
90
91 def GetChildNodesByTagName(self, node, tag_name):
92 found_children = []
93
94 for child in node.childNodes:
95 if (child.nodeName == tag_name):
96 found_children.append(child)
97
98 return found_children
99
100
101
102
103 def GetChildrenText(self, node, child_tag_name):
104 children_text = []
105
106 children = self.GetChildNodesByTagName(node, child_tag_name)
107
108 for child_node in children:
109 for sub_child in child_node.childNodes:
110 if (sub_child.nodeType == Node.TEXT_NODE):
111 children_text.append(sub_child.nodeValue)
112 break
113
114
115 return children_text
116
117
118
119