group.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from __future__ import absolute_import
  2. import logging
  3. import six
  4. from .pool import Pool
  5. log = logging.getLogger(__name__)
  6. class GroupMetaclass(type):
  7. __instances__ = dict()
  8. __blacklist__ = ('Group', 'GroupMetaclass')
  9. def __new__(cls, name, bases, attrs):
  10. if name in cls.__blacklist__:
  11. return super(GroupMetaclass, cls).__new__(cls, name, bases, attrs)
  12. group_key = attrs.get('group_key') or '{}Group'.format(name)
  13. new_class = super(GroupMetaclass, cls).__new__(cls, group_key,
  14. bases, attrs)
  15. pool_key = attrs.get('poll_key') or '{}Pool'.format(group_key)
  16. NewPool = type(pool_key, (Pool,),
  17. dict(pool_key=pool_key))
  18. setattr(new_class, 'pool', NewPool())
  19. setattr(new_class, 'pool_key', pool_key)
  20. setattr(new_class, 'group_key', group_key)
  21. if group_key not in cls.__instances__:
  22. cls.__instances__[group_key] = new_class
  23. return cls.__instances__[group_key]
  24. class Group(six.with_metaclass(GroupMetaclass, object)):
  25. group_key = None
  26. pool_key = None