Exemplo n.º 1
0
        def template(engine,
                     value,
                     type_,
                     exclude_properties=None,
                     default_type=None,
                     version_spec=None):
            object_store = helpers.get_object_store()
            passkey = None
            if not default_type:
                default_type = type_
            murano_class = type_.type
            if value is None:
                return None
            if isinstance(value, dsl_types.MuranoObject):
                obj = value
            elif isinstance(value, dsl_types.MuranoObjectInterface):
                obj = value.object
            elif isinstance(value, utils.MappingType):
                passkey = utils.create_marker('<Contract Passkey>')
                if exclude_properties:
                    parsed = helpers.parse_object_definition(
                        value, calling_type, context)
                    props = dsl.to_mutable(parsed['properties'], engine)
                    for p in exclude_properties:
                        helpers.patch_dict(props, p, passkey)
                    parsed['properties'] = props
                    value = helpers.assemble_object_definition(parsed)
                with helpers.thread_local_attribute(
                        constants.TL_CONTRACT_PASSKEY, passkey):
                    with helpers.thread_local_attribute(
                            constants.TL_OBJECTS_DRY_RUN, True):
                        obj = object_store.load(value,
                                                owner,
                                                context=context,
                                                default_type=default_type,
                                                scope_type=calling_type)
            else:
                raise exceptions.ContractViolationException(
                    'Value {0} cannot be represented as class {1}'.format(
                        format_scalar(value), type_))
            if not helpers.is_instance_of(
                    obj, murano_class.name, version_spec
                    or helpers.get_type(root_context)):
                raise exceptions.ContractViolationException(
                    'Object of type {0} is not compatible with '
                    'requested type {1}'.format(obj.type.name, type_))

            with helpers.thread_local_attribute(constants.TL_CONTRACT_PASSKEY,
                                                passkey):
                result = serializer.serialize(obj.real_this,
                                              object_store.executor,
                                              dsl_types.DumpTypes.Mixed)
                if exclude_properties:
                    for p in exclude_properties:
                        helpers.patch_dict(result, p, utils.NO_VALUE)
                return result
Exemplo n.º 2
0
def evaluate_constant(expr, engine, context):
    """Evaluate yaql expression into constant value if possible"""
    if isinstance(expr, expressions.Constant):
        return expr.value
    context = context.create_child_context()
    trap = utils.create_marker('trap')
    context['$'] = trap

    @specs.parameter('name', yaqltypes.StringConstant())
    @specs.name('#get_context_data')
    def get_context_data(name, context):
        res = context[name]
        if res is trap:
            raise yaql_exceptions.ResolutionError()
        return res

    context.register_function(get_context_data)

    try:
        return expressions.Statement(expr, engine).evaluate(context=context)
    except yaql_exceptions.YaqlException:
        return None
Exemplo n.º 3
0
 def transform(self):
     object_store = helpers.get_object_store()
     if self.value is None:
         return None
     if isinstance(self.value, dsl_types.MuranoObject):
         obj = self.value
     elif isinstance(self.value, dsl_types.MuranoObjectInterface):
         obj = self.value.object
     elif isinstance(self.value, utils.MappingType):
         passkey = utils.create_marker('<Contract Passkey>')
         if self.exclude_properties:
             parsed = helpers.parse_object_definition(
                 self.value, self.calling_type, self.context)
             props = dsl.to_mutable(parsed['properties'], self.engine)
             for p in self.exclude_properties:
                 helpers.patch_dict(props, p, passkey)
             parsed['properties'] = props
             value = helpers.assemble_object_definition(parsed)
         else:
             value = self.value
         with helpers.thread_local_attribute(constants.TL_CONTRACT_PASSKEY,
                                             passkey):
             with helpers.thread_local_attribute(
                     constants.TL_OBJECTS_DRY_RUN, True):
                 obj = object_store.load(value,
                                         self.owner,
                                         context=self.context,
                                         default_type=self.default_type,
                                         scope_type=self.calling_type)
                 obj.__passkey__ = passkey
     else:
         raise exceptions.ContractViolationException(
             'Value {0} cannot be represented as class {1}'.format(
                 helpers.format_scalar(self.value), self.type))
     self.value = obj
     return self.validate()
Exemplo n.º 4
0
import inspect
import os.path

import six
from yaql.language import exceptions as yaql_exc
from yaql.language import expressions as yaql_expressions
from yaql.language import utils
from yaql.language import yaqltypes

from murano.dsl import constants
from murano.dsl import dsl_types
from murano.dsl import helpers


NO_VALUE = utils.create_marker('NO_VALUE')


def name(dsl_name):
    def wrapper(cls):
        cls.__murano_name = dsl_name
        return cls
    return wrapper


class MuranoType(yaqltypes.PythonType):
    def __init__(self, murano_class, nullable=False, version_spec=None):
        self.murano_class = murano_class
        self.version_spec = version_spec
        super(MuranoType, self).__init__(
            (dsl_types.MuranoObject, MuranoObjectInterface), nullable)
Exemplo n.º 5
0
import os.path

import eventlet
import six
from yaql.language import expressions as yaql_expressions
from yaql.language import specs
from yaql.language import utils
from yaql.language import yaqltypes
from yaql import yaql_interface

from murano.dsl import constants
from murano.dsl import dsl_types
from murano.dsl import helpers


NO_VALUE = utils.create_marker('NO_VALUE')


def name(dsl_name):
    def wrapper(cls):
        cls.__murano_name = dsl_name
        return cls
    return wrapper


class MuranoObjectParameter(yaqltypes.PythonType):
    def __init__(self, murano_class=None, nullable=False, version_spec=None,
                 decorate=True):
        self.murano_class = murano_class
        self.version_spec = version_spec
        self.decorate = decorate
Exemplo n.º 6
0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import inspect

import six

from yaql.language import exceptions
from yaql.language import utils
from yaql.language import yaqltypes

NO_DEFAULT = utils.create_marker('<NoValue>')


class ParameterDefinition(object):
    __slots__ = ('value_type', 'name', 'position', 'default', 'alias')

    def __init__(self, name, value_type=None, position=None, alias=None,
                 default=None):
        self.value_type = value_type
        self.name = name
        self.position = position
        self.default = default
        self.alias = alias

    def __repr__(self):
        return '{0} => position={1} value_type={2} default={3}'.format(
Exemplo n.º 7
0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import inspect

import six

from yaql.language import exceptions
from yaql.language import utils
from yaql.language import yaqltypes

NO_DEFAULT = utils.create_marker('<NoValue>')


class ParameterDefinition(object):
    __slots__ = ('value_type', 'name', 'position', 'default', 'alias')

    def __init__(self,
                 name,
                 value_type=None,
                 position=None,
                 alias=None,
                 default=None):
        self.value_type = value_type
        self.name = name
        self.position = position
        self.default = default