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 import sys | |
| 6 | |
| 7 # Helper functions useful when writing scripts that are run from GN's | |
| 8 # exec_script function. | |
| 9 | |
| 10 class Error(Exception): | |
|
Dirk Pranke
2014/04/03 22:01:28
"Error" is kind of a generic concept in python use
| |
| 11 pass | |
| 12 | |
| 13 | |
| 14 def ExceptionAppend(e, msg): | |
|
Dirk Pranke
2014/04/03 22:01:28
I think you only use this in one place in gypi_to_
| |
| 15 """Append a message to the given exception's message.""" | |
| 16 if not e.args: | |
| 17 e.args = (msg,) | |
| 18 elif len(e.args) == 1: | |
| 19 e.args = (str(e.args[0]) + ' ' + msg,) | |
| 20 else: | |
| 21 e.args = (str(e.args[0]) + ' ' + msg,) + e.args[1:] | |
| 22 | |
| 23 | |
| 24 def PrintString(value): | |
| 25 """Writes a python string as a GN string.""" | |
| 26 if not isinstance(value, str): | |
| 27 raise Error("Argument to PrintList is not a Python list.") | |
|
Dirk Pranke
2014/04/03 22:01:28
presumably you mean "Python string" here ...
| |
| 28 if value.find('\n') >= 0: | |
| 29 raise Error("Trying to print a string with a newline in it.") | |
| 30 sys.stdout.write('"' + value.replace('"', '\\"') + '"') | |
|
Dirk Pranke
2014/04/03 22:01:28
I would probably inline these functions into Print
| |
| 31 | |
| 32 | |
| 33 def PrintList(value): | |
| 34 """Writes a python list as a GN list.""" | |
| 35 if not isinstance(value, list): | |
| 36 raise Error("Argument to PrintList is not a Python list.") | |
| 37 sys.stdout.write('[ ') | |
| 38 for i in value: | |
| 39 PrintValue(i, False) | |
| 40 sys.stdout.write(', ') | |
| 41 sys.stdout.write(']') | |
|
Dirk Pranke
2014/04/03 22:01:28
assuming you change this to just return a string,
| |
| 42 | |
| 43 | |
| 44 def PrintDict(value): | |
| 45 """Writes a python dictionary as a GN scope.""" | |
| 46 for key in value: | |
| 47 if not isinstance(key, str): | |
| 48 raise Error("Dictionary key is not a string.") | |
| 49 sys.stdout.write(key + " = ") | |
| 50 PrintValue(value[key], False) # No recursive dictionaries. | |
| 51 sys.stdout.write('\n') | |
|
Dirk Pranke
2014/04/03 22:01:28
similarly this can be:
return '\n'.join(("%s = %s
| |
| 52 | |
| 53 | |
| 54 def PrintValue(value, allow_dicts = True): | |
| 55 """Prints the given value to stdout. | |
|
Dirk Pranke
2014/04/03 22:01:28
I would probably separate the string conversion ou
| |
| 56 | |
| 57 allow_dicts indicates if this function will allow converting dictionaries | |
| 58 to GN scopes. This is only possible at the top level, you can't nest a | |
| 59 GN scope in a list, so this should be set to False for recursive calls.""" | |
| 60 if isinstance(value, str): | |
| 61 PrintString(value) | |
| 62 elif isinstance(value, list): | |
|
Dirk Pranke
2014/04/03 22:01:28
chromium python style prefers early exits where po
| |
| 63 PrintList(value) | |
| 64 elif isinstance(value, dict): | |
| 65 if not allow_dicts: | |
| 66 raise Error("Attempting to recursively print a dictionary.") | |
| 67 PrintDict(value) | |
| 68 elif isinstance(value, int): | |
| 69 sys.stdout.write(str(value)) | |
| 70 else: | |
| 71 raise Error("Unsupported type when printing to GN.") | |
| 72 | |
| 73 | |
| OLD | NEW |