Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: appengine/findit/crash/flag_manager.py

Issue 2593093003: [Predator] Add flag manager and flag manager test. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | appengine/findit/crash/test/flag_manager_test.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 # found in the LICENSE file.
4
5 """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.
6
7 from collections import defaultdict
8
9
10 class Flag(object):
11 """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`` :)
12
13 This object serves like a delegation to manipulate flag of obj,
14 condition will be used to evaluate the flag value.
15 """
16 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.
17 """Inits Flag to point to the flag of obj.
18
19 Args:
20 name (str): Name of the flag.
21 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.
22 the flag when the flag is off.
23 val (bool): Initial value of the flag.
24 """
25 self._name = name
26 self._condition = condition
27 self._val = val
28
29 @property
30 def name(self):
31 return self._name
32
33 @property
34 def val(self):
35 return self._val
36
37 @val.setter
38 def val(self, val):
39 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.
40
41 def __nonzero__(self):
42 return self._val
43
44 __bool__ = __nonzero__
45
46 def ConditionallyTurnOn(self, line):
47 """When the flag is off, turn on it if conditions met."""
48 if not self._val and self._condition and self._condition(line):
49 self._val = True
50
51
52 class FlagManager(object):
53 """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.
54
55 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.
56 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.
57 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.
58 """
59
60 def __init__(self):
61 self.flag_groups = defaultdict(list)
62 self.flags = {}
63
64 def ClearFlags(self):
65 self.flag_groups = defaultdict(list)
66 self.flags = {}
67
68 def Register(self, group_name, flag):
69 """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.
70
71 Flags under the same group should have the same scope.
72 For example, in stacktrace parsing, the scope of a flag like
73 ``after_sumary_line`` is the whole stacktrace, so it should be under
74 ``stacktrace_flags`` group, while the scope of
75 ``callstack_top_frame_has_no_symbol`` is callstack, and it should be
76 under ``callstack_flags`` group.
77 """
78 self.flag_groups[group_name].append(flag)
79 self.flags[flag.name] = flag
80
81 def GetFlagGroup(self, group_name=None):
82 """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.
83 if group_name:
84 return self.flag_groups.get(group_name, [])
85
86 flags = []
87 for group_flags in self.flag_groups.values():
88 flags.extend(group_flags)
89
90 return flags
91
92 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.
93 for flag in self.GetFlagGroup(group_name):
94 flag.val = False
95
96 def ConditionallyTurnOn(self, line, group_name=None):
wrengr 2016/12/22 22:31:10 ditto
Sharu Jiang 2016/12/27 23:48:03 Done.
97 for flag in self.GetFlagGroup(group_name):
98 flag.ConditionallyTurnOn(line)
99
100 def Get(self, flag_name):
wrengr 2016/12/22 22:31:11 ditto
Sharu Jiang 2016/12/27 23:48:03 Done.
101 return self.flags.get(flag_name)
102
103 def Set(self, flag_name, val):
wrengr 2016/12/22 22:31:10 ditto
Sharu Jiang 2016/12/27 23:48:03 Done.
104 flag = self.flags.get(flag_name)
105 if flag is None:
106 return
107
108 flag.val = val
OLDNEW
« no previous file with comments | « no previous file | appengine/findit/crash/test/flag_manager_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698