~ubuntu-branches/ubuntu/maverick/avr-libc/maverick

« back to all changes in this revision

Viewing changes to xml/Descparser.py

  • Committer: Bazaar Package Importer
  • Author(s): Hakan Ardo
  • Date: 2009-10-31 11:52:10 UTC
  • mfrom: (1.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20091031115210-crjd42sn6ezrj52c
* New upstream relese (closes: #544030)
* Added lintian overrides (closes: #553265)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/env python
2
 
#
3
 
# Copyright (c) 2004  Theodore A. Roth
4
 
# All rights reserved.
5
 
#
6
 
# Redistribution and use in source and binary forms, with or without
7
 
# modification, are permitted provided that the following conditions are met:
8
 
#
9
 
# * Redistributions of source code must retain the above copyright
10
 
#   notice, this list of conditions and the following disclaimer.
11
 
#
12
 
# * Redistributions in binary form must reproduce the above copyright
13
 
#   notice, this list of conditions and the following disclaimer in
14
 
#   the documentation and/or other materials provided with the
15
 
#   distribution.
16
 
#
17
 
# * Neither the name of the copyright holders nor the names of
18
 
#   contributors may be used to endorse or promote products derived
19
 
#   from this software without specific prior written permission.
20
 
#
21
 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25
 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26
 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
 
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
 
# POSSIBILITY OF SUCH DAMAGE.
32
 
#
33
 
# $Id: Descparser.py,v 1.1 2005/10/21 21:00:01 joerg_wunsch Exp $
34
 
#
35
 
 
36
 
import os, sys, types
37
 
 
38
 
from xml.sax import make_parser
39
 
from xml.sax.handler import ContentHandler
40
 
 
41
 
class XML_Mixin:
42
 
    children = {}
43
 
    def __init__ (self, attrs):
44
 
        self.xml_members = []
45
 
        for name in attrs.getNames ():
46
 
            if attrs.getType (name) == 'CDATA':
47
 
                self.xml_members.append (name)
48
 
                setattr (self, name, attrs.getValue (name))
49
 
        self.data = ''
50
 
 
51
 
    def create_child (self, name, attrs):
52
 
        return self.children[name] (attrs)
53
 
 
54
 
    def add_child (self, name, obj):
55
 
        self.xml_members.append (name)
56
 
        setattr (self, name, obj)
57
 
 
58
 
    def characters (self, ch):
59
 
        self.data += ch
60
 
 
61
 
    def __str__ (self):
62
 
        s = '[ data = "%s"; ' % (self.data)
63
 
        for member in self.xml_members:
64
 
            s += '%s = %s; ' % (member, str (getattr (self, member)))
65
 
        s += ']\n'
66
 
        return s
67
 
 
68
 
class PCData (XML_Mixin):
69
 
    def __repr__ (self):
70
 
        return '"%s"' % (self.data)
71
 
 
72
 
class MemorySizes (XML_Mixin):
73
 
    children = {
74
 
        'flash_size': PCData,
75
 
        'eeprom_size': PCData,
76
 
        'int_sram_size': PCData,
77
 
        'ext_sram_size': PCData
78
 
        }
79
 
 
80
 
class Vector (XML_Mixin):
81
 
    children = {
82
 
        'description': PCData,
83
 
        'sig_name': PCData,
84
 
        'alt_name': PCData
85
 
        }
86
 
    def add_child (self, name, obj):
87
 
        if name == "alt_name":
88
 
            try:
89
 
                x = getattr (self, name)
90
 
                x.data.append(obj.data)
91
 
                setattr (self, name, x)
92
 
            except AttributeError:
93
 
                x = obj.data
94
 
                obj.data = [x]
95
 
                self.xml_members.append (name)
96
 
                setattr (self, name, obj)
97
 
        else:
98
 
            self.xml_members.append (name)
99
 
            setattr (self, name, obj)
100
 
 
101
 
 
102
 
class Interrupts (dict, XML_Mixin):
103
 
    children = {
104
 
        'vector': Vector
105
 
        }
106
 
 
107
 
    def __init__ (self, attrs):
108
 
        dict.__init__ (self)
109
 
        XML_Mixin.__init__ (self, attrs)
110
 
 
111
 
    def add_child (self, name, obj):
112
 
        # Use the Vector 'num' attr as the key for the child object.
113
 
        if self.has_key (obj.num):
114
 
            raise 'Duplicate vector entry', obj
115
 
        self[obj.num] = obj
116
 
 
117
 
class BitField (XML_Mixin):
118
 
    children = {
119
 
        'description': PCData,
120
 
        'alt_name': PCData
121
 
        }
122
 
 
123
 
class IORegister (XML_Mixin):
124
 
    children = {
125
 
        'description': PCData,
126
 
        'alt_name': PCData,
127
 
        'bit_field': BitField,
128
 
        }
129
 
 
130
 
    def add_child (self, name, obj):
131
 
        if name == 'bit_field':
132
 
            if not hasattr (self, name):
133
 
                setattr (self, name, {})
134
 
            # Use the BitField 'bit' attr as the key for the child object.
135
 
            self.bit_field[obj.bit] = obj
136
 
        else:
137
 
            XML_Mixin.add_child (self, name, obj)
138
 
 
139
 
class IORegisterDict (dict, XML_Mixin):
140
 
    children = {
141
 
        'ioreg': IORegister
142
 
        }
143
 
 
144
 
    def __init__ (self, attrs):
145
 
        dict.__init__ (self)
146
 
        XML_Mixin.__init__ (self, attrs)
147
 
 
148
 
    def add_child (self, name, obj):
149
 
        # Use the IORegister 'name' attr as the key for the child object.
150
 
        if self.has_key (obj.name):
151
 
            raise 'Duplicate io register entry', obj
152
 
        self[obj.name] = obj
153
 
 
154
 
class BootMode (XML_Mixin):
155
 
    pass
156
 
 
157
 
class BootLoader (dict, XML_Mixin):
158
 
    children = {
159
 
        'mode': BootMode
160
 
        }
161
 
 
162
 
    def __init__ (self, attrs):
163
 
        dict.__init__ (self)
164
 
        XML_Mixin.__init__ (self, attrs)
165
 
 
166
 
    def add_child (self, name, obj):
167
 
        # Use the BootMode 'num' attr as the key for the child object.
168
 
        if self.has_key (obj.num):
169
 
            raise 'Duplicate boot mode entry', obj
170
 
 
171
 
        self[obj.num] = obj
172
 
 
173
 
class Device (XML_Mixin):
174
 
    children = {
175
 
        'version': PCData,
176
 
        'description': PCData,
177
 
        'memory_sizes': MemorySizes,
178
 
        'interrupts': Interrupts,
179
 
        'ioregisters': IORegisterDict,
180
 
        'bootloader': BootLoader
181
 
        }
182
 
 
183
 
class DescHandler (ContentHandler):
184
 
    """Content Handler for the device description xml files.
185
 
    """
186
 
 
187
 
    def __init__ (self):
188
 
        self.depth = 0
189
 
        self.stack = []
190
 
 
191
 
    def push (self, name, obj):
192
 
        self.stack.append ([name, obj])
193
 
 
194
 
    def pop (self):
195
 
        return self.stack.pop ()
196
 
 
197
 
    def get_curr_obj (self):
198
 
        return self.stack[-1][1]
199
 
 
200
 
    def startElement (self, name, attrs):
201
 
        if self.depth == 0:
202
 
            obj = Device (attrs)
203
 
            self.dev = obj
204
 
        else:
205
 
            # Look up the elements class in the parent's children dict.
206
 
            parent = self.get_curr_obj ()
207
 
            obj = parent.create_child (name, attrs)
208
 
 
209
 
        self.push (name, obj)
210
 
        self.depth += 1
211
 
 
212
 
    def endElement (self, name):
213
 
        self.depth -= 1
214
 
 
215
 
        # Pull the current element off the stack and add it to the parent.
216
 
        (cname, cobj) = self.pop ()
217
 
 
218
 
        cobj.data = ' '.join (cobj.data.split ())
219
 
 
220
 
        try:
221
 
            parent = self.get_curr_obj ()
222
 
            parent.add_child (cname, cobj)
223
 
        except IndexError:
224
 
            # We're back at the root element, so we're done.
225
 
            pass
226
 
 
227
 
        #print '%s%s %s' % ('  '*self.depth, cname, cobj)
228
 
 
229
 
    def characters (self, ch):
230
 
        self.get_curr_obj().characters (ch)
231
 
 
232
 
    def endDocument (self):
233
 
        #print self.dev
234
 
        pass
235
 
 
236
 
if __name__ == '__main__':
237
 
    parser = make_parser ()
238
 
 
239
 
    handler = DescHandler ()
240
 
    parser.setContentHandler (handler)
241
 
 
242
 
    if len(sys.argv) > 1:
243
 
        parser.parse (open (sys.argv[1]))
244
 
    else:
245
 
        parser.parse (open ('desc-90s1200.xml'))
246
 
 
247
 
    l = parser.getContentHandler().dev.interrupts.keys()
248
 
    l.sort(lambda x, y: cmp(int(x), int(y)))
249
 
    for key in l:
250
 
        ele = parser.getContentHandler().dev.interrupts[key]
251
 
        print "/* " + ele.description.data + " */"
252
 
        print "#define " + ele.sig_name.data + "\t_VECTOR(" + key + ")"
253
 
        for x in ele.alt_name.data:
254
 
            print "#define " + x + "\t_VECTOR(" + key + ")"
255
 
        print ""