diff --git a/sloth/annotations/container.py b/sloth/annotations/container.py index f683088..84daec5 100644 --- a/sloth/annotations/container.py +++ b/sloth/annotations/container.py @@ -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): """ diff --git a/sloth/core/utils.py b/sloth/core/utils.py new file mode 100644 index 0000000..037a415 --- /dev/null +++ b/sloth/core/utils.py @@ -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 diff --git a/sloth/items/factory.py b/sloth/items/factory.py index 1bb1249..24ef044 100644 --- a/sloth/items/factory.py +++ b/sloth/items/factory.py @@ -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):