More source_group renaming

This commit is contained in:
Matthew Tretter 2012-12-11 22:33:33 -05:00
parent 52fb4e24be
commit 184c13dd4e
3 changed files with 35 additions and 35 deletions

View file

@ -19,8 +19,8 @@ class Command(BaseCommand):
for spec_id in specs:
self.stdout.write('Validating spec: %s\n' % spec_id)
for source in source_group_registry.get(spec_id):
for source_file in source.files():
for source_group in source_group_registry.get(spec_id):
for source_file in source_group.files():
if source_file:
spec = generator_registry.get(spec_id, source_file=source_file) # TODO: HINTS! (Probably based on source, so this will need to be moved into loop below.)
self.stdout.write(' %s\n' % source_file)

View file

@ -53,57 +53,57 @@ class SourceGroupRegistry(object):
"""
_source_signals = [
_signals = [
source_created,
source_changed,
source_deleted,
]
def __init__(self):
self._sources = {}
for signal in self._source_signals:
signal.connect(self.source_receiver)
self._source_groups = {}
for signal in self._signals:
signal.connect(self.source_group_receiver)
before_access.connect(self.before_access_receiver)
def register(self, spec_id, sources):
def register(self, spec_id, source_groups):
"""
Associates sources with a spec id
Associates source groups with a spec id
"""
for source in sources:
if source not in self._sources:
self._sources[source] = set()
self._sources[source].add(spec_id)
for source_group in source_groups:
if source_group not in self._source_groups:
self._source_groups[source_group] = set()
self._source_groups[source_group].add(spec_id)
def unregister(self, spec_id, sources):
def unregister(self, spec_id, source_groups):
"""
Disassociates sources with a spec id
"""
for source in sources:
for source_group in source_groups:
try:
self._sources[source].remove(spec_id)
self._source_groups[source_group].remove(spec_id)
except KeyError:
continue
def get(self, spec_id):
return [source for source in self._sources
if spec_id in self._sources[source]]
return [source_group for source_group in self._source_groups
if spec_id in self._source_groups[source_group]]
def before_access_receiver(self, sender, generator, file, **kwargs):
generator.image_cache_strategy.invoke_callback('before_access', file)
def source_receiver(self, sender, source_file, signal, info, **kwargs):
def source_group_receiver(self, sender, source_file, signal, info, **kwargs):
"""
Redirects signals dispatched on sources to the appropriate specs.
"""
source = sender
if source not in self._sources:
source_group = sender
if source_group not in self._source_groups:
return
for spec in (generator_registry.get(id, source_file=source_file, **info)
for id in self._sources[source]):
for id in self._sources_groups[source_group]):
event_name = {
source_created: 'source_created',
source_changed: 'source_changed',

View file

@ -11,27 +11,27 @@ def ik_model_receiver(fn):
"""
@wraps(fn)
def receiver(self, sender, **kwargs):
if sender in (src.model_class for src in self._sources):
if sender in (src.model_class for src in self._source_groups):
fn(self, sender=sender, **kwargs)
return receiver
class ModelSignalRouter(object):
"""
Handles signals dispatched by models and relays them to the spec sources
that represent those models.
Handles signals dispatched by models and relays them to the spec source
groups that represent those models.
"""
def __init__(self):
self._sources = []
self._source_groups = []
uid = 'ik_spec_field_receivers'
post_init.connect(self.post_init_receiver, dispatch_uid=uid)
post_save.connect(self.post_save_receiver, dispatch_uid=uid)
post_delete.connect(self.post_delete_receiver, dispatch_uid=uid)
def add(self, source):
self._sources.append(source)
def add(self, source_group):
self._source_groups.append(source_group)
def init_instance(self, instance):
instance._ik = getattr(instance, '_ik', {})
@ -55,7 +55,7 @@ class ModelSignalRouter(object):
"""
return dict((src.image_field, getattr(instance, src.image_field)) for
src in self._sources if src.model_class is instance.__class__)
src in self._source_groups if src.model_class is instance.__class__)
@ik_model_receiver
def post_save_receiver(self, sender, instance=None, created=False, raw=False, **kwargs):
@ -82,19 +82,19 @@ class ModelSignalRouter(object):
def dispatch_signal(self, signal, file, model_class, instance, attname):
"""
Dispatch the signal for each of the matching sources. Note that more
than one source can have the same model and image_field; it's important
that we dispatch the signal for each.
Dispatch the signal for each of the matching source groups. Note that
more than one source can have the same model and image_field; it's
important that we dispatch the signal for each.
"""
for source in self._sources:
if source.model_class is model_class and source.image_field == attname:
for source_group in self._source_groups:
if source_group.model_class is model_class and source_group.image_field == attname:
info = dict(
source=source,
source_group=source_group,
instance=instance,
field_name=attname,
)
signal.send(sender=source, source_file=file, info=info)
signal.send(sender=source_group, source_file=file, info=info)
class ImageFieldSourceGroup(object):