
Adds support for a new value type, a dictionary. To script it looks the same as a scope, but the existing scope didn't have the right memory management semantics for this. Adds a new input conversion mode to interpret the result of a script as a GN block (basically an eval) and get the result in a dictionary. This allows returning named sets of things from a script. Updates the accessor and defined stuff to work with this new type. Adds some features to the input file manager so that when we do dynamic reading from a script we keep the text of the read around which means we can give proper errors that refer to the script output, and don't have to do the weird recursive set origin thing to clear out the pointers that became invalid when the InputFile went out of scope. This allows us to give better error messages when parsing input. The error handling in the input conversion code is changed accordingly. Checks for unused variables both in code that instantiates a template, and in the template code itself. Adds a shared python script for outputting values from Python to GN. Adds a python script to read .gypi files and return the value as a GN scope. BUG= R=dpranke@chromium.org, scottmg@chromium.org Review URL: https://codereview.chromium.org/223783005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@262045 0039d316-1c4b-4281-b951-d872f2087c98
40 lines
1.3 KiB
Python
40 lines
1.3 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):
|
|
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 when printing to GN.")
|