Imported Upstream version 3.2.2
[debian/gnuradio] / grc / base / 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 . import odict
22
23 class XMLSyntaxError(Exception):
24         def __init__(self, error_log):
25                 self._error_log = error_log
26         def __str__(self):
27                 return '\n'.join(map(str, self._error_log.filter_from_errors()))
28
29 def validate_dtd(xml_file, dtd_file=None):
30         """
31         Validate an xml file against its dtd.
32         @param xml_file the xml file
33         @param dtd_file the optional dtd file
34         @throws Exception validation fails
35         """
36         #perform parsing, use dtd validation if dtd file is not specified
37         parser = etree.XMLParser(dtd_validation=not dtd_file)
38         xml = etree.parse(xml_file, parser=parser)
39         if parser.error_log: raise XMLSyntaxError(parser.error_log)
40         #perform dtd validation if the dtd file is specified
41         if not dtd_file: return
42         dtd = etree.DTD(dtd_file)
43         if not dtd.validate(xml.getroot()): raise XMLSyntaxError(dtd.error_log)
44
45 def from_file(xml_file):
46         """
47         Create nested data from an xml file using the from xml helper.
48         @param xml_file the xml file path
49         @return the nested data
50         """
51         xml = etree.parse(xml_file).getroot()
52         return _from_file(xml)
53
54 def _from_file(xml):
55         """
56         Recursivly parse the xml tree into nested data format.
57         @param xml the xml tree
58         @return the nested data
59         """
60         tag = xml.tag
61         if not len(xml):
62                 return odict({tag: xml.text or ''}) #store empty tags (text is None) as empty string
63         nested_data = odict()
64         for elem in xml:
65                 key, value = _from_file(elem).items()[0]
66                 if nested_data.has_key(key): nested_data[key].append(value)
67                 else: nested_data[key] = [value]
68         #delistify if the length of values is 1
69         for key, values in nested_data.iteritems():
70                 if len(values) == 1: nested_data[key] = values[0]
71         return odict({tag: nested_data})
72
73 def to_file(nested_data, xml_file):
74         """
75         Write an xml file and use the to xml helper method to load it.
76         @param nested_data the nested data
77         @param xml_file the xml file path
78         """
79         xml = _to_file(nested_data)[0]
80         open(xml_file, 'w').write(etree.tostring(xml, xml_declaration=True, pretty_print=True))
81
82 def _to_file(nested_data):
83         """
84         Recursivly parse the nested data into xml tree format.
85         @param nested_data the nested data
86         @return the xml tree filled with child nodes
87         """
88         nodes = list()
89         for key, values in nested_data.iteritems():
90                 #listify the values if not a list
91                 if not isinstance(values, (list, set, tuple)):
92                         values = [values]
93                 for value in values:
94                         node = etree.Element(key)
95                         if isinstance(value, (str, unicode)): node.text = value
96                         else: node.extend(_to_file(value))
97                         nodes.append(node)
98         return nodes
99
100 if __name__ == '__main__':
101         """Use the main method to test parse xml's functions."""
102         pass