Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / src / utils / ParseXML.py
1 """
2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18 """
19
20 from lxml import etree
21 from .. utils import odict
22
23 XMLSyntaxError = etree.XMLSyntaxError
24
25 def validate_dtd(xml_file, dtd_file=None):
26         """
27         Validate an xml file against its dtd.
28         @param xml_file the xml file
29         @param dtd_file the optional dtd file
30         @throws Exception validation fails
31         """
32         if dtd_file:
33                 dtd = etree.DTD(dtd_file)
34                 xml = etree.parse(xml_file)
35                 if not dtd.validate(xml.getroot()):
36                         raise XMLSyntaxError, '\n'.join(map(str, dtd.error_log.filter_from_errors()))
37         else:
38                 parser = etree.XMLParser(dtd_validation=True)
39                 xml = etree.parse(xml_file, parser=parser)
40                 if parser.error_log:
41                         raise XMLSyntaxError, '\n'.join(map(str, parser.error_log.filter_from_errors()))
42
43 def from_file(xml_file):
44         """
45         Create nested data from an xml file using the from xml helper.
46         @param xml_file the xml file path
47         @return the nested data
48         """
49         xml = etree.parse(xml_file).getroot()
50         return _from_file(xml)
51
52 def _from_file(xml):
53         """
54         Recursivly parse the xml tree into nested data format.
55         @param xml the xml tree
56         @return the nested data
57         """
58         tag = xml.tag
59         if not len(xml):
60                 return odict({tag: xml.text or ''}) #store empty tags (text is None) as empty string
61         nested_data = odict()
62         for elem in xml:
63                 key, value = _from_file(elem).items()[0]
64                 if nested_data.has_key(key): nested_data[key].append(value)
65                 else: nested_data[key] = [value]
66         #delistify if the length of values is 1
67         for key, values in nested_data.iteritems():
68                 if len(values) == 1: nested_data[key] = values[0]
69         return odict({tag: nested_data})
70
71 def to_file(nested_data, xml_file):
72         """
73         Write an xml file and use the to xml helper method to load it.
74         @param nested_data the nested data
75         @param xml_file the xml file path
76         """
77         xml = _to_file(nested_data)[0]
78         open(xml_file, 'w').write(etree.tostring(xml, xml_declaration=True, pretty_print=True))
79
80 def _to_file(nested_data):
81         """
82         Recursivly parse the nested data into xml tree format.
83         @param nested_data the nested data
84         @return the xml tree filled with child nodes
85         """
86         nodes = list()
87         for key, values in nested_data.iteritems():
88                 #listify the values if not a list
89                 if not isinstance(values, (list, set, tuple)):
90                         values = [values]
91                 for value in values:
92                         node = etree.Element(key)
93                         if isinstance(value, (str, unicode)): node.text = value
94                         else: node.extend(_to_file(value))
95                         nodes.append(node)
96         return nodes
97
98 if __name__ == '__main__':
99         """Use the main method to test parse xml's functions."""
100         pass