OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 '''Utilities used by GRIT. | 6 '''Utilities used by GRIT. |
7 ''' | 7 ''' |
8 | 8 |
9 import codecs | 9 import codecs |
10 import htmlentitydefs | 10 import htmlentitydefs |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 def IsVerbose(): | 476 def IsVerbose(): |
477 return verbose | 477 return verbose |
478 | 478 |
479 def IsExtraVerbose(): | 479 def IsExtraVerbose(): |
480 return extra_verbose | 480 return extra_verbose |
481 | 481 |
482 def ParseDefine(define): | 482 def ParseDefine(define): |
483 '''Parses a define argument and returns the name and value. | 483 '''Parses a define argument and returns the name and value. |
484 | 484 |
485 The format is either "NAME=VAL" or "NAME", using True as the default value. | 485 The format is either "NAME=VAL" or "NAME", using True as the default value. |
486 Values of "1" and "0" are transformed to True and False respectively. | 486 Values of "1"/"true" and "0"/"false" are transformed to True and False |
| 487 respectively. |
487 | 488 |
488 Args: | 489 Args: |
489 define: a string of the form "NAME=VAL" or "NAME". | 490 define: a string of the form "NAME=VAL" or "NAME". |
490 | 491 |
491 Returns: | 492 Returns: |
492 A (name, value) pair. name is a string, value a string or boolean. | 493 A (name, value) pair. name is a string, value a string or boolean. |
493 ''' | 494 ''' |
494 parts = [part.strip() for part in define.split('=', 1)] | 495 parts = [part.strip() for part in define.split('=', 1)] |
495 assert len(parts) >= 1 | 496 assert len(parts) >= 1 |
496 name = parts[0] | 497 name = parts[0] |
497 val = True | 498 val = True |
498 if len(parts) > 1: | 499 if len(parts) > 1: |
499 val = parts[1] | 500 val = parts[1] |
500 if val == "1": val = True | 501 if val == "1" or val == "true": val = True |
501 elif val == "0": val = False | 502 elif val == "0" or val == "false": val = False |
502 return (name, val) | 503 return (name, val) |
503 | 504 |
504 | 505 |
505 class Substituter(object): | 506 class Substituter(object): |
506 '''Finds and substitutes variable names in text strings. | 507 '''Finds and substitutes variable names in text strings. |
507 | 508 |
508 Given a dictionary of variable names and values, prepares to | 509 Given a dictionary of variable names and values, prepares to |
509 search for patterns of the form [VAR_NAME] in a text. | 510 search for patterns of the form [VAR_NAME] in a text. |
510 The value will be substituted back efficiently. | 511 The value will be substituted back efficiently. |
511 Also applies to tclib.Message objects. | 512 Also applies to tclib.Message objects. |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 return self._AsCurrentDirClass(self.GetPath()) | 653 return self._AsCurrentDirClass(self.GetPath()) |
653 | 654 |
654 class _AsCurrentDirClass(object): | 655 class _AsCurrentDirClass(object): |
655 def __init__(self, path): | 656 def __init__(self, path): |
656 self.path = path | 657 self.path = path |
657 def __enter__(self): | 658 def __enter__(self): |
658 self.oldpath = os.getcwd() | 659 self.oldpath = os.getcwd() |
659 os.chdir(self.path) | 660 os.chdir(self.path) |
660 def __exit__(self, *exc_info): | 661 def __exit__(self, *exc_info): |
661 os.chdir(self.oldpath) | 662 os.chdir(self.oldpath) |
OLD | NEW |