OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 """This module provides maps and sets that report unused elements.""" | 6 """This module provides maps and sets that report unused elements.""" |
7 | 7 |
8 _monitored_values = [] | 8 _monitored_values = [] |
9 | 9 |
10 | 10 |
11 def FinishMonitoring(): | 11 def FinishMonitoring(): |
12 for value in _monitored_values: | 12 for value in _monitored_values: |
13 value.CheckUsage() | 13 value.CheckUsage() |
14 | 14 |
15 class MonitoredCollection(object): | 15 class MonitoredCollection(object): |
16 def __init__(self, name): | 16 def __init__(self, name): |
17 self.name = name | 17 self.name = name |
18 self._used_keys = set() | 18 self._used_keys = set() |
19 _monitored_values.append(self) | 19 _monitored_values.append(self) |
20 | 20 |
21 class Dict(MonitoredCollection): | 21 class Dict(MonitoredCollection): |
22 """Wrapper for a dict that reports unused keys.""" | 22 """Wrapper for a read-only dict that reports unused keys.""" |
23 | 23 |
24 def __init__(self, name, map): | 24 def __init__(self, name, map): |
25 super(Dict, self).__init__(name) | 25 super(Dict, self).__init__(name) |
26 self._map = map | 26 self._map = map |
27 | 27 |
28 def __getitem__(self, key): | 28 def __getitem__(self, key): |
29 self._used_keys.add(key) | 29 self._used_keys.add(key) |
30 return self._map[key] | 30 return self._map[key] |
31 | 31 |
32 def __setitem__(self, key, value): | 32 def __setitem__(self, key, value): |
(...skipping 10 matching lines...) Expand all Loading... |
43 self._used_keys.add(key) | 43 self._used_keys.add(key) |
44 return self._map.get(key, default) | 44 return self._map.get(key, default) |
45 | 45 |
46 def CheckUsage(self): | 46 def CheckUsage(self): |
47 for v in sorted(self._map.keys()): | 47 for v in sorted(self._map.keys()): |
48 if v not in self._used_keys: | 48 if v not in self._used_keys: |
49 print "dict '%s' has unused key '%s'" % (self.name, v) | 49 print "dict '%s' has unused key '%s'" % (self.name, v) |
50 | 50 |
51 | 51 |
52 class Set(MonitoredCollection): | 52 class Set(MonitoredCollection): |
53 """Wrapper for a set that reports unused keys.""" | 53 """Wrapper for a read-only set that reports unused keys.""" |
54 | 54 |
55 def __init__(self, name, a_set): | 55 def __init__(self, name, a_set): |
56 super(Set, self).__init__(name) | 56 super(Set, self).__init__(name) |
57 self._set = a_set | 57 self._set = a_set |
58 | 58 |
59 def __contains__(self, key): | 59 def __contains__(self, key): |
60 self._used_keys.add(key) | 60 self._used_keys.add(key) |
61 return key in self._set | 61 return key in self._set |
62 | 62 |
63 def __iter__(self): | |
64 return self._set.__iter__() | |
65 | |
66 def add(self, key): | |
67 self._set += [key] | |
68 | |
69 def CheckUsage(self): | 63 def CheckUsage(self): |
70 for v in sorted(self._set): | 64 for v in sorted(self._set): |
71 if v not in self._used_keys: | 65 if v not in self._used_keys: |
72 print "set '%s' has unused key '%s'" % (self.name, v) | 66 print "set '%s' has unused key '%s'" % (self.name, v) |
OLD | NEW |