Пример #1
0
    def _build_parent_remappings(self):
        """Remaps class parents.

        In case of multiple inheritance class may indirectly get several
        versions of the same class. It is reasonable to try to replace them
        with single version to avoid conflicts. We can do that when within
        versions that satisfy our class package requirements.
        But in order to merge several classes that are not our parents but
        grand parents we will need to modify classes that may be used
        somewhere else (with another set of requirements). We cannot do this.
        So instead we build translation table that will tell which ancestor
        class need to be replaced with which so that we minimize number of
        versions used for single class (or technically packages since version
        is a package attribute). For translation table to work there should
        be a method that returns all class virtual ancestors so that everybody
        will see them instead of accessing class parents directly and getting
        declared ancestors.
        """
        result = {}

        aggregation = {
            self.package.name: {(
                self.package,
                semantic_version.Spec('==' + str(self.package.version))
            )}
        }
        for cls, parent in helpers.traverse(
                ((self, parent) for parent in self._parents),
                lambda (c, p): ((p, anc) for anc in p.declared_parents)):
            if cls.package != parent.package:
                requirement = cls.package.requirements[parent.package.name]
                aggregation.setdefault(parent.package.name, set()).add(
                    (parent.package, requirement))
Пример #2
0
    def _build_parent_remappings(self):
        """Remaps class parents.

        In case of multiple inheritance class may indirectly get several
        versions of the same class. It is reasonable to try to replace them
        with single version to avoid conflicts. We can do that when within
        versions that satisfy our class package requirements.
        But in order to merge several classes that are not our parents but
        grand parents we will need to modify classes that may be used
        somewhere else (with another set of requirements). We cannot do this.
        So instead we build translation table that will tell which ancestor
        class need to be replaced with which so that we minimize number of
        versions used for single class (or technically packages since version
        is a package attribute). For translation table to work there should
        be a method that returns all class virtual ancestors so that everybody
        will see them instead of accessing class parents directly and getting
        declared ancestors.
        """
        result = {}

        aggregation = {
            self.package.name: {(
                self.package,
                semantic_version.Spec('==' + str(self.package.version))
            )}
        }
        for cls, parent in helpers.traverse(
                ((self, parent) for parent in self._parents),
                lambda cp: ((cp[1], anc) for anc in cp[1].declared_parents)):
            if cls.package != parent.package:
                requirement = cls.package.requirements[parent.package.name]
                aggregation.setdefault(parent.package.name, set()).add(
                    (parent.package, requirement))

        package_bindings = {}
        for versions in six.itervalues(aggregation):
            mappings = self._remap_package(versions)
            package_bindings.update(mappings)

        for cls in helpers.traverse(
                self.declared_parents, lambda c: c.declared_parents):
            if cls.package in package_bindings:
                package2 = package_bindings[cls.package]
                cls2 = package2.classes[cls.name]
                result[cls] = cls2
        return result
Пример #3
0
def weigh_type_hierarchy(cls):
    """Weighs classes in type hierarchy by their distance from the root

    :param cls: root of hierarchy
    :return: dictionary that has class name as keys and distance from the root
             a values. Root class has always a distance of 0. If the class
             (or different versions of that class) is achievable through
             several paths the shortest distance is used.
    """

    result = {}
    for c, w in helpers.traverse(
            [(cls, 0)], lambda t: six.moves.map(
                lambda p: (p, t[1] + 1), t[0].parents)):
        result.setdefault(c.name, w)
    return result
Пример #4
0
 def find_single_property(self, name):
     result = None
     parents = None
     gen = helpers.traverse(self)
     while True:
         try:
             mc = gen.send(parents)
             if name in mc.properties:
                 if result and result != mc:
                     raise exceptions.AmbiguousPropertyNameError(name)
                 result = mc
                 parents = []
             else:
                 parents = mc.parents(self)
         except StopIteration:
             return result
Пример #5
0
 def find_single_property(self, name):
     result = None
     parents = None
     gen = helpers.traverse(self)
     while True:
         try:
             mc = gen.send(parents)
             if name in mc.properties:
                 if result and result != mc:
                     raise exceptions.AmbiguousPropertyNameError(name)
                 result = mc
                 parents = []
             else:
                 parents = mc.parents(self)
         except StopIteration:
             return result
Пример #6
0
 def cast(self, cls):
     for p in helpers.traverse(self, lambda t: t.__parents.values()):
         if p.type is cls:
             return p
     raise TypeError('Cannot cast {0} to {1}'.format(self.type, cls))
Пример #7
0
 def ancestors(self):
     for c in helpers.traverse(self, lambda t: t.parents):
         if c is not self:
             yield c
Пример #8
0
 def ancestors(self):
     for c in helpers.traverse(self, lambda t: t.parents(self)):
         yield c
Пример #9
0
 def ancestors(self):
     return helpers.traverse(self, lambda t: t.declared_parents)
Пример #10
0
 def ancestors(self):
     return helpers.traverse(self, lambda t: t.declared_parents)
Пример #11
0
 def _list_properties(self, name):
     for p in helpers.traverse(self.real_this,
                               lambda t: t._parents.values()):
         if name in p.type.properties:
             yield p.type.properties[name]
Пример #12
0
 def cast(self, cls):
     for p in helpers.traverse(self, lambda t: t._parents.values()):
         if p.type == cls:
             return p
     raise TypeError('Cannot cast {0} to {1}'.format(self.type, cls))
Пример #13
0
 def ancestors(self):
     for c in helpers.traverse(self, lambda t: t.parents(self)):
         yield c
Пример #14
0
class MuranoClass(dsl_types.MuranoClass):
    def __init__(self, ns_resolver, name, package, parents=None):
        self._package = weakref.ref(package)
        self._methods = {}
        self._namespace_resolver = ns_resolver
        self._name = name
        self._properties = {}
        self._config = {}
        if self._name == constants.CORE_LIBRARY_OBJECT:
            self._parents = []
        else:
            self._parents = parents or [
                package.find_class(constants.CORE_LIBRARY_OBJECT)
            ]
        self._context = None
        self._parent_mappings = self._build_parent_remappings()

    @classmethod
    def create(cls, data, package, name=None):
        namespaces = data.get('Namespaces') or {}
        ns_resolver = namespace_resolver.NamespaceResolver(namespaces)

        if not name:
            name = ns_resolver.resolve_name(data['Name'])

        parent_class_names = data.get('Extends')
        parent_classes = []
        if parent_class_names:
            if not utils.is_sequence(parent_class_names):
                parent_class_names = [parent_class_names]
            for parent_name in parent_class_names:
                full_name = ns_resolver.resolve_name(parent_name)
                parent_classes.append(package.find_class(full_name))

        type_obj = cls(ns_resolver, name, package, parent_classes)

        properties = data.get('Properties') or {}
        for property_name, property_spec in properties.iteritems():
            spec = typespec.PropertySpec(property_spec, type_obj)
            type_obj.add_property(property_name, spec)

        methods = data.get('Methods') or data.get('Workflow') or {}

        method_mappings = {'initialize': '.init', 'destroy': '.destroy'}

        for method_name, payload in methods.iteritems():
            type_obj.add_method(method_mappings.get(method_name, method_name),
                                payload)

        return type_obj

    @property
    def name(self):
        return self._name

    @property
    def package(self):
        return self._package()

    @property
    def namespace_resolver(self):
        return self._namespace_resolver

    @property
    def declared_parents(self):
        return self._parents

    @property
    def methods(self):
        return self._methods

    @property
    def parent_mappings(self):
        return self._parent_mappings

    def extend_with_class(self, cls):
        ctor = yaql_integration.get_class_factory_definition(cls, self)
        self.add_method('__init__', ctor)

    def get_method(self, name):
        return self._methods.get(name)

    def add_method(self, name, payload):
        method = murano_method.MuranoMethod(self, name, payload)
        self._methods[name] = method
        self._context = None
        return method

    @property
    def properties(self):
        return self._properties.keys()

    def add_property(self, name, property_typespec):
        if not isinstance(property_typespec, typespec.PropertySpec):
            raise TypeError('property_typespec')
        self._properties[name] = property_typespec

    def get_property(self, name):
        return self._properties[name]

    def _find_method_chains(self, name, origin):
        queue = collections.deque([(self, ())])
        while queue:
            cls, path = queue.popleft()
            segment = (cls.methods[name], ) if name in cls.methods else ()
            leaf = True
            for p in cls.parents(origin):
                leaf = False
                queue.append((p, path + segment))
            if leaf:
                path = path + segment
                if path:
                    yield path

    def find_single_method(self, name):
        chains = sorted(self._find_method_chains(name, self),
                        key=lambda t: len(t))
        result = []
        for i in range(len(chains)):
            if chains[i][0] in result:
                continue
            add = True
            for j in range(i + 1, len(chains)):
                common = 0
                if not add:
                    break
                for p in range(len(chains[i])):
                    if chains[i][-p - 1] is chains[j][-p - 1]:
                        common += 1
                    else:
                        break
                if common == len(chains[i]):
                    add = False
                    break
            if add:
                result.append(chains[i][0])
        if len(result) < 1:
            raise exceptions.NoMethodFound(name)
        elif len(result) > 1:
            raise exceptions.AmbiguousMethodName(name)
        return result[0]

    def find_methods(self, predicate):
        result = []
        for c in self.ancestors():
            for method in c.methods.itervalues():
                if predicate(method) and method not in result:
                    result.append(method)
        return result

    def _iterate_unique_methods(self):
        names = set()
        for c in self.ancestors():
            names.update(c.methods.keys())
        for name in names:
            try:
                yield self.find_single_method(name)
            except exceptions.AmbiguousMethodName as e:

                def func(*args, **kwargs):
                    raise e

                yield murano_method.MuranoMethod(self, name, func)

    def find_property(self, name):
        result = []
        for mc in self.ancestors():
            if name in mc.properties and mc not in result:
                result.append(mc)
        return result

    def find_single_property(self, name):
        result = None
        parents = None
        gen = helpers.traverse(self)
        while True:
            try:
                mc = gen.send(parents)
                if name in mc.properties:
                    if result and result != mc:
                        raise exceptions.AmbiguousPropertyNameError(name)
                    result = mc
                    parents = []
                else:
                    parents = mc.parents(self)
            except StopIteration:
                return result

    def invoke(self, name, executor, this, args, kwargs, context=None):
        method = self.find_single_method(name)
        return method.invoke(executor, this, args, kwargs, context)

    def is_compatible(self, obj):
        if isinstance(obj,
                      (murano_object.MuranoObject, dsl.MuranoObjectInterface)):
            obj = obj.type
        return any(cls is self for cls in obj.ancestors())

    def new(self, owner, object_store, **kwargs):
        obj = murano_object.MuranoObject(self, owner, object_store, **kwargs)

        def initializer(__context, **params):
            if __context is None:
                __context = object_store.executor.create_object_context(obj)
            init_context = __context.create_child_context()
            init_context[constants.CTX_ALLOW_PROPERTY_WRITES] = True
            obj.initialize(init_context, object_store, params)
            return obj

        initializer.object = obj
        return initializer

    def __repr__(self):
        return 'MuranoClass({0}/{1})'.format(self.name, self.version)

    @property
    def version(self):
        return self.package.version

    def _build_parent_remappings(self):
        """Remaps class parents.

        In case of multiple inheritance class may indirectly get several
        versions of the same class. It is reasonable to try to replace them
        with single version to avoid conflicts. We can do that when within
        versions that satisfy our class package requirements.
        But in order to merge several classes that are not our parents but
        grand parents we will need to modify classes that may be used
        somewhere else (with another set of requirements). We cannot do this.
        So instead we build translation table that will tell which ancestor
        class need to be replaced with which so that we minimize number of
        versions used for single class (or technically packages since version
        is a package attribute). For translation table to work there should
        be a method that returns all class virtual ancestors so that everybody
        will see them instead of accessing class parents directly and getting
        declared ancestors.
        """
        result = {}

        aggregation = {
            self.package.name:
            {(self.package,
              semantic_version.Spec('==' + str(self.package.version)))}
        }
        for cls, parent in helpers.traverse(
            ((self, parent) for parent in self._parents), lambda (c, p): (
                (p, anc) for anc in p.declared_parents)):
            if cls.package != parent.package:
                requirement = cls.package.requirements[parent.package.name]
                aggregation.setdefault(parent.package.name, set()).add(
                    (parent.package, requirement))

        package_bindings = {}
        for versions in aggregation.itervalues():
            mappings = self._remap_package(versions)
            package_bindings.update(mappings)

        for cls in helpers.traverse(self.declared_parents,
                                    lambda c: c.declared_parents):
            if cls.package in package_bindings:
                package2 = package_bindings[cls.package]
                cls2 = package2.classes[cls.name]
                result[cls] = cls2
        return result