Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Helper functions useful when writing scripts that are run from GN's | |
| 6 exec_script function.""" | |
| 7 import sys | |
|
Dirk Pranke
2014/04/04 23:01:11
nit: I don't think you need this import any more.
| |
| 8 | |
| 9 | |
| 10 class GNException(Exception): | |
| 11 pass | |
| 12 | |
| 13 | |
| 14 def PrintDict(value): | |
| 15 """Writes a python dictionary as a GN scope.""" | |
|
Dirk Pranke
2014/04/04 23:01:11
nit: delete this function (no longer needed)?
| |
| 16 | |
| 17 | |
| 18 def ToGNString(value, allow_dicts = True): | |
| 19 """Prints the given value to stdout. | |
| 20 | |
| 21 allow_dicts indicates if this function will allow converting dictionaries | |
| 22 to GN scopes. This is only possible at the top level, you can't nest a | |
| 23 GN scope in a list, so this should be set to False for recursive calls.""" | |
| 24 if isinstance(value, str): | |
| 25 if value.find('\n') >= 0: | |
| 26 raise GNException("Trying to print a string with a newline in it.") | |
| 27 return '"' + value.replace('"', '\\"') + '"' | |
| 28 | |
| 29 if isinstance(value, list): | |
| 30 return '[ %s ]' % ', '.join(ToGNString(v) for v in value) | |
| 31 | |
| 32 if isinstance(value, dict): | |
| 33 if not allow_dicts: | |
| 34 raise GNException("Attempting to recursively print a dictionary.") | |
| 35 result = "" | |
| 36 for key in value: | |
| 37 if not isinstance(key, str): | |
| 38 raise GNException("Dictionary key is not a string.") | |
| 39 result += "%s = %s\n" % (key, ToGNString(value[key], False)) | |
| 40 return result | |
| 41 | |
| 42 if isinstance(value, int): | |
| 43 return str(value) | |
| 44 | |
| 45 raise GNException("Unsupported type when printing to GN.") | |
| 46 | |
| 47 | |
| OLD | NEW |