Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added loader module for importing a python class or django model by name
- Loading branch information
Showing
3 changed files
with
61 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# __BEGIN_LICENSE__ | ||
# Copyright (C) 2008-2010 United States Government as represented by | ||
# the Administrator of the National Aeronautics and Space Administration. | ||
# All Rights Reserved. | ||
# __END_LICENSE__ | ||
|
||
""" | ||
Utilities for loading Python classes and Django models by name. Modeled | ||
in part on django.utils.importlib. | ||
""" | ||
|
||
def getModClass(name): | ||
"""converts 'app_name.ModelName' to ['stuff.module', 'ClassName']""" | ||
try: | ||
dot = name.rindex('.') | ||
except ValueError: | ||
return name, '' | ||
return name[:dot], name[dot + 1:] | ||
|
||
|
||
def getModelByName(qualifiedName): | ||
""" | ||
converts 'module_name.ClassName' to a class object | ||
""" | ||
appName, className = qualifiedName.split('.', 1) | ||
modelsName = '%s.models' % appName | ||
__import__(modelsName) | ||
mod = sys.modules[modelsName] | ||
return getattr(mod, className) |
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,31 @@ | ||
# __BEGIN_LICENSE__ | ||
# Copyright (C) 2008-2010 United States Government as represented by | ||
# the Administrator of the National Aeronautics and Space Administration. | ||
# All Rights Reserved. | ||
# __END_LICENSE__ | ||
|
||
import shutil | ||
|
||
from geocamUtil.store import FileStore, LruCacheStore | ||
|
||
|
||
class StoreTest(TestCase): | ||
def storeTest(self, storeFactory): | ||
tempDir = tempfile.mkdtemp('-storeTestDir') | ||
|
||
store1 = storeFactory(tempDir) | ||
store1['a'] = 1 | ||
store1['b'] = 2 | ||
store1.sync() | ||
|
||
store2 = storeFactory(tempDir) | ||
self.assertEqual(store2['a'], 1) | ||
self.assertEqual(store2['b'], 2) | ||
|
||
shutil.rmtree(tempDir) | ||
|
||
def test_FileStore(self): | ||
self.storeTest(FileStore) | ||
|
||
def test_LruCacheStore(self): | ||
self.storeTest(lambda path: LruCacheStore(FileStore(path), 100)) |
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