def _validate_config(self): if 'transport_name' not in self.config: raise ConfigError("Missing 'transport_name' field in config.") send_to_options = self.config.get('send_to', {}) for tag in self.SEND_TO_TAGS: if tag not in send_to_options: raise ConfigError("No configuration for send_to tag %r but" " at least a transport_name is required." % (tag, )) if 'transport_name' not in send_to_options[tag]: raise ConfigError("The configuration for send_to tag %r must" " contain a transport_name." % (tag, )) return self.validate_config()
def assert_field(cfg, key): """ helper to check whether a config key is defined. Only used for verifying dict fields in the new-style configs """ if key not in cfg: raise ConfigError("Expected '%s' field in config" % key)
def __init__(self, transport, config): self.transport = transport self.log = transport.log self.redis = transport.redis self.codec = transport.get_static_config().codec_class() self.config = self.CONFIG_CLASS(config, static=True) self.data_coding_map = { 1: 'ascii', 3: 'latin1', # http://www.herongyang.com/Unicode/JIS-ISO-2022-JP-Encoding.html 5: 'iso2022_jp', 6: 'iso8859_5', 7: 'iso8859_8', # Actually UCS-2, but close enough. 8: 'utf-16be', # http://en.wikipedia.org/wiki/Short_Message_Peer-to-Peer 9: 'shift_jis', 10: 'iso2022_jp' } try: self.data_coding_map.update({ int(key): value for key, value in self.config.data_coding_overrides.items() }) except ValueError as e: raise ConfigError( "data_coding_overrides keys must be castable to ints. " "{}".format(e.message)) self.allow_empty_messages = self.config.allow_empty_messages
def _convert_rlimits(self, rlimits_config): rlimits = dict((getattr(resource, key, key), value) for key, value in rlimits_config.iteritems()) for key in rlimits.iterkeys(): if not isinstance(key, (int, long)): raise ConfigError("Unknown resource limit key %r" % (key,)) return rlimits
def setup_routing(self): self.r_config = self.config.get('redis_manager', {}) self.r_prefix = self.config['dispatcher_name'] self.rules = [] for rule in self.config.get('rules', []): if 'keyword' not in rule or 'app' not in rule: raise ConfigError("Rule definition %r must contain values for" " both 'app' and 'keyword'" % rule) rule = rule.copy() rule['keyword'] = rule['keyword'].lower() self.rules.append(rule) keyword_mappings = self.config.get('keyword_mappings', {}) for transport_name, keyword in keyword_mappings.items(): self.rules.append({ 'app': transport_name, 'keyword': keyword.lower() }) self.fallback_application = self.config.get('fallback_application') self.transport_mappings = self.config['transport_mappings'] self.expire_routing_timeout = int( self.config.get('expire_routing_memory', self.DEFAULT_ROUTING_TIMEOUT)) # FIXME: The following is a hack to deal with sync-only setup. self._redis_d = TxRedisManager.from_config(self.r_config) self._redis_d.addCallback(lambda m: m.sub_manager(self.r_prefix)) self._redis_d.addCallback(self._setup_redis)
def validate_config(self): self.manager_name = self.config['manager_name'] self.count_suffix = self.config.get('count_suffix', 'count') self.response_time_suffix = self.config.get('response_time_suffix', 'response_time') self.max_lifetime = int(self.config.get('max_lifetime', 60)) self.op_mode = self.config.get('op_mode', 'passive') if self.op_mode not in self.KNOWN_MODES: raise ConfigError('Unknown op_mode: %s' % (self.op_mode, ))
def create_middlewares_from_config(worker, config): """Return a list of middleware objects created from a worker configuration. """ middlewares = [] for item in config.get("middleware", []): keys = item.keys() if len(keys) != 1: raise ConfigError( "Middleware items contain only a single key-value pair. The" " key should be a name for the middleware. The value should be" " the full dotted name of the class implementing the" " middleware, or a mapping containing the keys 'class' with a" " value of the full dotted class name, 'consume_priority' with" " the priority level for consuming, and 'publish_priority'" " with the priority level for publishing, both integers.") middleware_name = keys[0] middleware_config = config.get(middleware_name, {}) if isinstance(item[middleware_name], basestring): cls_name = item[middleware_name] middleware_config['consume_priority'] = 0 middleware_config['publish_priority'] = 0 elif isinstance(item[middleware_name], dict): conf = item[middleware_name] cls_name = conf.get('class') try: middleware_config['consume_priority'] = int( conf.get('consume_priority', 0)) middleware_config['publish_priority'] = int( conf.get('publish_priority', 0)) except ValueError: raise ConfigError( "Middleware priority level must be an integer") else: raise ConfigError( "Middleware item values must either be a string with the", " full dotted name of the class implementing the middleware," " or a dictionary with 'class', 'consume_priority', and" " 'publish_priority' keys.") cls = load_class_by_string(cls_name) middleware = cls(middleware_name, middleware_config, worker) middlewares.append(middleware) return middlewares
def create_rules(self, migration_config): rules = [] for rule in migration_config['rules']: kw = rule.copy() rule_name = kw.pop('type') rule_maker = getattr(self, "make_rule_%s" % rule_name, None) if rule_maker is None: raise ConfigError("Unknown rule type %r" % rule_name) rules.append(rule_maker(kw)) return rules
def get_auth_headers(self, config): auth_method, username, password = (config.rapidsms_auth_method, config.rapidsms_username, config.rapidsms_password) if auth_method not in self.supported_auth_methods: raise ConfigError('HTTP Authentication method %s' ' not supported' % (repr(auth_method,))) if username is not None: handler = self.supported_auth_methods.get(auth_method) return handler(username, password) return {}
def _dispatch_inbound(self, publish_function, vumi_message): transport_name = vumi_message['transport_name'] redirect_to = self.inbound_mappings[transport_name] if not redirect_to: raise ConfigError( "No exposed name available for %s's inbound message: %s" % (transport_name, vumi_message)) msg_copy = vumi_message.copy() msg_copy['transport_name'] = redirect_to publish_function(redirect_to, msg_copy)
def create_middlewares_from_config(worker, config): """Return a list of middleware objects created from a worker configuration. """ middlewares = [] for item in config.get("middleware", []): keys = item.keys() if len(keys) != 1: raise ConfigError("Middleware items contain only a single" " key-value pair. The key should be a name" " for the middleware. The value should be" " the full dotted name of the class" " implementing the middleware.") middleware_name = keys[0] cls_name = item[middleware_name] middleware_config = config.get(middleware_name, {}) cls = load_class_by_string(cls_name) middleware = cls(middleware_name, middleware_config, worker) middlewares.append(middleware) return middlewares
def setup_routing(self): self.r_config = self.config.get('redis_config', {}) self.r_prefix = self.config['dispatcher_name'] self.r_server = redis.Redis(**self.r_config) self.rules = [] for rule in self.config.get('rules', []): if 'keyword' not in rule or 'app' not in rule: raise ConfigError("Rule definition %r must contain values for" " both 'app' and 'keyword'" % rule) rule = rule.copy() rule['keyword'] = rule['keyword'].lower() self.rules.append(rule) keyword_mappings = self.config.get('keyword_mappings', {}) for transport_name, keyword in keyword_mappings.items(): self.rules.append({ 'app': transport_name, 'keyword': keyword.lower() }) self.fallback_application = self.config.get('fallback_application') self.transport_mappings = self.config['transport_mappings'] self.expire_routing_timeout = int( self.config.get('expire_routing_memory', self.DEFAULT_ROUTING_TIMEOUT))
def _validate_config(self): if 'transport_name' not in self.config: raise ConfigError("Missing 'transport_name' field in config.") return self.validate_config()
def setup_routing(self): if len(self.config['exposed_names']) != 1: raise ConfigError("Only one exposed name allowed for %s." % (type(self).__name__, )) [self.exposed_name] = self.config['exposed_names']
def validate_config(self): if "worker_name" not in self.config: raise ConfigError("DecisionTreeWorker requires a worker_name" " in its configuration.")