Пример #1
0
def split(mapping):
    """Split a configuration mapping into ``protocol``, and the rest."""
    if not 'protocol' in mapping:
        raise exc.ConfigurationError('protocol',
                                     'Missing protocol specification')
    protocol = mapping.pop('protocol')
    return mapping, protocol
Пример #2
0
    def __call__(self, loader, node):
        kwargs, protocol = split(loader.construct_mapping(node, deep=True))

        try:
            return self.cls.instantiate(protocol, **kwargs)
        except Exception as ex:
            raise exc.ConfigurationError(
                'config',
                'Abstract factory error while parsing YAML: {0}'.format(ex),
                loader, node), None, sys.exc_info()[2]
Пример #3
0
Файл: mq.py Проект: zfarkas/util
 def __init__(self, **config):
     try:
         log.debug('Config:\n%r', config)
         self.credentials = pika.PlainCredentials(config['user'],
                                                  config['password'])
         self.connection_parameters = pika.ConnectionParameters(
             config['host'], config.get('port', 5672), config['vhost'],
             self.credentials)
     except KeyError as e:
         raise exc.ConfigurationError(e)
     self.default_exchange = config.get('exchange', '')
     self.default_routing_key = config.get('routing_key', None)
     self.auto_delete = config.get('auto_delete', False)
Пример #4
0
    def instantiate(cls, protocol, *args, **kwargs):
        """
        Instantiates the given class while inhibiting implicit __init__ call.
        This lets the factory __new__ hide the protocol specification from the
        factory class.

        Use this to instantiate factory classes from code.

        This method will instantiate the correct backend identified by
        ``protocol`` in ``kwargs``.

        :raises occo.exceptions.ConfigurationError: if ``protocol`` is not
            specified in ``kwargs``, or the backend identified by ``protocol``
            does not exist.
        """

        if protocol is None:
            log.debug('Factory: Instantiating %s itself', cls.__name__)
            objclass = cls
        else:
            if not hasattr(cls, 'backends'):
                raise exc.ConfigurationError(
                    'backends',
                    ("The MultiBackend class {0!r} "
                     "has no registered backends.").format(cls.__name__))
            if not protocol in cls.backends:
                raise exc.ConfigurationError(
                    'protocol',
                    'The backend {0!r} does not exist. Available backends: {1!r}'
                    .format(protocol, cls.backends))
            log.debug('Instantiating a backend for %s; protocol: %r',
                      cls.__name__, protocol)
            objclass = cls.backends[protocol]

        obj = object.__new__(objclass)
        objclass.__init__(obj, *args, **kwargs)
        return obj
Пример #5
0
Файл: mq.py Проект: zfarkas/util
 def __init__(self,
              processor,
              pargs=[],
              pkwargs={},
              cancel_event=None,
              **config):
     super(MQEventDrivenConsumer, self).__init__(**config)
     comm.EventDrivenConsumer.__init__(self,
                                       processor=processor,
                                       pargs=pargs,
                                       pkwargs=pkwargs)
     self.cancel_event = cancel_event
     try:
         self.queue = config['queue']
     except KeyError:
         raise exc.ConfigurationError('queue', 'Queue name is mandatory')
Пример #6
0
 def __init__(self,
              host='localhost',
              port='6379',
              db=0,
              altdbs=None,
              serialize=yaml.dump,
              deserialize=yaml.load,
              **kwargs):
     super(RedisKVStore, self).__init__(**kwargs)
     self.host, self.port, self.default_db = host, port, db
     self.altdbs = util.coalesce(altdbs, dict())
     self.inverse_altdbs = dict(
         (v, k) for k, v in list(self.altdbs.items()))
     if len(self.altdbs) != len(self.inverse_altdbs):
         raise exc.ConfigurationError(
             'The specified altdbs is not a bijection', self.altdbs)
     self.serialize = serialize
     self.deserialize = deserialize