Debian Codegraph @

build-aux/qmi-codegen/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 Lesser General Public License as published by the Free
5 # Software Foundation; either version 2 of the License, or (at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 #
17 # Copyright (C) 2012 Lanedo GmbH
18 # Copyright (C) 2012-2022 Aleksander Morgado <[email protected]>
19 #
20
21 import string
22 import re
23
24 """
25 Add the common copyright header to the given file
26 """
27 def add_copyright(f):
28 f.write(
29 "\n"
30 "/* GENERATED CODE... DO NOT EDIT */\n"
31 "\n"
32 "/*\n"
33 " * This library is free software; you can redistribute it and/or\n"
34 " * modify it under the terms of the GNU Lesser General Public\n"
35 " * License as published by the Free Software Foundation; either\n"
36 " * version 2 of the License, or (at your option) any later version.\n"
37 " *\n"
38 " * This library is distributed in the hope that it will be useful,\n"
39 " * but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
40 " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
41 " * Lesser General Public License for more details.\n"
42 " *\n"
43 " * You should have received a copy of the GNU Lesser General Public\n"
44 " * License along with this library; if not, write to the\n"
45 " * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,\n"
46 " * Boston, MA 02110-1301 USA.\n"
47 " *\n"
48 " * Copyright (C) 2012 Lanedo GmbH\n"
49 " * Copyright (C) 2012-2022 Aleksander Morgado <[email protected]>\n"
50 " */\n"
51 "\n");
52
53
54 """
55 Build a header guard string based on the given filename
56 """
57 def build_header_guard(output_name):
58 return "__LIBQMI_GLIB_" + output_name.replace('-', '_').upper() + "__"
59
60 """
61 Write the common header start chunk
62 """
63 def add_header_start(f, output_name, service):
64 translations = { 'guard' : build_header_guard(output_name),
65 'service' : build_underscore_name(service) }
66 template = (
67 "\n"
68 "#include <glib.h>\n"
69 "#include <glib-object.h>\n"
70 "#include <gio/gio.h>\n"
71 "\n"
72 "#include \"qmi-enums.h\"\n")
73 # CTL, DPM, GMS and ATR don't have enums
74 if service not in ('CTL', 'DPM', 'GMS', 'ATR'):
75 template += (
76 "#include \"qmi-enums-${service}.h\"\n")
77 if service == 'CTL':
78 template += (
79 "#include \"qmi-enums-private.h\"\n")
80 # DMS, NAS, LOC, DSD and WDS have flags64
81 if service in ('DMS', 'NAS', 'LOC', 'DSD', 'WDS'):
82 template += (
83 "#include \"qmi-flags64-${service}.h\"\n")
84 template += (
85 "#include \"qmi-message.h\"\n"
86 "#include \"qmi-client.h\"\n"
87 "\n"
88 "#ifndef ${guard}\n"
89 "#define ${guard}\n"
90 "\n"
91 "G_BEGIN_DECLS\n"
92 "\n")
93 f.write(string.Template(template).substitute(translations))
94
95
96 """
97 Write the common header stop chunk
98 """
99 def add_header_stop(f, output_name):
100 template = string.Template (
101 "\n"
102 "G_END_DECLS\n"
103 "\n"
104 "#endif /* ${guard} */\n")
105 f.write(template.substitute(guard = build_header_guard(output_name)))
106
107
108 """
109 Write the common source file start chunk
110 """
111 def add_source_start(f, output_name):
112 template = string.Template (
113 "\n"
114 "#include <string.h>\n"
115 "\n"
116 "#include \"${name}.h\"\n"
117 "#include \"qmi-common.h\"\n"
118 "#include \"qmi-enum-types.h\"\n"
119 "#include \"qmi-flag-types.h\"\n"
120 "#include \"qmi-enum-types-private.h\"\n"
121 "#include \"qmi-flag-types-private.h\"\n"
122 "#include \"qmi-flags64-types.h\"\n"
123 "#include \"qmi-error-types.h\"\n"
124 "#include \"qmi-device.h\"\n"
125 "#include \"qmi-helpers.h\"\n"
126 '\n'
127 '#define QMI_STATUS_SUCCESS 0x0000\n'
128 '#define QMI_STATUS_FAILURE 0x0001\n'
129 "\n")
130 f.write(template.substitute(name = output_name))
131
132
133 """
134 Write a separator comment in the file
135 """
136 def add_separator(f, separator_type, separator_name):
137 template = string.Template (
138 "\n"
139 "/*****************************************************************************/\n"
140 "/* ${type}: ${name} */\n"
141 "\n")
142 f.write(template.substitute(type = separator_type,
143 name = separator_name))
144
145
146 """
147 Build an underscore name from the given full name
148 e.g.: "This is a message" --> "this_is_a_message"
149 """
150 def build_underscore_name(name):
151 return name.replace(' ', '_').lower()
152
153
154 """
155 Build an underscore uppercase name from the given full name
156 e.g.: "This is a message" --> "THIS_IS_A_MESSAGE"
157 """
158 def build_underscore_uppercase_name(name):
159 return name.replace(' ', '_').upper()
160
161
162 """
163 Build an underscore name from the given camelcase name
164 e.g.: "ThisIsAMessage" --> "this_is_a_message"
165 """
166 def build_underscore_name_from_camelcase(camelcase):
167 s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', camelcase)
168 s2 = re.sub('(.)([0-9][a-z]+)', r'\1_\2', s1)
169 return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s2).lower()
170
171
172 """
173 Build a camelcase name from the given full name
174 e.g.: "This is a message" --> "ThisIsAMessage"
175 """
176 def build_camelcase_name(name):
177 return string.capwords(name).replace(' ', '')
178
179
180 """
181 Build a dashed lowercase name from the given full name
182 e.g.: "This is a message" --> "this-is-a-message"
183 """
184 def build_dashed_name(name):
185 return name.lower().replace(' ', '-')
186
187
188 """
189 Remove the given prefix from the string
190 """
191 def remove_prefix(line, prefix):
192 return line[len(prefix):] if line.startswith(prefix) else line
193
194
195 """
196 Read the contents of the JSON file, skipping lines prefixed with '//', which are
197 considered comments.
198 """
199 def read_json_file(path):
200 f = open(path)
201 out = ''
202 for line in f.readlines():
203 stripped = line.strip()
204 if stripped.startswith('//'):
205 # Skip this line
206 # We add an empty line instead so that errors when parsing the JSON
207 # report the proper line number
208 out += "\n"
209 else:
210 out += line
211 return out
212
213
214 """
215 Returns True if the given format corresponds to a basic unsigned integer type
216 """
217 def format_is_unsigned_integer(fmt):
218 if fmt == 'guint8' or \
219 fmt == 'guint16' or \
220 fmt == 'guint32' or \
221 fmt == 'guint64' or \
222 fmt == 'guint-sized' :
223 return True
224 else:
225 return False
226
227
228 """
229 Returns True if the given format corresponds to a basic signed integer type
230 """
231 def format_is_signed_integer(fmt):
232 if fmt == 'gint8' or \
233 fmt == 'gint16' or \
234 fmt == 'gint32' or \
235 fmt == 'gint64':
236 return True
237 else:
238 return False
239
240 """
241 Returns True if the given format corresponds to a basic floating point type
242 """
243 def format_is_float(fmt):
244 return fmt in ('gfloat', 'gdouble')
245
246
247 """
248 Returns True if the given format corresponds to a basic signed or unsigned
249 integer type
250 """
251 def format_is_integer(fmt):
252 if format_is_unsigned_integer(fmt) or \
253 format_is_signed_integer(fmt):
254 return True
255 else:
256 return False
257
258
259 """
260 Compare two version strings given in MAJOR.MINOR[.MICRO] format.
261 Just to avoid needing to include e.g. packaging.version.parse just for this
262 """
263 def version_compare(v1,v2):
264 v1_split = v1.split(".")
265 v2_split = v2.split(".")
266 major_v1 = int(v1_split[0])
267 major_v2 = int(v2_split[0])
268 if major_v2 > major_v1:
269 return 1
270 if major_v2 < major_v1:
271 return -1
272 # major_v2 == major_v1
273 minor_v1 = int(v1_split[1])
274 minor_v2 = int(v2_split[1])
275 if minor_v2 > minor_v1:
276 return 1
277 if minor_v2 < minor_v1:
278 return -1
279 # minor_v2 == minor_v1
280 micro_v1 = int(v1_split[2]) if len(v1_split) > 2 else 0
281 micro_v2 = int(v2_split[2]) if len(v2_split) > 2 else 0
282 if micro_v2 > micro_v1:
283 return 1
284 if micro_v2 < micro_v1:
285 return -1
286 # micro_v2 == micro_v1
287 return 0