def schedule_loadbalancer(conf, lb_ref): conf.register_opts(bind_opts) device_filters = [utils.import_class(foo) for foo in conf.device_filters] all_devices = db_api.device_get_all(conf) if not all_devices: raise exp.DeviceNotFound cost_functions = [] for fullname in conf.device_cost_functions: conf_name = 'device_cost_%s_weight' % fullname.rpartition('.')[-1] try: weight = getattr(conf, conf_name) except cfg.NoSuchOptError: conf.register_opt(cfg.FloatOpt(conf_name, default=1.)) weight = getattr(conf, conf_name) cost_functions.append((utils.import_class(fullname), weight)) filtered_devices = [dev for dev in all_devices if all(filt(conf, lb_ref, dev) for filt in device_filters)] if not filtered_devices: raise exp.NoValidDevice costed = [] for dev in filtered_devices: w = 0. for cost_func, weight in cost_functions: w += weight * cost_func(conf, lb_ref, dev) costed.append((w, dev)) costed.sort() return costed[0][1]
def _process_config(conf): conf.register_opts(bind_opts) device_filters = [utils.import_class(foo) for foo in conf.device_filters] cost_functions = [] for fullname in conf.device_cost_functions: conf_name = 'device_cost_%s_weight' % fullname.rpartition('.')[-1] try: weight = getattr(conf, conf_name) except cfg.NoSuchOptError: conf.register_opt(cfg.FloatOpt(conf_name, default=1.)) weight = getattr(conf, conf_name) cost_functions.append((utils.import_class(fullname), weight)) return device_filters, cost_functions
def __init__(self, app, conf, **local_conf): self.conf = conf self.conf.register_opts(self.opts) # Determine the context class to use self.ctxcls = RequestContext if 'context_class' in local_conf: self.ctxcls = utils.import_class(local_conf['context_class']) super(ContextMiddleware, self).__init__(app)
def get_device_driver(conf, device_id): try: return DEVICE_DRIVERS[device_id] except KeyError: conf.register_opt(drivers_opt) drivers = {} for driver_str in conf.device_drivers: driver_type, _sep, driver = driver_str.partition('=') drivers[driver_type.lower()] = utils.import_class(driver) device_ref = db_api.device_get(conf, device_id) try: cls = drivers[device_ref['type'].lower()] except KeyError: raise NotImplementedError("Driver not found for type %s" % \ (device_ref['type'],)) DEVICE_DRIVERS[device_id] = cls(conf, device_ref) return DEVICE_DRIVERS[device_id]
def _import_factory(self, local_conf): """Import an app/filter class. Lookup the KEY from the PasteDeploy local conf and import the class named there. This class can then be used as an app or filter factory. Note we support the <module>:<class> format. Note also that if you do e.g. key = value then ConfigParser returns a value with a leading newline, so we strip() the value before using it. """ class_name = local_conf[self.KEY].replace(':', '.').strip() return utils.import_class(class_name)