Debian Codegraph @

utils/qmidb/Enums.py

raw
1 # -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 #
3 # This program is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU General Public License as published by the Free Software
5 # Foundation, version 2.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT
8 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10 # details.
11 #
12 # You should have received a copy of the GNU General Public License along with
13 # this program; if not, write to the Free Software Foundation, Inc., 51
14 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 #
16 # Copyright (C) 2011 - 2012 Red Hat, Inc.
17 #
18
19 import utils
20
21 class EnumEntry:
22 def __init__(self, line):
23 parts = line.split('^')
24 if len(parts) < 3:
25 raise Exception("Invalid enum entry line '%s'" % line)
26 self.id = int(parts[0])
27 self.value = int(parts[1], 0)
28 self.name = parts[2].replace('"', '')
29 self.descid = None
30 if len(parts) > 3:
31 self.descid = int(parts[3])
32
33 def emit(self, enum_name):
34 print "\tGOBI_%s_%s\t\t= 0x%08x, /* %s */" % (
35 utils.constname(enum_name),
36 utils.constname(self.name),
37 self.value, self.name)
38
39 class Enum:
40 def __init__(self, line):
41 parts = line.split('^')
42 if len(parts) < 4:
43 raise Exception("Invalid enum line '%s'" % line)
44 self.id = int(parts[0])
45 self.name = parts[1].replace('"', '')
46 self.descid = int(parts[2])
47 self.internal = int(parts[3]) != 0
48 self.values = [] # list of EnumEntry objects
49
50 def add_entry(self, entry):
51 for v in self.values:
52 if entry.value == v.value:
53 raise Exception("Enum %d already has value %d" % (self.id, v.value))
54 self.values.append(entry)
55 self.values.sort(lambda x, y: cmp(x.value, y.value))
56
57 def emit(self):
58 print 'typedef enum { /* %s */ ' % self.name
59 for en in self.values:
60 en.emit(self.name)
61 print "} gobi_%s;\n" % utils.nicename(self.name)
62
63
64 class Enums(utils.DbFile):
65 def __init__(self, path):
66 self.enums = {}
67
68 # parse the enums
69 f = file(path + "Enum.txt")
70 for line in f:
71 try:
72 enum = Enum(line.strip())
73 self.enums[enum.id] = enum
74 except Exception(e):
75 pass
76 f.close()
77
78 # and now the enum entries
79 f = file(path + "EnumEntry.txt")
80 for line in f:
81 try:
82 entry = EnumEntry(line.strip())
83 self.enums[entry.id].add_entry(entry)
84 except Exception(e):
85 pass
86 f.close()
87
88 def emit(self):
89 for e in self.enums:
90 self.enums[e].emit()
91
92 def get_child(self, eid):
93 return self.enums[eid]
94