deb-murano/murano/engine/system/yaql_functions.py
Stan Lagun 16030aeec8 yaql context versioning
This commit reworks mechanism how context chain
is built at each point. The goal was to make
entire chain be dependent on format version
number. This would allow for example to use
yaql 1.0 legacy mode context for MuranoPL/1.0
and not to use it for 2.0.

Not the entire chain is built on demand up to the
root instead of attaching to existed parent context
as it was before. DSL host (engine) is no more
required to have custom MuranoDslExecutor implementation
but a new ContextManager interface that is used
to control contexts on each layer. Engine uses
this interface to register system classes and
engine level yaql functions. Also a ContextManager
is a foundation for method mocking because now
all MuranoPL methods are yaql functions stored
in context and with ContextManager it is possible
to inject/replace function in each scope (
global, package, class, object)

Partially implements: blueprint murano-versioning

Change-Id: I0a553e8044061fe780a83bc04d70b8f80580988f
2015-09-04 14:37:14 +00:00

220 lines
6.3 KiB
Python

# Copyright (c) 2013 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.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 base64
import collections
import random
import re
import string
import time
import types
import jsonpatch
import jsonpointer
from yaql.language import specs
from yaql.language import utils
from yaql.language import yaqltypes
from murano.common import config as cfg
from murano.dsl import constants
from murano.dsl import dsl
from murano.dsl import helpers
from murano.dsl import yaql_integration
_random_string_counter = None
@specs.parameter('value', yaqltypes.String())
@specs.extension_method
def base64encode(value):
return base64.b64encode(value)
@specs.parameter('value', yaqltypes.String())
@specs.extension_method
def base64decode(value):
return base64.b64decode(value)
@specs.parameter('collection', yaqltypes.Iterable())
@specs.parameter('composer', yaqltypes.Lambda())
@specs.extension_method
def pselect(collection, composer):
return helpers.parallel_select(collection, composer)
@specs.parameter('mappings', collections.Mapping)
@specs.extension_method
def bind(obj, mappings):
if isinstance(obj, types.StringTypes) and obj.startswith('$'):
value = _convert_macro_parameter(obj[1:], mappings)
if value is not None:
return value
elif utils.is_sequence(obj):
return [bind(t, mappings) for t in obj]
elif isinstance(obj, collections.Mapping):
result = {}
for key, value in obj.iteritems():
result[bind(key, mappings)] = bind(value, mappings)
return result
elif isinstance(obj, types.StringTypes) and obj.startswith('$'):
value = _convert_macro_parameter(obj[1:], mappings)
if value is not None:
return value
return obj
def _convert_macro_parameter(macro, mappings):
replaced = [False]
def replace(match):
replaced[0] = True
return unicode(mappings.get(match.group(1)))
result = re.sub('{(\\w+?)}', replace, macro)
if replaced[0]:
return result
else:
return mappings[macro]
@specs.parameter('group', yaqltypes.String())
@specs.parameter('setting', yaqltypes.String())
def config(group, setting):
return cfg.CONF[group][setting]
@specs.parameter('setting', yaqltypes.String())
@specs.name('config')
def config_default(setting):
return cfg.CONF[setting]
@specs.parameter('string', yaqltypes.String())
@specs.parameter('start', int)
@specs.parameter('length', int)
@specs.inject('delegate', yaqltypes.Delegate('substring', method=True))
@specs.extension_method
def substr(delegate, string, start, length=-1):
return delegate(string, start, length)
@specs.extension_method
def patch_(engine, obj, patch):
if not isinstance(patch, tuple):
patch = (patch,)
patch = dsl.to_mutable(patch, engine)
patch = jsonpatch.JsonPatch(patch)
try:
obj = dsl.to_mutable(obj, engine)
return patch.apply(obj, in_place=True)
except jsonpointer.JsonPointerException:
return obj
def _int2base(x, base):
"""Converts decimal integers into another number base
from base-2 to base-36.
:param x: decimal integer
:param base: number base, max value is 36
:return: integer converted to the specified base
"""
digs = string.digits + string.lowercase
if x < 0:
sign = -1
elif x == 0:
return '0'
else:
sign = 1
x *= sign
digits = []
while x:
digits.append(digs[x % base])
x /= base
if sign < 0:
digits.append('-')
digits.reverse()
return ''.join(digits)
def random_name():
"""Replace '#' char in pattern with supplied number, if no pattern is
supplied generate short and unique name for the host.
:param pattern: hostname pattern
:param number: number to replace with in pattern
:return: hostname
"""
global _random_string_counter
counter = _random_string_counter or 1
# generate first 5 random chars
prefix = ''.join(random.choice(string.lowercase) for _ in range(5))
# convert timestamp to higher base to shorten hostname string
# (up to 8 chars)
timestamp = _int2base(int(time.time() * 1000), 36)[:8]
# third part of random name up to 2 chars
# (1295 is last 2-digit number in base-36, 1296 is first 3-digit number)
suffix = _int2base(counter, 36)
_random_string_counter = (counter + 1) % 1296
return prefix + timestamp + suffix
@specs.parameter('collection', yaqltypes.Iterable())
@specs.parameter('default', nullable=True)
@specs.extension_method
def first_or_default(collection, default=None):
try:
return iter(collection).next()
except StopIteration:
return default
@specs.parameter('logger_name', yaqltypes.String(True))
def logger(context, logger_name):
"""Instantiate Logger"""
log = yaql_integration.call_func(
context, 'new', 'io.murano.system.Logger',
logger_name=logger_name)
return log
@helpers.memoize
def get_context(runtime_version):
context = yaql_integration.create_empty_context()
context.register_function(base64decode)
context.register_function(base64encode)
context.register_function(pselect)
context.register_function(bind)
context.register_function(random_name)
context.register_function(patch_)
context.register_function(config)
context.register_function(config_default)
context.register_function(logger)
if runtime_version < constants.RUNTIME_VERSION_2_0:
context.register_function(substr)
context.register_function(first_or_default)
root_context = yaql_integration.create_context(runtime_version)
for t in ('to_lower', 'to_upper', 'trim', 'join', 'split',
'starts_with', 'ends_with', 'matches', 'replace',
'flatten'):
for spec in utils.to_extension_method(t, root_context):
context.register_function(spec)
return context