Chromium Code Reviews| Index: appengine/findit/crash/flag_manager.py |
| diff --git a/appengine/findit/crash/flag_manager.py b/appengine/findit/crash/flag_manager.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..34897d2709faf5c145d91f72a424fb0e5fcdf828 |
| --- /dev/null |
| +++ b/appengine/findit/crash/flag_manager.py |
| @@ -0,0 +1,108 @@ |
| +# Copyright 2016 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""Module handles flags while doing stream parsing.""" |
|
wrengr
2016/12/22 22:31:11
It'd be helpful to explain what "flags" are. I kno
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + |
| +from collections import defaultdict |
| + |
| + |
| +class Flag(object): |
| + """Represents a flag of a obj. |
|
wrengr
2016/12/22 22:31:10
"a obj" -> "an obj". But (a) we prefer avoiding ab
Sharu Jiang
2016/12/27 23:48:03
Renamed to ``ParsingFlag`` :)
|
| + |
| + This object serves like a delegation to manipulate flag of obj, |
| + condition will be used to evaluate the flag value. |
| + """ |
| + def __init__(self, name, condition=None, val=False): |
|
wrengr
2016/12/22 22:31:10
val -> value
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + """Inits Flag to point to the flag of obj. |
| + |
| + Args: |
| + name (str): Name of the flag. |
| + condition (function): Funtion that returns bool, the condition to turn on |
|
wrengr
2016/12/22 22:31:11
what arguments does the function take? in what sor
Sharu Jiang
2016/12/27 23:48:04
Done.
|
| + the flag when the flag is off. |
| + val (bool): Initial value of the flag. |
| + """ |
| + self._name = name |
| + self._condition = condition |
| + self._val = val |
| + |
| + @property |
| + def name(self): |
| + return self._name |
| + |
| + @property |
| + def val(self): |
| + return self._val |
| + |
| + @val.setter |
| + def val(self, val): |
| + self._val = val |
|
wrengr
2016/12/22 22:31:11
Rather than having a setter, I think it'd be bette
Sharu Jiang
2016/12/27 23:48:03
good point, done.
|
| + |
| + def __nonzero__(self): |
| + return self._val |
| + |
| + __bool__ = __nonzero__ |
| + |
| + def ConditionallyTurnOn(self, line): |
| + """When the flag is off, turn on it if conditions met.""" |
| + if not self._val and self._condition and self._condition(line): |
| + self._val = True |
| + |
| + |
| +class FlagManager(object): |
| + """A manager to manage manipulations of all the registered flags. |
|
wrengr
2016/12/22 22:31:10
"manager to manage" is awkward
Sharu Jiang
2016/12/27 23:48:03
Oops.
|
| + |
| + FlagManager groups and manages flags based on group name. FlagManager takes |
|
wrengr
2016/12/22 22:31:10
suggest: "groups" -> "collects"
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + care of manipulations of flags during the stream parsing, include evaluating |
|
wrengr
2016/12/22 22:31:10
"manipulations of" -> "manipulating"
"include eva
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + based on condition of the flag and resetting the flags. |
|
wrengr
2016/12/22 22:31:10
"based on condition" -> "based on the condition"
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + """ |
| + |
| + def __init__(self): |
| + self.flag_groups = defaultdict(list) |
| + self.flags = {} |
| + |
| + def ClearFlags(self): |
| + self.flag_groups = defaultdict(list) |
| + self.flags = {} |
| + |
| + def Register(self, group_name, flag): |
| + """Registers a list of flags of a group. |
|
wrengr
2016/12/22 22:31:11
list? looks like it only takes one flag
Also the
Sharu Jiang
2016/12/27 23:48:04
Oops, forgot to update the doc str.
|
| + |
| + Flags under the same group should have the same scope. |
| + For example, in stacktrace parsing, the scope of a flag like |
| + ``after_sumary_line`` is the whole stacktrace, so it should be under |
| + ``stacktrace_flags`` group, while the scope of |
| + ``callstack_top_frame_has_no_symbol`` is callstack, and it should be |
| + under ``callstack_flags`` group. |
| + """ |
| + self.flag_groups[group_name].append(flag) |
| + self.flags[flag.name] = flag |
| + |
| + def GetFlagGroup(self, group_name=None): |
| + """Returns flags of a group or all flags if None group provided.""" |
|
wrengr
2016/12/22 22:31:10
"flags of a group" -> "a group's flags"
comma bef
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + if group_name: |
| + return self.flag_groups.get(group_name, []) |
| + |
| + flags = [] |
| + for group_flags in self.flag_groups.values(): |
| + flags.extend(group_flags) |
| + |
| + return flags |
| + |
| + def Reset(self, group_name=None): |
|
wrengr
2016/12/22 22:31:10
docstring. Especially since it does things based o
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + for flag in self.GetFlagGroup(group_name): |
| + flag.val = False |
| + |
| + def ConditionallyTurnOn(self, line, group_name=None): |
|
wrengr
2016/12/22 22:31:10
ditto
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + for flag in self.GetFlagGroup(group_name): |
| + flag.ConditionallyTurnOn(line) |
| + |
| + def Get(self, flag_name): |
|
wrengr
2016/12/22 22:31:11
ditto
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + return self.flags.get(flag_name) |
| + |
| + def Set(self, flag_name, val): |
|
wrengr
2016/12/22 22:31:10
ditto
Sharu Jiang
2016/12/27 23:48:03
Done.
|
| + flag = self.flags.get(flag_name) |
| + if flag is None: |
| + return |
| + |
| + flag.val = val |