- move import_callable to core.utils

- allow str arguments in CONTAINERS
This commit is contained in:
Martin Baeuml
2011-05-24 22:09:43 +02:00
parent af67ec1720
commit 5c54661fa0
3 changed files with 30 additions and 23 deletions
+7 -2
View File
@@ -1,6 +1,7 @@
import os
import fnmatch
from sloth.core.exceptions import ImproperlyConfigured, NotImplementedException
from sloth.core.utils import import_callable
try:
import cPickle as pickle
except:
@@ -18,7 +19,11 @@ class AnnotationContainerFactory:
The mapping between file pattern and container class responsible
for loading/saving.
"""
self.containers_ = containers
self.containers_ = []
for pattern, item in containers:
if type(item) == str:
item = import_callable(item)
self.containers_.append((pattern, item))
def create(self, filename, *args, **kwargs):
"""
@@ -71,7 +76,7 @@ class AnnotationContainer:
implementation this will try to load the image from a path
relative to the label files directory.
"""
pass
return okapy.loadImage(filename)
def loadVideo(self, filename):
"""
+21
View File
@@ -0,0 +1,21 @@
from sloth.core import exceptions
import importlib
def import_callable(module_path_name):
"""
Import the callable given by ``module_path_name``.
"""
try:
module_path, name = module_path_name.rsplit('.', 1)
except ValueError:
raise exceptions.ImproperlyConfigured('%s is not a valid module path' % module_path_name)
try:
mod = importlib.import_module(module_path)
except ImportError, e:
raise exceptions.ImproperlyConfigured('Error importing module %s: "%s"' % (module_path, e))
try:
item_callable = getattr(mod, name)
except AttributeError:
raise exceptions.ImproperlyConfigured('Module "%s" does not define a "%s" callable' % (module_path, name))
return item_callable
+2 -21
View File
@@ -1,5 +1,5 @@
from sloth.core import exceptions
import importlib
from sloth.core.utils import import_callable
class Factory:
"""
@@ -22,25 +22,6 @@ class Factory:
for _type, item in items.iteritems():
self.register(_type, item, replace=True)
def import_callable(self, module_path_name):
"""
Import the callable given by ``module_path_name``.
"""
try:
module_path, name = module_path_name.rsplit('.', 1)
except ValueError:
raise exceptions.ImproperlyConfigured('%s is not a valid module path' % module_path_name)
try:
mod = importlib.import_module(module_path)
except ImportError, e:
raise exceptions.ImproperlyConfigured('Error importing module %s: "%s"' % (module_path, e))
try:
item_callable = getattr(mod, name)
except AttributeError:
raise exceptions.ImproperlyConfigured('Module "%s" does not define a "%s" callable' % (module_path, name))
return item_callable
def register(self, _type, item, replace=False):
"""
Register a new type-item mapping.
@@ -59,7 +40,7 @@ class Factory:
(_type, str(self.items_[_type])))
else:
if type(item) == str:
item = self.import_callable(item)
item = import_callable(item)
self.items_[_type] = item
def clear(self, _type=None):