Пример #1
0
class ResourceManager(object):
    def __init__(self, context):
        murano_class = helpers.get_type(helpers.get_caller_context(context))
        self._package = murano_class.package

    @staticmethod
    @specs.parameter('owner', dsl.MuranoTypeParameter(nullable=True))
    @specs.inject('receiver', yaqltypes.Receiver())
    def string(receiver, name, owner=None):
        path = ResourceManager._get_package(owner, receiver).get_resource(name)
        with open(path) as file:
            return file.read()

    @classmethod
    @specs.parameter('owner', dsl.MuranoTypeParameter(nullable=True))
    @specs.inject('receiver', yaqltypes.Receiver())
    def json(cls, receiver, name, owner=None):
        return jsonlib.loads(cls.string(receiver, name, owner))

    @classmethod
    @specs.parameter('owner', dsl.MuranoTypeParameter(nullable=True))
    @specs.inject('receiver', yaqltypes.Receiver())
    def yaml(cls, receiver, name, owner=None):
        return yamllib.load(cls.string(receiver, name, owner),
                            Loader=yaml_loader)

    @staticmethod
    def _get_package(owner, receiver):
        if owner is None:
            if isinstance(receiver, dsl_types.MuranoObjectInterface):
                return receiver.extension._package
            murano_class = helpers.get_type(helpers.get_caller_context())
        else:
            murano_class = owner.type
        return murano_class.package
Пример #2
0
        class PythonClass(object):
            @staticmethod
            @specs.parameter('arg', yaqltypes.Integer())
            @specs.inject('receiver', yaqltypes.Receiver())
            def static_python_method(arg, receiver):
                if isinstance(receiver, dsl_types.MuranoObjectInterface):
                    return 3 * arg
                return 7 * arg

            @classmethod
            @specs.inject('receiver', yaqltypes.Receiver())
            def classmethod_python_method(cls, arg, receiver):
                if isinstance(receiver, dsl_types.MuranoObjectInterface):
                    return cls.__name__.upper() + str(arg)
                return cls.__name__ + str(arg)
Пример #3
0
class ResourceManager(object):
    def __init__(self, context):
        murano_class = helpers.get_type(helpers.get_caller_context(context))
        self._package = murano_class.package

    @staticmethod
    @specs.parameter('owner', dsl.MuranoTypeParameter(nullable=True))
    @specs.inject('receiver', yaqltypes.Receiver())
    @specs.meta(constants.META_NO_TRACE, True)
    def string(receiver, name, owner=None, binary=False):
        path = ResourceManager._get_package(owner, receiver).get_resource(name)
        mode = 'rb' if binary else 'rU'
        with open(path, mode) as file:
            return file.read()

    @classmethod
    @specs.parameter('owner', dsl.MuranoTypeParameter(nullable=True))
    @specs.inject('receiver', yaqltypes.Receiver())
    @specs.meta(constants.META_NO_TRACE, True)
    def json(cls, receiver, name, owner=None):
        return jsonlib.loads(cls.string(receiver, name, owner))

    @classmethod
    @specs.parameter('owner', dsl.MuranoTypeParameter(nullable=True))
    @specs.inject('receiver', yaqltypes.Receiver())
    @specs.meta(constants.META_NO_TRACE, True)
    def yaml(cls, receiver, name, owner=None):
        # NOTE(kzaitsev, Sam Pilla) Bandit will raise an issue here,
        # because it thinks that we're using an unsafe yaml.load.
        # However we're passing a SafeLoader here
        # (see definition of `yaml_loader` in this file; L27-30)
        # so a `nosec` was added to ignore the false positive report.
        return yamllib.load(  # nosec
            cls.string(receiver, name, owner), Loader=yaml_loader)

    @staticmethod
    def _get_package(owner, receiver):
        if owner is None:
            if isinstance(receiver, dsl_types.MuranoObjectInterface):
                return receiver.extension._package
            murano_class = helpers.get_type(helpers.get_caller_context())
        else:
            murano_class = owner.type
        return murano_class.package