Debian Codegraph @

utils/qmidb/utils.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 def constname(name):
20 rlist = { '(': '',
21 ')': '',
22 ' ': '_',
23 '/': '_',
24 '.': '',
25 ',': '' }
26 # Capture stuff like "-9 dB" correctly
27 if name.find("dB") == -1:
28 rlist['-'] = '_'
29 else:
30 rlist['-'] = 'NEG_'
31
32 sanitized = ""
33 for c in name:
34 try:
35 sanitized += rlist[c]
36 except KeyError:
37 sanitized += c
38
39 # Handle E_UTRA -> EUTRA and E_UTRAN -> EUTRAN
40 foo = sanitized.upper().strip('_')
41 foo.replace('E_UTRA', 'EUTRA')
42
43 # And HSDPA+ -> HSDPA_PLUS then strip all plusses
44 foo = foo.replace('HSDPA+', 'HSDPA_PLUS')
45 foo = foo.replace('+', '')
46
47 # 1xEV-DO -> 1x EVDO
48 foo = foo.replace("1XEV_DO", "1X_EVDO")
49 foo = foo.replace("EV_DO", "EVDO")
50
51 # Wi-Fi -> WiFi
52 foo = foo.replace("WI_FI", "WIFI")
53
54 # modify certain words
55 words = foo.split('_')
56 klist = [ 'UNSUPPORTED' ]
57 slist = [ 'STATES', 'REASONS', 'FORMATS', 'SELECTIONS', 'TYPES', 'TAGS',
58 'PROTOCOLS', 'CLASSES', 'ACTIONS', 'VALUES', 'OPTIONS',
59 'DOMAINS', 'DEVICES', 'MODES', 'MONTHS', 'PREFERENCES',
60 'PREFS', 'DURATIONS', 'CAPABILITIES', 'INTERFACES',
61 'TECHNOLOGIES', 'NETWORKS', 'DAYS', 'SYSTEMS', 'SCHEMES',
62 'INDICATORS', 'ENCODINGS', 'INITIALS', 'BITS', 'MEDIUMS',
63 'BASES', 'ERRORS', 'RESULTS', 'RATIOS', 'DELIVERIES',
64 'FAMILIES', 'SETTINGS', 'SOURCES', 'ORDERS' ]
65 blist = { 'CLASSES': 'CLASS' }
66 final = ''
67 for word in words:
68 if word in klist or len(word) == 0:
69 continue
70 if len(final):
71 final += '_'
72 if word in blist:
73 final += blist[word]
74 elif word in slist:
75 if word.endswith("IES"):
76 final += word[:len(word) - 3] + "Y"
77 elif word.endswith("S"):
78 final += word[:len(word) - 1]
79 else:
80 final += word
81 return final
82
83 def nicename(name):
84 name = name.lower()
85 name = name.replace("1xev-do", "1x evdo")
86 name = name.replace("ev-do", "evdo")
87 name = name.replace("wi-fi", "wifi")
88 name = name.replace("%", "pct")
89 name = name.replace(' ', '_').replace('/', '_').replace('-', '_').replace('.','').replace('(', '').replace(')', '')
90 name = name.replace("___", "_").replace("__", "_")
91 return name.strip('_')
92
93
94 class DbFile:
95 # Base class for objects that handle reading a database file like
96 # Enum.txt or Struct.txt
97
98 def __init__(self, path):
99 raise Exception("init() method must be implemented")
100
101 def validate(self):
102 pass
103
104 def has_child(self, cid):
105 raise Exception("has_child() method must be implemented")
106
107 def get_child(self, cid):
108 raise Exception("get_child() method must be implemented")
109
110 def emit(self):
111 pass
112
113 def emit_unused(self, used, fields, enums):
114 pass
115