| OLD | NEW |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 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 import collections |
| 5 import email.utils | 6 import email.utils |
| 6 | 7 |
| 7 | 8 |
| 8 def lazy_property(func): | 9 def lazy_property(func): |
| 9 """A decorator for lazy properties.""" | 10 """A decorator for lazy properties.""" |
| 10 name = '__lazy_' + func.__name__ | 11 name = '__lazy_' + func.__name__ |
| 11 | 12 |
| 12 def get_property(self, *args, **kwargs): | 13 def get_property(self, *args, **kwargs): |
| 13 if not hasattr(self, name): | 14 if not hasattr(self, name): |
| 14 setattr(self, name, func(self, *args, **kwargs)) | 15 setattr(self, name, func(self, *args, **kwargs)) |
| 15 return getattr(self, name) | 16 return getattr(self, name) |
| 16 | 17 |
| 17 return property(get_property) | 18 return property(get_property) |
| 18 | 19 |
| 19 | 20 |
| 20 class LazyDict(object): | 21 class LazyDict(collections.Mapping): |
| 21 """A simple immutable and lazy dictionary. | 22 """A simple immutable and lazy dictionary. |
| 22 | 23 |
| 23 This looks up the actual key values on first access to the key and caches the | 24 This looks up the actual key values on first access to the key and caches the |
| 24 value to speed up subsequent accesses. | 25 value to speed up subsequent accesses. |
| 25 """ | 26 """ |
| 26 | 27 |
| 27 def __init__(self, lookup_fn): | 28 def __init__(self, lookup_fn): |
| 28 self.items = {} | 29 self.items = {} |
| 29 self.lookup = lookup_fn | 30 self.lookup = lookup_fn |
| 30 | 31 |
| 31 def __getitem__(self, name): | 32 def __getitem__(self, name): |
| 32 if name not in self.items.keys(): | 33 if name not in self.items.keys(): |
| 33 self.items[name] = self.lookup(name) | 34 self.items[name] = self.lookup(name) |
| 34 return self.items[name] | 35 return self.items[name] |
| 35 | 36 |
| 37 def __iter__(self): |
| 38 return self.items.iterkeys() |
| 36 | 39 |
| 37 class ObjectDict(object): | 40 def __len__(self): |
| 41 return len(self.items) |
| 42 |
| 43 def __repr__(self): |
| 44 return repr(self.items) |
| 45 |
| 46 |
| 47 class ObjectDict(collections.Mapping): |
| 38 """Wraps a dictionary to allow value retrieval in dot notation.""" | 48 """Wraps a dictionary to allow value retrieval in dot notation.""" |
| 39 | 49 |
| 40 def __init__(self, data): | 50 def __init__(self, data): |
| 41 self.data = data | 51 self.__data = data |
| 42 | 52 |
| 43 def __getitem__(self, name): | 53 def __getitem__(self, name): |
| 44 val = self.data[name] | 54 val = self.__data[name] |
| 45 if type(val) == dict: | 55 return ObjectDict.wrap(val) |
| 46 return ObjectDict(val) | |
| 47 return val | |
| 48 | 56 |
| 49 def __getattr__(self, name): | 57 def __getattr__(self, name): |
| 50 try: | 58 try: |
| 51 return self[name] | 59 return self[name] |
| 52 except KeyError as e: | 60 except KeyError as e: |
| 53 raise AttributeError(e) | 61 raise AttributeError(e) |
| 54 | 62 |
| 55 def __iter__(self): | 63 def __iter__(self): |
| 56 return self.data.iterkeys() | 64 return self.__data.iterkeys() |
| 65 |
| 66 def __len__(self): |
| 67 return len(self.__data) |
| 68 |
| 69 def __repr__(self): |
| 70 return repr(self.__data) |
| 71 |
| 72 @staticmethod |
| 73 def wrap(val): |
| 74 if isinstance(val, dict): |
| 75 return ObjectDict(val) |
| 76 return val |
| 57 | 77 |
| 58 | 78 |
| 59 def get_emails(string): | 79 def get_emails(string): |
| 60 """Normalizes a string containing a list of email recepients. | 80 """Normalizes a string containing a list of email recepients. |
| 61 | 81 |
| 62 Takes a string in the format present in mail headers and returns a list of | 82 Takes a string in the format present in mail headers and returns a list of |
| 63 email addresses. For example, the input | 83 email addresses. For example, the input |
| 64 'test@example.com, committers <committers@chromium.org> | 84 'test@example.com, committers <committers@chromium.org> |
| 65 will produce this return value: | 85 will produce this return value: |
| 66 [ 'test@example.com', 'committers@chromium.org' ] | 86 [ 'test@example.com', 'committers@chromium.org' ] |
| 67 """ | 87 """ |
| 68 return [entry[1] for entry in email.utils.getaddresses([string])] | 88 return [entry[1] for entry in email.utils.getaddresses([string])] |
| OLD | NEW |