Exemplo n.º 1
0
    def load(self, value, owner, default_type=None,
             scope_type=None, context=None, **kwargs):
        parsed = helpers.parse_object_definition(value, scope_type, context)
        if not parsed:
            raise ValueError('Invalid object representation format')

        if owner is self._root_owner:
            self._initializing = True

        class_obj = parsed['type'] or default_type
        if not class_obj:
            raise ValueError(
                'Invalid object representation: '
                'no type information was provided')
        if isinstance(class_obj, dsl_types.MuranoTypeReference):
            class_obj = class_obj.type
        object_id = parsed['id']
        is_tmp_object = (object_id is None and
                         owner is not self._root_owner and
                         self._initializing)
        obj = None if object_id is None else self.get(
            object_id, self._keep_ids)
        if not obj:
            if is_tmp_object or helpers.is_objects_dry_run_mode():
                mo_type = murano_object.MuranoObject
            else:
                mo_type = murano_object.RecyclableMuranoObject
            obj = mo_type(
                class_obj, owner,
                name=parsed['name'],
                metadata=parsed['metadata'],
                object_id=object_id if self._keep_ids else None)
            obj.load_dependencies(parsed['dependencies'])
            if parsed['destroyed']:
                obj.mark_destroyed()
            self.put(obj, object_id or obj.object_id)

            system_value = ObjectStore._get_designer_attributes(
                parsed['extra'])
            self._designer_attributes_store[object_id] = system_value

        if context is None:
            context = self.executor.create_object_context(obj)

        def run_initialize():
            self._initializers.extend(
                obj.initialize(context, parsed['properties']))

        run_initialize()
        if owner is self._root_owner:
            self._initializing = False
            run_initialize()

        if owner is self._root_owner:
            with helpers.with_object_store(self.parent_store):
                for fn in self._initializers:
                    fn()

        return obj
Exemplo n.º 2
0
    def initialize(self, context, params, used_names=None):
        context = context.create_child_context()
        context[constants.CTX_ALLOW_PROPERTY_WRITES] = True
        object_store = helpers.get_object_store()
        for property_name in self.type.properties:
            spec = self.type.properties[property_name]
            if spec.usage == dsl_types.PropertyUsages.Config:
                if property_name in self._config:
                    property_value = self._config[property_name]
                else:
                    property_value = dsl.NO_VALUE
                self.set_property(property_name,
                                  property_value,
                                  dry_run=self._initialized)

        init = self.type.methods.get('.init')
        used_names = used_names or set()
        names = set(self.type.properties)
        if init:
            names.update(init.arguments_scheme.keys())
        last_errors = len(names)
        init_args = {}
        while True:
            errors = 0
            for property_name in names:
                if init and property_name in init.arguments_scheme:
                    spec = init.arguments_scheme[property_name]
                    is_init_arg = True
                else:
                    spec = self.type.properties[property_name]
                    is_init_arg = False

                if property_name in used_names:
                    continue
                if spec.usage in (dsl_types.PropertyUsages.Config,
                                  dsl_types.PropertyUsages.Static):
                    used_names.add(property_name)
                    continue
                if spec.usage == dsl_types.PropertyUsages.Runtime:
                    if not spec.has_default:
                        used_names.add(property_name)
                        continue
                    property_value = dsl.NO_VALUE
                else:
                    property_value = params.get(property_name, dsl.NO_VALUE)
                try:
                    if is_init_arg:
                        init_args[property_name] = property_value
                    else:
                        self.set_property(property_name,
                                          property_value,
                                          context,
                                          dry_run=self._initialized)
                    used_names.add(property_name)
                except exceptions.UninitializedPropertyAccessError:
                    errors += 1
                except exceptions.ContractViolationException:
                    if spec.usage != dsl_types.PropertyUsages.Runtime:
                        raise
            if not errors:
                break
            if errors >= last_errors:
                raise exceptions.CircularExpressionDependenciesError()
            last_errors = errors

        if (not object_store.initializing and self._extension is None
                and not self._initialized and not self._destroyed
                and not helpers.is_objects_dry_run_mode()):
            method = self.type.methods.get('__init__')
            if method:
                filtered_params = yaql_integration.filter_parameters(
                    method.body, **params)
                yield lambda: method.invoke(self, filtered_params[0],
                                            filtered_params[1], context)

        for parent in self._parents.values():
            for t in parent.initialize(context, params, used_names):
                yield t

        def run_init():
            if init:
                context[constants.CTX_ARGUMENT_OWNER] = self.real_this
                init.invoke(self.real_this, (), init_args,
                            context.create_child_context())
            self._initialized = True

        if (not object_store.initializing
                and not helpers.is_objects_dry_run_mode()
                and not self._initialized and not self._destroyed):
            yield run_init