Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Helper functions useful when writing scripts that integrate with GN. | 5 """Helper functions useful when writing scripts that integrate with GN. |
| 6 | 6 |
| 7 The main functions are ToGNString and FromGNString which convert between | 7 The main functions are ToGNString and FromGNString which convert between |
| 8 serialized GN veriables and Python variables. | 8 serialized GN veriables and Python variables. |
| 9 | 9 |
| 10 To use in a random python file in the build: | 10 To use in a random python file in the build: |
| 11 | 11 |
| 12 import os | 12 import os |
| 13 import sys | 13 import sys |
| 14 | 14 |
| 15 sys.path.append(os.path.join(os.path.dirname(__file__), | 15 sys.path.append(os.path.join(os.path.dirname(__file__), |
| 16 os.pardir, os.pardir, "build")) | 16 os.pardir, os.pardir, "build")) |
| 17 import gn_helpers | 17 import gn_helpers |
| 18 | 18 |
| 19 Where the sequence of parameters to join is the relative path from your source | 19 Where the sequence of parameters to join is the relative path from your source |
| 20 file to the build directory.""" | 20 file to the build directory.""" |
| 21 | 21 |
| 22 class GNException(Exception): | 22 class GNException(Exception): |
| 23 pass | 23 pass |
| 24 | 24 |
| 25 | 25 |
| 26 def ToGNString(value, allow_dicts = True): | 26 def ToGNString(value, allow_dicts = True): |
| 27 """Prints the given value to stdout. | 27 """Prints the given value to stdout. |
|
brettw
2016/04/12 21:46:03
While you're here can you fix this line? It return
Dirk Pranke
2016/04/12 21:47:52
Sure.
| |
| 28 | 28 |
| 29 allow_dicts indicates if this function will allow converting dictionaries | 29 allow_dicts indicates if this function will allow converting dictionaries |
| 30 to GN scopes. This is only possible at the top level, you can't nest a | 30 to GN scopes. This is only possible at the top level, you can't nest a |
| 31 GN scope in a list, so this should be set to False for recursive calls.""" | 31 GN scope in a list, so this should be set to False for recursive calls.""" |
| 32 if isinstance(value, str): | 32 if isinstance(value, str): |
| 33 if value.find('\n') >= 0: | 33 if value.find('\n') >= 0: |
| 34 raise GNException("Trying to print a string with a newline in it.") | 34 raise GNException("Trying to print a string with a newline in it.") |
| 35 return '"' + \ | 35 return '"' + \ |
| 36 value.replace('\\', '\\\\').replace('"', '\\"').replace('$', '\\$') + \ | 36 value.replace('\\', '\\\\').replace('"', '\\"').replace('$', '\\$') + \ |
| 37 '"' | 37 '"' |
| 38 | 38 |
| 39 if isinstance(value, unicode): | 39 if isinstance(value, unicode): |
| 40 return ToGNString(value.encode('utf-8')) | 40 return ToGNString(value.encode('utf-8')) |
| 41 | 41 |
| 42 if isinstance(value, bool): | 42 if isinstance(value, bool): |
| 43 if value: | 43 if value: |
| 44 return "true" | 44 return "true" |
| 45 return "false" | 45 return "false" |
| 46 | 46 |
| 47 if isinstance(value, list): | 47 if isinstance(value, list): |
| 48 return '[ %s ]' % ', '.join(ToGNString(v) for v in value) | 48 return '[ %s ]' % ', '.join(ToGNString(v) for v in value) |
| 49 | 49 |
| 50 if isinstance(value, dict): | 50 if isinstance(value, dict): |
| 51 if not allow_dicts: | 51 if not allow_dicts: |
| 52 raise GNException("Attempting to recursively print a dictionary.") | 52 raise GNException("Attempting to recursively print a dictionary.") |
| 53 result = "" | 53 result = "" |
| 54 for key in value: | 54 for key in sorted(value): |
|
Dirk Pranke
2016/04/12 18:06:49
this makes things a little more deterministic and
| |
| 55 if not isinstance(key, str): | 55 if not isinstance(key, str): |
| 56 raise GNException("Dictionary key is not a string.") | 56 raise GNException("Dictionary key is not a string.") |
| 57 result += "%s = %s\n" % (key, ToGNString(value[key], False)) | 57 result += "%s = %s\n" % (key, ToGNString(value[key], False)) |
| 58 return result | 58 return result |
| 59 | 59 |
| 60 if isinstance(value, int): | 60 if isinstance(value, int): |
| 61 return str(value) | 61 return str(value) |
| 62 | 62 |
| 63 raise GNException("Unsupported type when printing to GN.") | 63 raise GNException("Unsupported type when printing to GN.") |
| 64 | 64 |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 342 location in the input. If it does, the text is consumed and the function | 342 location in the input. If it does, the text is consumed and the function |
| 343 returns true. Otherwise, returns false and the current position is | 343 returns true. Otherwise, returns false and the current position is |
| 344 unchanged.""" | 344 unchanged.""" |
| 345 end = self.cur + len(constant) | 345 end = self.cur + len(constant) |
| 346 if end > len(self.input): | 346 if end > len(self.input): |
| 347 return False # Not enough room. | 347 return False # Not enough room. |
| 348 if self.input[self.cur:end] == constant: | 348 if self.input[self.cur:end] == constant: |
| 349 self.cur = end | 349 self.cur = end |
| 350 return True | 350 return True |
| 351 return False | 351 return False |
| OLD | NEW |