Skip to content

Commit 7ff705f

Browse files
committed
Py2/3-safe checkspecs
1 parent 7e9de0d commit 7ff705f

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

tools/checkspecs.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ def _parse_lines(self, linesource, module):
155155
classes.sort()
156156
return functions, classes
157157

158+
@classmethod
159+
def _normalize_repr(cls, value):
160+
if isinstance(value, list):
161+
return '[{}]'.format(', '.join(map(cls._normalize_repr, value)))
162+
if isinstance(value, tuple):
163+
if len(value) == 1:
164+
return '({},)'.format(cls._normalize_repr(value[0]))
165+
return '({})'.format(', '.join(map(cls._normalize_repr, value)))
166+
if isinstance(value, (str, bytes)):
167+
value = repr(value)
168+
if value[0] not in ('"', "'"):
169+
value = value[1:]
170+
else:
171+
value = repr(value)
172+
return value
173+
158174
def test_specs(self, uri):
159175
"""Check input and output specs in an uri
160176
@@ -206,6 +222,7 @@ def test_specs(self, uri):
206222
if not os.path.exists(nonautotest):
207223
with open(testfile, 'wt') as fp:
208224
cmd = ['# AUTO-GENERATED by tools/checkspecs.py - DO NOT EDIT',
225+
'from __future__ import unicode_literals',
209226
'from ..%s import %s' % (uri.split('.')[-1], c),
210227
'']
211228
cmd.append('\ndef test_%s_inputs():' % c)
@@ -215,14 +232,7 @@ def test_specs(self, uri):
215232
for key, value in sorted(trait.__dict__.items()):
216233
if key in in_built or key == 'desc':
217234
continue
218-
if isinstance(value, (str, bytes)):
219-
quote = "'"
220-
if "'" in value:
221-
quote = '"'
222-
input_fields += "%s=%s%s%s,\n " % (key, quote,
223-
value, quote)
224-
else:
225-
input_fields += "%s=%s,\n " % (key, value)
235+
input_fields += "%s=%s,\n " % (key, self._normalize_repr(value))
226236
input_fields += '),\n '
227237
cmd += [' input_map = dict(%s)' % input_fields]
228238
cmd += [' inputs = %s.input_spec()' % c]
@@ -259,14 +269,7 @@ def test_specs(self, uri):
259269
for key, value in sorted(trait.__dict__.items()):
260270
if key in in_built or key == 'desc':
261271
continue
262-
if isinstance(value, (str, bytes)):
263-
quote = "'"
264-
if "'" in value:
265-
quote = '"'
266-
input_fields += "%s=%s%s%s,\n " % (key, quote,
267-
value, quote)
268-
else:
269-
input_fields += "%s=%s,\n " % (key, value)
272+
input_fields += "%s=%s,\n " % (key, self._normalize_repr(value))
270273
input_fields += '),\n '
271274
cmd += [' output_map = dict(%s)' % input_fields]
272275
cmd += [' outputs = %s.output_spec()' % c]

0 commit comments

Comments
 (0)