]>
gitweb.michael.orlitzky.com - dead/pictar.git/blob - Mime/XmlDataParser.py
6d703a6b70811b4728449413cc2323b021c13c71
2 from SupportedType
import SupportedType
3 from xml
.etree
.ElementTree
import ElementTree
7 This class parses all of the information in the config file, and
8 makes it available through various methods.
11 def __init__(self
, xml_file_path
):
13 We build a list of supported mimetypes upon instantiation.
15 self
.__supported
_types
= []
17 xml_doc
= ElementTree()
18 xml_doc
.parse(xml_file_path
)
20 supported_type_elements
= xml_doc
.findall('supported_type')
22 # For each supported_type element, we build a SupportedType
23 # object and add it to our __supported_types list.
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')
32 current_type
= SupportedType(name
= name
, extensions
= extensions
)
34 if (len(descriptions
) > 0):
35 current_type
.description
= descriptions
[0]
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])
42 if (len(template_data
) > 0):
43 current_type
.template_data
= template_data
[0]
45 self
.__supported
_types
.append(current_type
)
51 def GetAllExtensions(self
):
54 for current_type
in self
.__supported
_types
:
55 for extension
in current_type
.extensions
:
56 all_extensions
.append(extension
)
62 def GetTemplateByExtension(self
, extension
):
64 Get the (x)html template which corresponds to a mimetype
65 having the supplied extension.
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()
76 def GetNameByExtension(self
, extension
):
78 Get the name of the mimetype to which a particular extension
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
90 def GetChildrenText(self
, node
, child_tag_name
):
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.
98 children
= node
.findall(child_tag_name
)
100 for child_node
in children
:
101 children_text
.append(child_node
.text
)