Exemplo n.º 1
0
def parameterized_class(cls):
    """A class decorator for running parameterized test cases.
    Mark your class with @parameterized_class.
    Mark your test cases with @parameterized.
    """
    test_functions = {
        k: v for k, v in vars(cls).items() if k.startswith('test')
    }
    for name, f in test_functions.items():
        if not hasattr(f, '_test_data'):
            continue

        # remove the original test function from the class
        delattr(cls, name)

        # add a new test function to the class for each entry in f._test_data
        for tag, args in f._test_data.items():
            new_name = "{0}_{1}".format(f.__name__, tag)
            if hasattr(cls, new_name):
                raise Exception(
                    "Parameterized test case '{0}.{1}' created from '{0}.{2}' "
                    "already exists".format(cls.__name__, new_name, name))

            # Using `def new_method(self): f(self, **args)` is not sufficient
            # (all new_methods use the same args value due to late binding).
            # Instead, use this factory function.
            new_method = def_method(f, **args)

            # To add a method to a class, available for all instances:
            #   MyClass.method = types.MethodType(f, None, MyClass)
            setattr(cls, new_name, six.create_unbound_method(new_method, cls))
    return cls
Exemplo n.º 2
0
    def _set_cinder_config(cls, host, locks_path, cinder_config_params):
        """Setup the parser with all the known Cinder configuration."""
        cfg.CONF.set_default('state_path', os.getcwd())
        cfg.CONF.set_default('lock_path', '$state_path', 'oslo_concurrency')

        cls._parser = six.moves.configparser.SafeConfigParser()
        cls._parser.set('DEFAULT', 'enabled_backends', '')

        if locks_path:
            cls._parser.add_section('oslo_concurrency')
            cls._parser.set('oslo_concurrency', 'lock_path', locks_path)
            cls._parser.add_section('coordination')
            cls._parser.set('coordination', 'backend_url',
                            'file://' + locks_path)
        if host:
            cls._parser.set('DEFAULT', 'host', host)

        # All other configuration options go into the DEFAULT section
        cls.__set_parser_kv(cinder_config_params, 'DEFAULT')

        # We replace the OSLO's default parser to read from a StringIO instead
        # of reading from a file.
        cls._config_string_io = six.moves.StringIO()
        cfg.ConfigParser.parse = six.create_unbound_method(
            cls._config_parse, cfg.ConfigParser)

        # Replace command line arg parser so we ignore caller's args
        cfg._CachedArgumentParser.parse_args = lambda *a, **kw: None

        # Update the configuration with the options we have configured
        cfg.CONF(project='cinder',
                 version=cinderlib.__version__,
                 default_config_files=['in_memory_file'])
        cls._update_cinder_config()
Exemplo n.º 3
0
def parameterized_class(cls):
    """A class decorator for running parameterized test cases.

    Mark your class with @parameterized_class.
    Mark your test cases with @parameterized.
    """
    test_functions = {
        k: v
        for k, v in vars(cls).items() if k.startswith('test')
    }
    for name, f in test_functions.items():
        if not hasattr(f, '_test_data'):
            continue

        # remove the original test function from the class
        delattr(cls, name)

        # add a new test function to the class for each entry in f._test_data
        for tag, args in f._test_data.items():
            new_name = "{0}_{1}".format(f.__name__, tag)
            if hasattr(cls, new_name):
                raise Exception(
                    "Parameterized test case '{0}.{1}' created from '{0}.{2}' "
                    "already exists".format(cls.__name__, new_name, name))

            # Using `def new_method(self): f(self, **args)` is not sufficient
            # (all new_methods use the same args value due to late binding).
            # Instead, use this factory function.
            new_method = def_method(f, **args)

            # To add a method to a class, available for all instances:
            #   MyClass.method = types.MethodType(f, None, MyClass)
            setattr(cls, new_name, six.create_unbound_method(new_method, cls))
    return cls
Exemplo n.º 4
0
def _replace_oslo_cli_parse():
    original_cli_parser = cfg.ConfigOpts._parse_cli_opts

    def _parse_cli_opts(self, args):
        return original_cli_parser(self, [])

    cfg.ConfigOpts._parse_cli_opts = six.create_unbound_method(
        _parse_cli_opts, cfg.ConfigOpts)
Exemplo n.º 5
0
def test_create_unbound_method():
    class X(object):
        pass

    def f(self):
        return self
    u = six.create_unbound_method(f, X)
    py.test.raises(TypeError, u)
    if six.PY2:
        assert isinstance(u, types.MethodType)
    x = X()
    assert f(x) is x
Exemplo n.º 6
0
def test_create_unbound_method():
    class X(object):
        pass

    def f(self):
        return self
    u = six.create_unbound_method(f, X)
    py.test.raises(TypeError, u)
    if six.PY2:
        assert isinstance(u, types.MethodType)
    x = X()
    assert f(x) is x
Exemplo n.º 7
0
def bind_service_methods(target, services=None, bind_to_class=False):
    if services is None:
        services = [target]

    from pyinthesky import meta
    bind_to_meta = bind_target_class = False

    if bind_to_class is True:
        target_class = target.__class__
    elif bind_to_class is False:
        target_class = None
    elif hasattr(meta, bind_to_class):
        target_class = getattr(meta, bind_to_class)
        bind_target_class = True
    else:
        target_class = type(bind_to_class, (target.__class__,), {})
        target_class.__module__ = 'pyinthesky.meta'
        bind_to_meta = True
        bind_target_class = True

    serv_methods = {}
    for service in services:
        for methname, (in_args, out_args) in service.methods.items():
            func = partial(service.invoke, methname)
            args = list(in_args.argument_order)
            f = method_sig_wrapper(func, methname, args,
                in_args.argument_defaults,
                make_method=target_class is not None)
            serv_methods.setdefault(methname, []).append([
                f, service.name + '.' + methname,
            ])

    # We've now built functions for all remote methods, so see what we
    # can set and what we can't.
    for methname, methods in serv_methods.items():
        if len(methods) == 1:
            f = methods[0][0]
        else:
            f = make_ambiguous_function(methname, [x[1] for x in methods])
        if target_class is None:
            setattr(target, methname, f)
        else:
            m = six.create_unbound_method(f, target_class)
            setattr(target_class, methname, m)

    if bind_target_class:
        target.__class__ = target_class
    if bind_to_meta:
        setattr(meta, target_class.__name__, target_class)
    return dict((k, len(serv_methods[k]) == 1) for k in serv_methods)
Exemplo n.º 8
0
    def __call__(self):
        """ Return the original listener method, or None if it no longer exists.
        """
        obj = self.obj
        if obj is None:
            # Unbound method.
            return six.create_unbound_method(self.func, self.cls)
        else:
            objc = obj()
            if objc is None:
                # Bound method whose object has been garbage collected.
                return

            return six.create_bound_method(self.func, objc)
Exemplo n.º 9
0
    def _create_go_to_method(cls, path, class_name=None):
        go_to_method = Navigation.GoToMethodFactory(path, class_name)
        inst_method = six.create_unbound_method(go_to_method, Navigation)

        # TODO(e0ne): remove python2 support once all integration jobs
        # will be switched to python3.
        if six.PY3:
            def _go_to_page(self, path):
                return Navigation._go_to_page(self, path)

            wrapped_go_to = functools.partialmethod(_go_to_page, path)
            setattr(Navigation, inst_method.name, wrapped_go_to)
        else:
            setattr(Navigation, inst_method.name, inst_method)
Exemplo n.º 10
0
    def __call__(self):
        """ Return the original listener method, or None if it no longer exists.
        """
        obj = self.obj
        if obj is None:
            # Unbound method.
            return six.create_unbound_method(self.func, self.cls)
        else:
            objc = obj()
            if objc is None:
                # Bound method whose object has been garbage collected.
                return

            return six.create_bound_method(self.func, objc)
Exemplo n.º 11
0
    def _create_go_to_method(cls, path, class_name=None):
        go_to_method = Navigation.GoToMethodFactory(path, class_name)
        inst_method = six.create_unbound_method(go_to_method, Navigation)

        # TODO(e0ne): remove python2 support once all integration jobs
        # will be switched to python3.
        if six.PY3:

            def _go_to_page(self, path):
                return Navigation._go_to_page(self, path)

            wrapped_go_to = functools.partialmethod(_go_to_page, path)
            setattr(Navigation, inst_method.name, wrapped_go_to)
        else:
            setattr(Navigation, inst_method.name, inst_method)
Exemplo n.º 12
0
    def get_snmp_handler(mcs, script, metric, rule):
        """
        Generate SNMP handler for @metrics
        """
        def f(self, metrics):
            self.schedule_snmp_oids(rule, metric, metrics)

        fn = mcs.get_snmp_handler_name(metric)
        f.mt_has_script = None
        f.mt_has_capability = "SNMP"
        f.mt_matcher = None
        f.mt_access = "S"
        f.mt_volatile = False
        setattr(script, fn, six.create_unbound_method(f, script))
        ff = getattr(script, fn)
        ff.__func__.__name__ = fn
        return ff
Exemplo n.º 13
0
    def extension_add_method(self, object, name, function):
        """extension_add_method(object, name, function)

        Add an X extension module method.  OBJECT is the type of
        object to add the function to, a string from this list:

            display
            resource
            drawable
            window
            pixmap
            fontable
            font
            gc
            colormap
            cursor

        NAME is the name of the method, a string.  FUNCTION is a
        normal function whose first argument is a 'self'.
        """

        if object == 'display':
            if hasattr(self, name):
                raise AssertionError(
                    'attempting to replace display method: %s' % name)

            self.display_extension_methods[name] = function

        else:
            class_list = (object, ) + _resource_hierarchy.get(object, ())
            for class_name in class_list:
                cls = _resource_baseclasses[class_name]
                if hasattr(cls, name):
                    raise AssertionError(
                        'attempting to replace %s method: %s' %
                        (class_name, name))

                method = create_unbound_method(function, cls)

                # Maybe should check extension overrides too
                try:
                    self.class_extension_dicts[class_name][name] = method
                except KeyError:
                    self.class_extension_dicts[class_name] = {name: method}
Exemplo n.º 14
0
    def extension_add_method(self, object, name, function):
        """extension_add_method(object, name, function)

        Add an X extension module method.  OBJECT is the type of
        object to add the function to, a string from this list:

            display
            resource
            drawable
            window
            pixmap
            fontable
            font
            gc
            colormap
            cursor

        NAME is the name of the method, a string.  FUNCTION is a
        normal function whose first argument is a 'self'.
        """

        if object == 'display':
            if hasattr(self, name):
                raise AssertionError('attempting to replace display method: %s' % name)

            self.display_extension_methods[name] = function

        else:
            class_list = (object, ) + _resource_hierarchy.get(object, ())
            for class_name in class_list:
                cls = _resource_baseclasses[class_name]
                if hasattr(cls, name):
                    raise AssertionError('attempting to replace %s method: %s' % (class_name, name))

                method = create_unbound_method(function, cls)

                # Maybe should check extension overrides too
                try:
                    self.class_extension_dicts[class_name][name] = method
                except KeyError:
                    self.class_extension_dicts[class_name] = { name: method }
Exemplo n.º 15
0
    def define_network(self,
                       hidden_layers=[50, 30],
                       dropout_at=[1],
                       keep_prob=0.8):
        import tensorflow as tf
        import tensorflow.contrib.slim as slim

        name = 'FullyConnected{}'.format('x'.join(map(str, hidden_layers)))

        network = type(
            self.__class__.__name__ + name, (NeuralNetwork, ), {
                'x_shape': lambda _: [None, self.num_features],
                'y_shape': lambda _: [None, self.num_classes]
            })

        network.x_shape = property(network.x_shape)
        network.y_shape = property(network.y_shape)

        def rebuild_model(o, X, reuse=None):
            sizes = [self.num_features
                     ] + list(hidden_layers) + [self.num_classes]
            layers = [X]
            for i, activation in enumerate([tf.nn.relu
                                            for _ in hidden_layers] + [None]):
                out = layers[i]
                if i - 1 in dropout_at:
                    out = slim.dropout(out, keep_prob, is_training=o.is_train)
                out = slim.fully_connected(out,
                                           sizes[i + 1],
                                           reuse=reuse,
                                           scope=o.name + '/L{}'.format(i),
                                           activation_fn=activation)
                layers.append(out)
            return layers

        network.rebuild_model = six.create_unbound_method(
            rebuild_model, network)

        return network
Exemplo n.º 16
0
def as_method(func):
    return six.create_unbound_method(func, PelicanHTMLTranslator)
Exemplo n.º 17
0
 def _create_go_to_method(cls, path, class_name=None):
     go_to_method = Navigation.GoToMethodFactory(path, class_name)
     inst_method = six.create_unbound_method(go_to_method, Navigation)
     setattr(Navigation, inst_method.name, inst_method)
Exemplo n.º 18
0
def create_tango_class(server, obj, tango_class_name=None, member_filter=None):
    slog = server.server_instance.replace("/", ".")
    log = logging.getLogger("tango.Server." + slog)

    obj_klass = obj.__class__
    obj_klass_name = obj_klass.__name__

    if tango_class_name is None:
        tango_class_name = obj_klass_name

    class DeviceDispatcher(Device):

        TangoClassName = tango_class_name

        def __init__(self, tango_class_obj, name):
            tango_object = server.get_tango_object(name)
            self.__tango_object = weakref.ref(tango_object)
            Device.__init__(self, tango_class_obj, name)

        def init_device(self):
            Device.init_device(self)
            self.set_state(DevState.ON)

        @property
        def _tango_object(self):
            return self.__tango_object()

        @property
        def _object(self):
            return self._tango_object._object

    DeviceDispatcher.__name__ = tango_class_name
    DeviceDispatcher.TangoClassName = tango_class_name
    DeviceDispatcherClass = DeviceDispatcher.TangoClassClass

    for name in dir(obj):
        if name.startswith("_"):
            continue
        log.debug("inspecting %s.%s", obj_klass_name, name)
        try:
            member = getattr(obj, name)
        except:
            log.info("failed to inspect member '%s.%s'", obj_klass_name, name)
            log.debug("Details:", exc_info=1)
        if inspect.isclass(member) or inspect.ismodule(member):
            continue
        if member_filter and not member_filter(obj, tango_class_name, name,
                                               member):
            log.debug("filtered out %s from %s", name, tango_class_name)
            continue
        if inspect.isroutine(member):
            # try to find out if there are any parameters
            in_type = CmdArgType.DevEncoded
            out_type = CmdArgType.DevEncoded
            try:
                arg_spec = inspect_getargspec(member)
                if not arg_spec.args:
                    in_type = CmdArgType.DevVoid
            except TypeError:
                pass

            if in_type == CmdArgType.DevVoid:

                def _command(dev, func_name=None):
                    obj = dev._object
                    f = getattr(obj, func_name)
                    result = server.worker.execute(f)
                    return server.dumps(result)
            else:

                def _command(dev, param, func_name=None):
                    obj = dev._object
                    args, kwargs = loads(*param)
                    f = getattr(obj, func_name)
                    result = server.worker.execute(f, *args, **kwargs)
                    return server.dumps(result)

            cmd = functools.partial(_command, func_name=name)
            cmd.__name__ = name
            doc = member.__doc__
            if doc is None:
                doc = ""
            cmd.__doc__ = doc
            cmd = six.create_unbound_method(cmd, DeviceDispatcher)
            setattr(DeviceDispatcher, name, cmd)
            DeviceDispatcherClass.cmd_list[name] = \
                [[in_type, doc], [out_type, ""]]
        else:
            read_only = False
            if hasattr(obj_klass, name):
                kmember = getattr(obj_klass, name)
                if inspect.isdatadescriptor(kmember):
                    if kmember.fset is None:
                        read_only = True
                else:
                    continue
            value = member
            dtype, fmt, x, y = __to_tango_type_fmt(value)
            if dtype is None or dtype == CmdArgType.DevEncoded:
                dtype = CmdArgType.DevEncoded
                fmt = AttrDataFormat.SCALAR

                def read(dev, attr):
                    name = attr.get_name()
                    value = server.worker.execute(getattr, dev._object, name)
                    attr.set_value(*server.dumps(value))

                def write(dev, attr):
                    name = attr.get_name()
                    value = attr.get_write_value()
                    value = loads(*value)
                    server.worker.execute(setattr, dev._object, name, value)
            else:

                def read(dev, attr):
                    name = attr.get_name()
                    value = server.worker.execute(getattr, dev._object, name)
                    attr.set_value(value)

                def write(dev, attr):
                    name = attr.get_name()
                    value = attr.get_write_value()
                    server.worker.execute(setattr, dev._object, name, value)

            read.__name__ = "_read_" + name
            setattr(DeviceDispatcher, read.__name__, read)

            pars = dict(name=name,
                        dtype=dtype,
                        dformat=fmt,
                        max_dim_x=x,
                        max_dim_y=y,
                        fget=read)
            if not read_only:
                write.__name__ = "_write_" + name
                pars['fset'] = write
                setattr(DeviceDispatcher, write.__name__, write)
            attr_data = AttrData.from_dict(pars)
            DeviceDispatcherClass.attr_list[name] = attr_data
    return DeviceDispatcher
Exemplo n.º 19
0
 def _create_go_to_method(cls, path, class_name=None):
     go_to_method = Navigation.GoToMethodFactory(path, class_name)
     inst_method = six.create_unbound_method(go_to_method, Navigation)
     setattr(Navigation, inst_method.name, inst_method)