Support inner Config class for id, sources

Closes #164
This commit is contained in:
Matthew Tretter 2012-10-24 22:23:32 -04:00
parent 8d3fcafcd9
commit bdec396180
2 changed files with 21 additions and 2 deletions

View file

@ -12,3 +12,7 @@ class UnknownExtensionError(Exception):
class UnknownFormatError(Exception):
pass
class MissingSpecId(Exception):
pass

View file

@ -2,7 +2,8 @@ from django.conf import settings
from hashlib import md5
import os
import pickle
from ..exceptions import UnknownExtensionError, AlreadyRegistered, NotRegistered
from ..exceptions import (UnknownExtensionError, AlreadyRegistered,
NotRegistered, MissingSpecId)
from ..files import ImageSpecCacheFile, IKContentFile
from ..imagecache.backends import get_default_image_cache_backend
from ..imagecache.strategies import StrategyWrapper
@ -36,11 +37,25 @@ class SpecRegistry(object):
signal.connect(self.source_receiver)
before_access.connect(self.before_access_receiver)
def register(self, spec, id):
def register(self, spec, id=None):
config = getattr(spec, 'Config', None)
if id is None:
id = getattr(config, 'id', None)
if id is None:
raise MissingSpecId('No id provided for %s. You must either pass an'
' id to the register function, or add an id'
' attribute to the inner Config class of your'
' spec.' % spec)
if id in self._specs:
raise AlreadyRegistered('The spec with id %s is already registered' % id)
self._specs[id] = spec
for source in getattr(config, 'sources', None) or []:
self.add_source(source, id)
def unregister(self, id, spec):
try:
del self._specs[id]