Index: build/gn_helpers.py |
diff --git a/build/gn_helpers.py b/build/gn_helpers.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..cb7531d9dfa119ef5926c239418ffa04c91e14a9 |
--- /dev/null |
+++ b/build/gn_helpers.py |
@@ -0,0 +1,73 @@ |
+# 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. |
+ |
+import sys |
+ |
+# Helper functions useful when writing scripts that are run from GN's |
+# exec_script function. |
+ |
+class Error(Exception): |
Dirk Pranke
2014/04/03 22:01:28
"Error" is kind of a generic concept in python use
|
+ pass |
+ |
+ |
+def ExceptionAppend(e, msg): |
Dirk Pranke
2014/04/03 22:01:28
I think you only use this in one place in gypi_to_
|
+ """Append a message to the given exception's message.""" |
+ if not e.args: |
+ e.args = (msg,) |
+ elif len(e.args) == 1: |
+ e.args = (str(e.args[0]) + ' ' + msg,) |
+ else: |
+ e.args = (str(e.args[0]) + ' ' + msg,) + e.args[1:] |
+ |
+ |
+def PrintString(value): |
+ """Writes a python string as a GN string.""" |
+ if not isinstance(value, str): |
+ raise Error("Argument to PrintList is not a Python list.") |
Dirk Pranke
2014/04/03 22:01:28
presumably you mean "Python string" here ...
|
+ if value.find('\n') >= 0: |
+ raise Error("Trying to print a string with a newline in it.") |
+ sys.stdout.write('"' + value.replace('"', '\\"') + '"') |
Dirk Pranke
2014/04/03 22:01:28
I would probably inline these functions into Print
|
+ |
+ |
+def PrintList(value): |
+ """Writes a python list as a GN list.""" |
+ if not isinstance(value, list): |
+ raise Error("Argument to PrintList is not a Python list.") |
+ sys.stdout.write('[ ') |
+ for i in value: |
+ PrintValue(i, False) |
+ sys.stdout.write(', ') |
+ sys.stdout.write(']') |
Dirk Pranke
2014/04/03 22:01:28
assuming you change this to just return a string,
|
+ |
+ |
+def PrintDict(value): |
+ """Writes a python dictionary as a GN scope.""" |
+ for key in value: |
+ if not isinstance(key, str): |
+ raise Error("Dictionary key is not a string.") |
+ sys.stdout.write(key + " = ") |
+ PrintValue(value[key], False) # No recursive dictionaries. |
+ sys.stdout.write('\n') |
Dirk Pranke
2014/04/03 22:01:28
similarly this can be:
return '\n'.join(("%s = %s
|
+ |
+ |
+def PrintValue(value, allow_dicts = True): |
+ """Prints the given value to stdout. |
Dirk Pranke
2014/04/03 22:01:28
I would probably separate the string conversion ou
|
+ |
+ 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): |
+ PrintString(value) |
+ elif isinstance(value, list): |
Dirk Pranke
2014/04/03 22:01:28
chromium python style prefers early exits where po
|
+ PrintList(value) |
+ elif isinstance(value, dict): |
+ if not allow_dicts: |
+ raise Error("Attempting to recursively print a dictionary.") |
+ PrintDict(value) |
+ elif isinstance(value, int): |
+ sys.stdout.write(str(value)) |
+ else: |
+ raise Error("Unsupported type when printing to GN.") |
+ |
+ |