| 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 |
| 10 To use in a random python file in the build: |
| 11 |
| 12 import os |
| 13 import sys |
| 14 |
| 15 sys.path.append(os.path.join(os.path.dirname(__file__), |
| 16 os.pardir, os.pardir, "build")) |
| 17 import gn_helpers |
| 18 |
| 19 Where the sequence of parameters to join is the relative path from your source |
| 20 file to the build directory.""" |
| 9 | 21 |
| 10 class GNException(Exception): | 22 class GNException(Exception): |
| 11 pass | 23 pass |
| 12 | 24 |
| 13 | 25 |
| 14 def ToGNString(value, allow_dicts = True): | 26 def ToGNString(value, allow_dicts = True): |
| 15 """Prints the given value to stdout. | 27 """Prints the given value to stdout. |
| 16 | 28 |
| 17 allow_dicts indicates if this function will allow converting dictionaries | 29 allow_dicts indicates if this function will allow converting dictionaries |
| 18 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 |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 location in the input. If it does, the text is consumed and the function | 279 location in the input. If it does, the text is consumed and the function |
| 268 returns true. Otherwise, returns false and the current position is | 280 returns true. Otherwise, returns false and the current position is |
| 269 unchanged.""" | 281 unchanged.""" |
| 270 end = self.cur + len(constant) | 282 end = self.cur + len(constant) |
| 271 if end >= len(self.input): | 283 if end >= len(self.input): |
| 272 return False # Not enough room. | 284 return False # Not enough room. |
| 273 if self.input[self.cur:end] == constant: | 285 if self.input[self.cur:end] == constant: |
| 274 self.cur = end | 286 self.cur = end |
| 275 return True | 287 return True |
| 276 return False | 288 return False |
| OLD | NEW |