4"""Helper functions useful when writing scripts that are run from GN's
5exec_script function."""
16_Ord = ord
if sys.version_info.major < 3
else lambda c: c
19def _TranslateToGnChars(s):
20 for decoded_ch
in s.encode(
'utf-8'):
21 code = _Ord(decoded_ch)
22 if code
in (34, 36, 92):
23 yield '\\' + chr(code)
24 elif 32 <= code < 127:
27 yield '$0x%02X' % code
31 """Returns a stringified GN equivalent of a Python value.
33 allow_dicts indicates if this function will allow converting dictionaries
34 to GN scopes. This
is only possible at the top level, you can
't nest a
35 GN scope in a list, so this should be set to
False for recursive calls.
"""
36 if isinstance(value, str)
or isinstance(value, unicode):
37 if value.find(
'\n') >= 0:
38 raise GNException(
"Trying to print a string with a newline in it.")
39 return '"' +
''.
join(_TranslateToGnChars(value)) +
'"'
41 if isinstance(value, list):
44 if isinstance(value, dict):
46 raise GNException(
"Attempting to recursively print a dictionary.")
49 if not isinstance(key, str):
50 raise GNException(
"Dictionary key is not a string.")
51 result +=
"%s = %s\n" % (key,
ToGNString(value[key],
False))
54 if isinstance(value, int):
57 raise GNException(
"Unsupported type %s (value %s) when printing to GN." %
def ToGNString(value, allow_dicts=True)
static SkString join(const CommandLineFlags::StringArray &)