mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 18:49:37 +00:00
e7d71bedf5
Changes to the canonical are only addition of a comment header and path-setting lines: ``` chrome_src = os.path.abspath(os.path.join(script_dir, os.pardir)) SRC_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(chrome_src, 'tools')) ``` Change-Id: I403c1d7dd56cc45fcddd93142e8929e8cd5ae75f Reviewed-on: https://dart-review.googlesource.com/76705 Reviewed-by: Zach Anderson <zra@google.com> Commit-Queue: Alexander Aprelev <aam@google.com>
40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
# Copyright 2014 The Chromium Authors. All rights reserved.
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
# found in the LICENSE file.
|
|
|
|
"""Helper functions useful when writing scripts that are run from GN's
|
|
exec_script function."""
|
|
|
|
class GNException(Exception):
|
|
pass
|
|
|
|
|
|
def ToGNString(value, allow_dicts = True):
|
|
"""Prints the given value to stdout.
|
|
|
|
allow_dicts indicates if this function will allow converting dictionaries
|
|
to GN scopes. This is only possible at the top level, you can't nest a
|
|
GN scope in a list, so this should be set to False for recursive calls."""
|
|
if isinstance(value, str) or isinstance(value, unicode):
|
|
if value.find('\n') >= 0:
|
|
raise GNException("Trying to print a string with a newline in it.")
|
|
return '"' + value.replace('"', '\\"') + '"'
|
|
|
|
if isinstance(value, list):
|
|
return '[ %s ]' % ', '.join(ToGNString(v) for v in value)
|
|
|
|
if isinstance(value, dict):
|
|
if not allow_dicts:
|
|
raise GNException("Attempting to recursively print a dictionary.")
|
|
result = ""
|
|
for key in value:
|
|
if not isinstance(key, str):
|
|
raise GNException("Dictionary key is not a string.")
|
|
result += "%s = %s\n" % (key, ToGNString(value[key], False))
|
|
return result
|
|
|
|
if isinstance(value, int):
|
|
return str(value)
|
|
|
|
raise GNException("Unsupported type %s (value %s) when printing to GN." % (type(value), value))
|