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