-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from ralphbean/feature/dynamic-services
Defer importing services until they are needed.
- Loading branch information
Showing
3 changed files
with
97 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
|
||
class DeferredImportingDict(object): | ||
def __init__(self, d): | ||
self._dict = d | ||
self._cache = {} | ||
|
||
def keys(self): | ||
return self._dict.keys() | ||
|
||
def __contains__(self, key): | ||
return key in self._dict | ||
|
||
def __getitem__(self, key): | ||
if not key in self: | ||
raise KeyError(key) | ||
|
||
if not key in self._cache: | ||
self._cache[key] = self._import(self._dict[key]) | ||
|
||
return self._cache[key] | ||
|
||
@classmethod | ||
def _import(cls, location): | ||
""" Given the string 'module1.module2:Object', returns Object. """ | ||
mod_name, obj_name = location = location.strip().split(':') | ||
tokens = mod_name.split('.') | ||
|
||
fromlist = '[]' | ||
if len(tokens) > 1: | ||
fromlist = '.'.join(tokens[:-1]) | ||
|
||
module = __import__(mod_name, fromlist=fromlist) | ||
|
||
try: | ||
return getattr(module, obj_name) | ||
except AttributeError: | ||
raise ImportError("%r not found in %r" % (obj_name, mod_name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from nose.tools import raises | ||
|
||
import unittest2 | ||
|
||
import bugwarrior.utils | ||
|
||
|
||
class UtilsTest(unittest2.TestCase): | ||
def setUp(self): | ||
self.d = bugwarrior.utils.DeferredImportingDict({ | ||
'chain': 'itertools:chain', | ||
}) | ||
self.fail = bugwarrior.utils.DeferredImportingDict({ | ||
'dne1': 'itertools:DNE', | ||
'dne2': 'notarealmodule:something', | ||
}) | ||
|
||
def test_importing_dict_access_success(self): | ||
item = self.d['chain'] | ||
import itertools | ||
self.assertEquals(item, itertools.chain) | ||
|
||
def test_importing_dict_contains_success(self): | ||
self.assertEquals('chain' in self.d, True) | ||
|
||
def test_importing_dict_contains_failure(self): | ||
self.assertEquals('nothing' in self.d, False) | ||
|
||
def test_importing_dict_keys(self): | ||
self.assertEquals(set(self.d.keys()), set(['chain'])) | ||
|
||
@raises(ImportError) | ||
def test_importing_unimportable_object(self): | ||
self.fail['dne1'] | ||
|
||
@raises(ImportError) | ||
def test_importing_unimportable_module(self): | ||
self.fail['dne2'] |