def standard_extensions(ext_mgr): """Registers all standard API extensions.""" # Walk through all the modules in our directory... our_dir = __path__[0] for dirpath, dirnames, filenames in os.walk(our_dir): # Compute the relative package name from the dirpath relpath = os.path.relpath(dirpath, our_dir) if relpath == '.': relpkg = '' else: relpkg = '.%s' % '.'.join(relpath.split(os.sep)) # Now, consider each file in turn, only considering .py files for fname in filenames: root, ext = os.path.splitext(fname) # Skip __init__ and anything that's not .py if ext != '.py' or root == '__init__': continue # Try loading it classname = ( "%s%s.%s.%s%s" % (__package__, relpkg, root, root[0].upper(), root[1:])) try: ext_mgr.load_extension(classname) except Exception as exc: LOG.warn( _('Failed to load extension %(classname)s: ' '%(exc)s') % locals()) # Now, let's consider any subdirectories we may have... subdirs = [] for dname in dirnames: # Skip it if it does not have __init__.py if not os.path.exists(os.path.join(dirpath, dname, '__init__.py')): continue # If it has extension(), delegate... ext_name = ("%s%s.%s.extension" % (__package__, relpkg, dname)) try: ext = utils.import_class(ext_name) except exception.ClassNotFound: # extension() doesn't exist on it, so we'll explore # the directory for ourselves subdirs.append(dname) else: try: ext(ext_mgr) except Exception as exc: LOG.warn( _('Failed to load extension %(ext_name)s: ' '%(exc)s') % locals()) # Update the list of directories we'll explore... dirnames[:] = subdirs
def __init__(self, driver=None, *args, **kwargs): """Inits the driver from parameter or flag __init__ is run every time AuthManager() is called, so we only reset the driver if it is not set or a new driver is specified. """ self.network_manager = utils.import_object(FLAGS.network_manager) if driver or not getattr(self, 'driver', None): self.driver = utils.import_class(driver or FLAGS.auth_driver) if AuthManager.mc is None: AuthManager.mc = memcache.Client(FLAGS.memcached_servers, debug=0)
def __init__(self, host, binary, topic, manager, report_interval=None, periodic_interval=None, *args, **kwargs): self.host = host self.binary = binary self.topic = topic self.manager_class_name = manager manager_class = utils.import_class(self.manager_class_name) self.manager = manager_class(host=self.host, *args, **kwargs) self.report_interval = report_interval self.periodic_interval = periodic_interval super(Service, self).__init__(*args, **kwargs) self.saved_args, self.saved_kwargs = args, kwargs self.timers = []
def standard_extensions(ext_mgr): """Registers all standard API extensions.""" # Walk through all the modules in our directory... our_dir = __path__[0] for dirpath, dirnames, filenames in os.walk(our_dir): # Compute the relative package name from the dirpath relpath = os.path.relpath(dirpath, our_dir) if relpath == ".": relpkg = "" else: relpkg = ".%s" % ".".join(relpath.split(os.sep)) # Now, consider each file in turn, only considering .py files for fname in filenames: root, ext = os.path.splitext(fname) # Skip __init__ and anything that's not .py if ext != ".py" or root == "__init__": continue # Try loading it classname = "%s%s.%s.%s%s" % (__package__, relpkg, root, root[0].upper(), root[1:]) try: ext_mgr.load_extension(classname) except Exception as exc: LOG.warn(_("Failed to load extension %(classname)s: " "%(exc)s") % locals()) # Now, let's consider any subdirectories we may have... subdirs = [] for dname in dirnames: # Skip it if it does not have __init__.py if not os.path.exists(os.path.join(dirpath, dname, "__init__.py")): continue # If it has extension(), delegate... ext_name = "%s%s.%s.extension" % (__package__, relpkg, dname) try: ext = utils.import_class(ext_name) except exception.ClassNotFound: # extension() doesn't exist on it, so we'll explore # the directory for ourselves subdirs.append(dname) else: try: ext(ext_mgr) except Exception as exc: LOG.warn(_("Failed to load extension %(ext_name)s: " "%(exc)s") % locals()) # Update the list of directories we'll explore... dirnames[:] = subdirs
def load_extension(self, ext_factory): """Execute an extension factory. Loads an extension. The 'ext_factory' is the name of a callable that will be imported and called with one argument--the extension manager. The factory callable is expected to call the register() method at least once. """ LOG.debug(_("Loading extension %s"), ext_factory) # Load the factory factory = utils.import_class(ext_factory) # Call it LOG.debug(_("Calling extension factory %s"), ext_factory) factory(self)
def _get_manager(self): """Initialize a Manager object appropriate for this service. Use the service name to look up a Manager subclass from the configuration and initialize an instance. If no class name is configured, just return None. :returns: a Manager instance, or None. """ fl = '%s_manager' % self.name if not fl in FLAGS: return None manager_class_name = FLAGS.get(fl, None) if not manager_class_name: return None manager_class = utils.import_class(manager_class_name) return manager_class()
def get_cost_functions(self, topic=None): """Returns a list of tuples containing weights and cost functions to use for weighing hosts """ if topic is None: # Schedulers only support compute right now. topic = "compute" if topic in self.cost_function_cache: return self.cost_function_cache[topic] cost_fns = [] for cost_fn_str in FLAGS.least_cost_functions: if '.' in cost_fn_str: short_name = cost_fn_str.split('.')[-1] else: short_name = cost_fn_str cost_fn_str = "%s.%s.%s" % ( __name__, self.__class__.__name__, short_name) if not (short_name.startswith('%s_' % topic) or short_name.startswith('noop')): continue try: # NOTE: import_class is somewhat misnamed since # the weighing function can be any non-class callable # (i.e., no 'self') cost_fn = utils.import_class(cost_fn_str) except exception.ClassNotFound: raise exception.SchedulerCostFunctionNotFound( cost_fn_str=cost_fn_str) try: flag_name = "%s_weight" % cost_fn.__name__ weight = getattr(FLAGS, flag_name) except AttributeError: raise exception.SchedulerWeightFlagNotFound( flag_name=flag_name) cost_fns.append((weight, cost_fn)) self.cost_function_cache[topic] = cost_fns return cost_fns
def get_cost_functions(self, topic=None): """Returns a list of tuples containing weights and cost functions to use for weighing hosts """ if topic is None: # Schedulers only support compute right now. topic = "compute" if topic in self.cost_function_cache: return self.cost_function_cache[topic] cost_fns = [] for cost_fn_str in FLAGS.least_cost_functions: if '.' in cost_fn_str: short_name = cost_fn_str.split('.')[-1] else: short_name = cost_fn_str cost_fn_str = "%s.%s.%s" % (__name__, self.__class__.__name__, short_name) if not (short_name.startswith('%s_' % topic) or short_name.startswith('noop')): continue try: # NOTE: import_class is somewhat misnamed since # the weighing function can be any non-class callable # (i.e., no 'self') cost_fn = utils.import_class(cost_fn_str) except exception.ClassNotFound: raise exception.SchedulerCostFunctionNotFound( cost_fn_str=cost_fn_str) try: flag_name = "%s_weight" % cost_fn.__name__ weight = getattr(FLAGS, flag_name) except AttributeError: raise exception.SchedulerWeightFlagNotFound( flag_name=flag_name) cost_fns.append((weight, cost_fn)) self.cost_function_cache[topic] = cost_fns return cost_fns
def __init__(self, application, limits=None, limiter=None, **kwargs): """ Initialize new `RateLimitingMiddleware`, which wraps the given WSGI application and sets up the given limits. @param application: WSGI application to wrap @param limits: String describing limits @param limiter: String identifying class for representing limits Other parameters are passed to the constructor for the limiter. """ base_wsgi.Middleware.__init__(self, application) # Select the limiter class if limiter is None: limiter = Limiter else: limiter = utils.import_class(limiter) # Parse the limits, if any are provided if limits is not None: limits = limiter.parse_limits(limits) self._limiter = limiter(limits or DEFAULT_LIMITS, **kwargs)
import random import time from urlparse import urlparse from tank.common import exception as tank_exception from engine import exception from engine import flags from engine import log as logging from engine import utils LOG = logging.getLogger('engine.image.tank') FLAGS = flags.FLAGS TankClient = utils.import_class('tank.client.Client') def _parse_image_ref(image_href): """Parse an image href into composite parts. :param image_href: href of an image :returns: a tuple of the form (image_id, host, port) :raises ValueError """ o = urlparse(image_href) port = o.port or 80 host = o.netloc.split(':', 1)[0] image_id = o.path.split('/')[-1] return (image_id, host, port)
def get_default_image_service(): ImageService = utils.import_class(FLAGS.image_service) return ImageService()
from tank.common import exception as tank_exception from engine import exception from engine import flags from engine import log as logging from engine import utils LOG = logging.getLogger('engine.image.tank') FLAGS = flags.FLAGS TankClient = utils.import_class('tank.client.Client') def _parse_image_ref(image_href): """Parse an image href into composite parts. :param image_href: href of an image :returns: a tuple of the form (image_id, host, port) :raises ValueError """ o = urlparse(image_href) port = o.port or 80 host = o.netloc.split(':', 1)[0] image_id = o.path.split('/')[-1] return (image_id, host, port)
def __init__(self, app, controller): super(Requestify, self).__init__(app) self.controller = utils.import_class(controller)()