| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 """Module handling flags while doing stream parsing. | 5 """Module handling flags while doing stream parsing. |
| 6 | 6 |
| 7 ``ParsingFlag`` has a name, a turn_on_condition function which returns a boolean | 7 ``ParsingFlag`` has a name, a turn_on_condition function which returns a boolean |
| 8 predicate and a boolean value. We say that a flag with the boolean value | 8 predicate and a boolean value. We say that a flag with the boolean value |
| 9 ``True`` is "on", and a flag with the boolean value ``False`` is "off". | 9 ``True`` is "on", and a flag with the boolean value ``False`` is "off". |
| 10 The turn_on_condition function is used to check whether a flag should be turned | 10 The turn_on_condition function is used to check whether a flag should be turned |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 Args: | 50 Args: |
| 51 name (str): Name of the flag. | 51 name (str): Name of the flag. |
| 52 turn_on_condition (callable): The function takes a str line as input and | 52 turn_on_condition (callable): The function takes a str line as input and |
| 53 returns a boolean value. When the flag is "off", this funtion can be | 53 returns a boolean value. When the flag is "off", this funtion can be |
| 54 called to check whether the flag should be turned on or not. | 54 called to check whether the flag should be turned on or not. |
| 55 Note, if the flag is "on", the return value of this function means "do | 55 Note, if the flag is "on", the return value of this function means "do |
| 56 nothing" and shouldn't affect the value of the flag. | 56 nothing" and shouldn't affect the value of the flag. |
| 57 value (bool): Initial value of the flag. | 57 value (bool): Initial value of the flag. |
| 58 """ | 58 """ |
| 59 self._name = name | 59 self._name = name |
| 60 self._turn_on_condition = turn_on_condition | 60 self._turn_on_condition = turn_on_condition or (lambda _: False) |
| 61 self._value = value | 61 self._value = value |
| 62 | 62 |
| 63 @property | 63 @property |
| 64 def name(self): | 64 def name(self): |
| 65 return self._name | 65 return self._name |
| 66 | 66 |
| 67 @property | 67 @property |
| 68 def value(self): | 68 def value(self): |
| 69 return self._value | 69 return self._value |
| 70 | 70 |
| 71 def TurnOn(self): | 71 def TurnOn(self): |
| 72 self._value = True | 72 self._value = True |
| 73 | 73 |
| 74 def TurnOff(self): | 74 def TurnOff(self): |
| 75 self._value = False | 75 self._value = False |
| 76 | 76 |
| 77 def __nonzero__(self): | 77 def __nonzero__(self): |
| 78 return self._value | 78 return self._value |
| 79 | 79 |
| 80 __bool__ = __nonzero__ | 80 __bool__ = __nonzero__ |
| 81 | 81 |
| 82 def ConditionallyTurnOn(self, line): | 82 def ConditionallyTurnOn(self, line): |
| 83 """When the flag is off, turns on it if turn_on_conditions met.""" | 83 """When the flag is off, turns on it if turn_on_conditions met.""" |
| 84 if (not self._value and self._turn_on_condition and | 84 if not self._value and self._turn_on_condition(line): |
| 85 self._turn_on_condition(line)): | |
| 86 self.TurnOn() | 85 self.TurnOn() |
| 87 | 86 |
| 88 | 87 |
| 89 class FlagManager(object): | 88 class FlagManager(object): |
| 90 """A manager to track all the registered flags. | 89 """A manager to track all the registered flags. |
| 91 | 90 |
| 92 FlagManager collects and manages flags based on group names. FlagManager takes | 91 FlagManager collects and manages flags based on group names. FlagManager takes |
| 93 care of manipulating flags during the stream parsing, including evaluating | 92 care of manipulating flags during the stream parsing, including evaluating |
| 94 based on the turn_on_conditions of flags, and resetting flags. | 93 based on the turn_on_conditions of flags, and resetting flags. |
| 95 | 94 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 153 |
| 155 flag.TurnOn() | 154 flag.TurnOn() |
| 156 | 155 |
| 157 def TurnOff(self, flag_name): | 156 def TurnOff(self, flag_name): |
| 158 """Sets the instance with flag_name to False.""" | 157 """Sets the instance with flag_name to False.""" |
| 159 flag = self.flags.get(flag_name) | 158 flag = self.flags.get(flag_name) |
| 160 if flag is None: | 159 if flag is None: |
| 161 return | 160 return |
| 162 | 161 |
| 163 flag.TurnOff() | 162 flag.TurnOff() |
| OLD | NEW |