コード例 #1
0
ファイル: test_system.py プロジェクト: PaulReiber/yaql
    def test_int_bool_resolving(self):
        @parameter('param', arg_type=types.IntType)
        def int_func(param):
            return "int: " + str(param)

        @parameter('param', arg_type=types.BooleanType)
        def bool_func(param):
            return "bool: " + str(param)

        context1 = yaql.create_context(False)
        context2 = yaql.create_context(False)
        context3 = yaql.create_context(False)
        context4 = yaql.create_context(False)

        context1.register_function(int_func, 'foo')
        context2.register_function(bool_func, 'foo')
        context3.register_function(int_func, 'foo')
        context3.register_function(bool_func, 'foo')
        context4.register_function(bool_func, 'foo')
        context4.register_function(int_func, 'foo')

        self.assertEquals("int: 1", self.eval('foo(1)', context=context1))
        self.assertEquals("int: 0", self.eval('foo(0)', context=context1))
        self.assertRaises(Exception, self.eval, "foo('1')", context=context1)
        self.assertRaises(Exception, self.eval, 'foo(1)', context=context2)

        self.assertEquals("bool: True",
                          self.eval('foo(true)', context=context2))
        self.assertEquals("bool: False",
                          self.eval('foo(false)', context=context2))
        self.assertRaises(Exception, self.eval, "foo(1)", context=context2)
        self.assertRaises(Exception, self.eval, 'foo(0)', context=context2)
        self.assertRaises(Exception, self.eval, 'foo(True)', context=context2)
        self.assertRaises(Exception, self.eval, "foo('true')",
                          context=context2)

        self.assertEquals("int: 1", self.eval('foo(1)', context=context3))
        self.assertEquals("int: 0", self.eval('foo(0)', context=context3))
        self.assertEquals("bool: True",
                          self.eval('foo(true)', context=context3))
        self.assertEquals("bool: False",
                          self.eval('foo(false)', context=context3))

        self.assertEquals("int: 1", self.eval('foo(1)', context=context4))
        self.assertEquals("int: 0", self.eval('foo(0)', context=context4))
        self.assertEquals("bool: True",
                          self.eval('foo(true)', context=context4))
        self.assertEquals("bool: False",
                          self.eval('foo(false)', context=context4))
コード例 #2
0
    def __init__(self, cleaned_data, version, forms=None, templates=None,
                 application=None, **kwargs):
        self.cleaned_data = cleaned_data
        self.templates = templates or {}
        self.spec_version = str(version)

        if application is None:
            raise ValueError('Application section is required')
        else:
            self.application = application

        self.context = yaql.create_context()
        yaql_functions.register(self.context)

        self.forms = []
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

        if forms:
            for counter, data in enumerate(forms):
                name, field_specs, validators = self.extract_form_data(data)
                self._add_form(name, field_specs, validators)

        # Add ManageWorkflowForm
        workflow_form = catalog_forms.WorkflowManagementForm
        if semantic_version.Version.coerce(self.spec_version) >= \
                semantic_version.Version.coerce('2.2'):
            app_name_field = workflow_form.name_field(self._get_app_name())
            workflow_form.field_specs.insert(0, app_name_field)

        self._add_form(workflow_form.name,
                       workflow_form.field_specs,
                       workflow_form.validators)
コード例 #3
0
 def create_topics(job, warning, event_ctx):
     if not rules:
         return ()
     ctx = yaql.create_context()
     _add_job_context(ctx, job)
     _add_warning_context(ctx, warning, event_ctx)
     return get_topics(rules, ctx)
コード例 #4
0
def create_context(add_serializers=False, add_datadiff=False, **kwargs):
    context = yaql.create_context(**kwargs)
    if add_serializers:
        serializers.register(context)
    if add_datadiff:
        datadiff.register(context)
    return context
コード例 #5
0
    def __init__(self, cleaned_data, forms=None, templates=None,
                 application=None, **kwargs):
        self.cleaned_data = cleaned_data
        self.templates = templates or {}

        if application is None:
            raise ValueError('Application section is required')
        else:
            self.application = application

        self.context = yaql.create_context()
        yaql_functions.register(self.context)

        self.forms = []
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

        if forms:
            for data in forms:
                (name, field_specs, validators,
                 verbose_name) = self.extract_form_data(data)
                self._add_form(name, field_specs, validators, verbose_name)

        # Add ManageWorkflowForm
        workflow_form = catalog_forms.WorkflowManagementForm
        self._add_form(workflow_form.name, workflow_form.field_specs,
                       workflow_form.validators, _("What's next?"))
コード例 #6
0
ファイル: run.py プロジェクト: lightsey/yaql
def main():
    p = optparse.OptionParser()
    p.add_option("--data", "-d")
    p.add_option("-t", action="store_true", dest="tokens")
    p.add_option("--legacy", action="store_true", dest="legacy")

    options, arguments = p.parse_args()
    if options.data:
        try:
            with open(options.data) as f:
                data = json.load(f)
        except Exception:
            print("Unable to load data from " + options.data)
            return
    else:
        data = None

    engine_options = {"yaql.limitIterators": 100, "yaql.treatSetsAsLists": True, "yaql.memoryQuota": 10000}

    if options.legacy:
        factory = yaql.legacy.YaqlFactory()
        context = yaql.legacy.create_context()
        context["legacy"] = True
    else:
        factory = yaql.YaqlFactory()
        context = yaql.create_context()

    parser = factory.create(options=engine_options)
    cli_functions.register_in_context(context, parser)
    if options.tokens:
        parser("__main(true)").evaluate(data, context)
    else:
        parser("__main(false)").evaluate(data, context)
コード例 #7
0
    def test_evaluate(self):
        yaql_value = mock.Mock(spec=yaql_expression.YaqlExpression,
                               evaluate=lambda context: 'atom')
        complex_value = {
            yaql_value: ['some', (1, yaql_value), lambda: 'hi!'],
            'sample': [yaql_value, xrange(5)]
        }
        complex_literal = {
            'atom': ['some', (1, 'atom'), 'hi!'],
            'sample': ['atom', [0, 1, 2, 3, 4]]
        }
        # tuple(evaluate(list)) transformation adds + 1
        complex_literal_depth = 3 + 1

        context = yaql.create_context(False)
        evaluated_value = helpers.evaluate(yaql_value, context, 1)
        non_evaluated_value = helpers.evaluate(yaql_value, context, 0)
        evaluated_complex_value = helpers.evaluate(complex_value, context)
        non_evaluated_complex_value = helpers.evaluate(complex_value, context,
                                                       complex_literal_depth)

        self.assertEqual('atom', evaluated_value)
        self.assertNotEqual('atom', non_evaluated_value)
        self.assertEqual(complex_literal, evaluated_complex_value)
        self.assertNotEqual(complex_literal, non_evaluated_complex_value)
コード例 #8
0
    def __init__(self, stack, fn_name, args):
        super(Yaql, self).__init__(stack, fn_name, args)
        self.parser = self.get_yaql_parser()
        self.context = yaql.create_context()

        if not isinstance(self.args, collections.Mapping):
            raise TypeError(
                _('Arguments to "%s" must be a map.') % self.fn_name)

        try:
            self._expression = self.args['expression']
            self._data = self.args.get('data', {})
            for arg in six.iterkeys(self.args):
                if arg not in ['expression', 'data']:
                    raise KeyError
        except (KeyError, TypeError):
            example = ('''%s:
              expression: $.data.var1.sum()
              data:
                var1: [3, 2, 1]''') % self.fn_name
            raise KeyError(
                _('"%(name)s" syntax should be %(example)s') % {
                    'name': self.fn_name,
                    'example': example
                })
        self.validate_args()
コード例 #9
0
    def __init__(self, forms=None, templates=None, application=None, **kwargs):
        self.templates = templates or {}

        if application is None:
            raise ValueError('Application section is required')
        else:
            self.application = application

        self.context = yaql.create_context()
        yaql_functions.register(self.context)

        self.cleaned_data = {}
        self.forms = []
        self._forms = []
        for key, value in kwargs.iteritems():
            setattr(self, key, value)

        if forms:
            for data in forms:
                name, field_specs, validators = self.extract_form_data(data)
                self._init_forms(field_specs, name, validators)

        # Add ManageWorkflowForm
        workflow_form = catalog_forms.WorkflowManagementForm
        self._init_forms(workflow_form.field_specs, workflow_form.name,
                         workflow_form.validators)
コード例 #10
0
 def create_topics(job):
     if not rules:
         return ()
     # from yaql import yaqlization
     # yaqlization.yaqlize(job)
     ctx = yaql.create_context()
     _add_job_context(ctx, job)
     return get_topics(rules, ctx)
コード例 #11
0
def create_context(*args, **kwargs):
    tuples = kwargs.pop('tuples', True)

    context = yaql.create_context(*args, **kwargs)
    context = context.create_child_context()

    std_legacy.register(context, tuples)
    return context
コード例 #12
0
ファイル: legacy.py プロジェクト: ativelkov/yaql-1
def create_context(*args, **kwargs):
    tuples = kwargs.pop('tuples', True)

    context = yaql.create_context(*args, **kwargs)
    context = context.create_child_context()

    std_legacy.register(context, tuples)
    return context
コード例 #13
0
ファイル: executor.py プロジェクト: tsufiev/MuranoDsl
    def __init__(self, object_store):
        self._object_store = object_store
        self._root_context = yaql.create_context(True)

        @ContextAware()
        def resolve(context, name, obj):
            return self._resolve(context, name, obj)

        self._root_context.register_function(resolve, '#resolve')
コード例 #14
0
ファイル: expressions.py プロジェクト: openstack/yaql
 def evaluate(self, data=utils.NO_VALUE, context=None):
     if context is None or context is utils.NO_VALUE:
         context = yaql.create_context()
     if data is not utils.NO_VALUE:
         if self.engine.options.get('yaql.convertInputData', True):
             context['$'] = utils.convert_input_data(data)
         else:
             context['$'] = data
     return self(utils.NO_VALUE, context, self.engine)
コード例 #15
0
ファイル: functions.py プロジェクト: xglhjk6/heat
 def get_yaql_parser(cls):
     if cls._parser is None:
         global_options = {
             'yaql.limitIterators': cfg.CONF.yaql.limit_iterators,
             'yaql.memoryQuota': cfg.CONF.yaql.memory_quota
         }
         cls._parser = yaql.YaqlFactory().create(global_options)
         cls._context = yaql.create_context()
     return cls._parser
コード例 #16
0
 def get_yaql_parser(cls):
     if cls._parser is None:
         global_options = {
             'yaql.limitIterators': cfg.CONF.yaql.limit_iterators,
             'yaql.memoryQuota': cfg.CONF.yaql.memory_quota
         }
         cls._parser = yaql.YaqlFactory().create(global_options)
         cls._context = yaql.create_context()
     return cls._parser
コード例 #17
0
    def evaluate(self, data=None, context=None):
        if not context:
            context = Context(yaql.create_context())
        if data:
            context.set_data(data)

        f = self.create_callable(context)
        # noinspection PyCallingNonCallable
        return f()
コード例 #18
0
 def evaluate(self, data=utils.NO_VALUE, context=None):
     if context is None or context is utils.NO_VALUE:
         context = yaql.create_context()
     if data is not utils.NO_VALUE:
         if self.engine.options.get('yaql.convertInputData', True):
             context['$'] = utils.convert_input_data(data)
         else:
             context['$'] = data
     return self(utils.NO_VALUE, context, self.engine)
コード例 #19
0
ファイル: expressions.py プロジェクト: tsufiev/yaql
    def evaluate(self, data=None, context=None):
        if not context:
            context = Context(yaql.create_context())
        if data:
            context.set_data(data)

        f = self.create_callable(context)
        # noinspection PyCallingNonCallable
        return f()
コード例 #20
0
    def __init__(self, *args, **kwargs):
        LOG.info("Creating form {0}".format(self.__class__.__name__))
        super(ServiceConfigurationForm, self).__init__(*args, **kwargs)

        self.auto_id = '{0}_%s'.format(self.initial.get('app_id'))
        self.context = yaql.create_context()
        yaql_functions.register(self.context)

        self.finalize_fields()
        self.update_fields()
コード例 #21
0
    def __init__(self, *args, **kwargs):
        LOG.info("Creating form {0}".format(self.__class__.__name__))
        super(ServiceConfigurationForm, self).__init__(*args, **kwargs)

        self.context = yaql.create_context()
        yaql_functions.register(self.context)

        self.finalize_fields()
        self.initial = kwargs.get('initial', self.initial)
        self.update_fields()
コード例 #22
0
ファイル: run.py プロジェクト: xavierhardy/yaql
def main():
    p = optparse.OptionParser()
    p.add_option('--data', '-d', help="input file")
    p.add_option('--string', '-s', action='store_true',
                 help="input is a string")
    p.add_option('--native', '-n', action='store_true',
                 help="output data in Python native format")
    p.add_option('--array', '-a', action='store_true',
                 help="read input line by line")
    p.add_option('--tokens', '-t', action='store_true', dest='tokens',
                 help="print lexical tokens info")
    p.add_option('--legacy', action='store_true', dest='legacy',
                 help="enable legacy v0.2 compatibility mode")

    options, arguments = p.parse_args()
    if options.data:
        try:
            if options.data == '-':
                data = read_data(sys.stdin, options)
            else:
                with open(options.data) as f:
                    data = read_data(f, options)
        except Exception:
            print('Unable to load data from ' + options.data, file=sys.stderr)
            exit(1)
    else:
        data = None

    engine_options = {
        'yaql.limitIterators': 1000,
        'yaql.convertSetsToLists': True,
        'yaql.memoryQuota': 100000
    }

    if options.legacy:
        factory = yaql.legacy.YaqlFactory()
        context = yaql.legacy.create_context()
        context['legacy'] = True
    else:
        factory = yaql.YaqlFactory()
        context = yaql.create_context()

    if options.native:
        context['#nativeOutput'] = True

    parser = factory.create(options=engine_options)
    cli_functions.register_in_context(context, parser)

    if len(arguments) > 0:
        for arg in arguments:
            cli_functions.evaluate(arg, parser, data, context)
    elif options.tokens:
        parser('__main(true)').evaluate(data, context)
    else:
        parser('__main(false)').evaluate(data, context)
コード例 #23
0
ファイル: yaqluator.py プロジェクト: istalker2/yaqluator
def _evaluate(yaql_expression, yaml_data, legacy=False):
    if legacy:
        factory = yaql.legacy.YaqlFactory()
        context = yaql.legacy.create_context()
        context['legacy'] = True
    else:
        factory = yaql.YaqlFactory()
        context = yaql.create_context()

    parser = factory.create()
    return parser(yaql_expression).evaluate(yaml_data, context)
コード例 #24
0
    def new_context(self):
        """
        Prepares a new context for new evaluation. 
        Context should be new for each eval_expr. Lightweight object.
        :return: 
        """
        ctxt = yaql.create_context()

        # Register all required functions to the context
        versions_yaql.register(ctxt, self.engine)
        return ctxt
コード例 #25
0
 def __setstate__(self, d):
     for k, v in d.iteritems():
         setattr(self, k, v)
     # dealing with the attributes which cannot be serialized (see
     # http://tinyurl.com/kxx3tam on pickle restrictions )
     # yaql context is not serializable because it contains lambda functions
     self.context = yaql.create_context()
     yaql_functions.register(self.context)
     # form classes are not serializable 'cause they are defined dynamically
     self.forms = []
     for name, field_specs, validators in d.get('_forms', []):
         self._add_form(name, field_specs, validators)
コード例 #26
0
ファイル: expression.py プロジェクト: dz0ny/yglu
def get_context(root):
    key = id(root)
    if key in contexts:
        context = contexts[key]
    else:
        context = yaql.create_context(delegates=True)
        if "YGLU_ENABLE_ENV" in os.environ:
            context["$env"] = os.environ
        context["$_"] = root
        for process in context_processors:
            process(context, root)
        contexts[key] = context
    return context
コード例 #27
0
ファイル: test_engine.py プロジェクト: Magic-Mirror/murano
    def test_evaluate(self):
        yaql_value = mock.Mock(yaql_expression.YaqlExpression,
                               return_value='atom')
        complex_value = {yaql_value: ['some', (1, yaql_value), 'hi!'],
                         'sample': [yaql_value, six.moves.range(5)]}
        complex_literal = utils.FrozenDict({
            'atom': ('some', (1, 'atom'), 'hi!'),
            'sample': ('atom', (0, 1, 2, 3, 4))
        })
        context = yaql.create_context()
        evaluated_value = helpers.evaluate(yaql_value, context)
        evaluated_complex_value = helpers.evaluate(complex_value, context)

        self.assertEqual('atom', evaluated_value)
        self.assertEqual(complex_literal, evaluated_complex_value)
コード例 #28
0
    def test_evaluate(self):
        yaql_value = mock.Mock(yaql_expression.YaqlExpression,
                               return_value='atom')
        complex_value = {yaql_value: ['some', (1, yaql_value), 'hi!'],
                         'sample': [yaql_value, xrange(5)]}
        complex_literal = utils.FrozenDict({
            'atom': ('some', (1, 'atom'), 'hi!'),
            'sample': ('atom', (0, 1, 2, 3, 4))
        })
        context = yaql.create_context()
        evaluated_value = helpers.evaluate(yaql_value, context)
        evaluated_complex_value = helpers.evaluate(complex_value, context)

        self.assertEqual('atom', evaluated_value)
        self.assertEqual(complex_literal, evaluated_complex_value)
コード例 #29
0
def get_yaql_context(data_context):
    global ROOT_YAQL_CONTEXT

    if not ROOT_YAQL_CONTEXT:
        ROOT_YAQL_CONTEXT = yaql.create_context()

        _register_yaql_functions(ROOT_YAQL_CONTEXT)
    new_ctx = ROOT_YAQL_CONTEXT.create_child_context()
    new_ctx['$'] = data_context

    if isinstance(data_context, dict):
        new_ctx['__env'] = data_context.get('__env')
        new_ctx['__execution'] = data_context.get('__execution')
        new_ctx['__task_execution'] = data_context.get('__task_execution')

    return new_ctx
コード例 #30
0
ファイル: yaql_utils.py プロジェクト: dennybaa/mistral
def get_yaql_context(data_context):
    global ROOT_CONTEXT

    if not ROOT_CONTEXT:
        ROOT_CONTEXT = yaql.create_context()

        _register_functions(ROOT_CONTEXT)

    new_ctx = ROOT_CONTEXT.create_child_context()
    new_ctx['$'] = data_context

    if isinstance(data_context, dict):
        new_ctx['__env'] = data_context.get('__env')
        new_ctx['__execution'] = data_context.get('__execution')

    return new_ctx
コード例 #31
0
ファイル: yaqluator.py プロジェクト: ALU-CloudBand/yaqluator
def _evaluate(yaql_expression, yaml_data, legacy=False):
    engine_options = {
        'yaql.limitIterators': 100,
        'yaql.convertSetsToLists': True,
        'yaql.memoryQuota': 10000
    }

    if legacy:
        factory = yaql.legacy.YaqlFactory()
        context = yaql.legacy.create_context()
        context['legacy'] = True
    else:
        factory = yaql.YaqlFactory()
        context = yaql.create_context()

    parser = factory.create(options=engine_options)
    return parser(yaql_expression).evaluate(yaml_data, context)
コード例 #32
0
ファイル: yaqluator.py プロジェクト: ALU-CloudBand/yaqluator
def _evaluate(yaql_expression, yaml_data, legacy=False):
    engine_options = {
        'yaql.limitIterators': 100,
        'yaql.convertSetsToLists': True,
        'yaql.memoryQuota': 10000
    }

    if legacy:
        factory = yaql.legacy.YaqlFactory()
        context = yaql.legacy.create_context()
        context['legacy'] = True
    else:
        factory = yaql.YaqlFactory()
        context = yaql.create_context()

    parser = factory.create(options=engine_options)
    return parser(yaql_expression).evaluate(yaml_data, context)
コード例 #33
0
ファイル: run.py プロジェクト: tsufiev/yaql
def main():
    p = optparse.OptionParser()
    p.add_option('--data', '-d')
    options, arguments = p.parse_args()
    if options.data:
        try:
            json_str = open(options.data).read()
            decoder = JSONDecoder()
            data = decoder.decode(json_str)
        except:
            print "Unable to load data from "+options.data
            return
    else:
        data = None

    context = yaql.create_context()
    extensions.register_in_context(context)
    yaql.parse('__main()').evaluate(data, context)
コード例 #34
0
ファイル: run.py プロジェクト: tsufiev/yaql
def main():
    p = optparse.OptionParser()
    p.add_option('--data', '-d')
    options, arguments = p.parse_args()
    if options.data:
        try:
            json_str = open(options.data).read()
            decoder = JSONDecoder()
            data = decoder.decode(json_str)
        except:
            print "Unable to load data from " + options.data
            return
    else:
        data = None

    context = yaql.create_context()
    extensions.register_in_context(context)
    yaql.parse('__main()').evaluate(data, context)
コード例 #35
0
ファイル: functions.py プロジェクト: rabi/heat
    def __init__(self, stack, fn_name, args):
        super(Yaql, self).__init__(stack, fn_name, args)
        self._yaql_context = yaql.create_context()

        if not isinstance(self.args, collections.Mapping):
            raise TypeError(_('Arguments to "%s" must be a map.') %
                            self.fn_name)

        try:
            self._expression = self.args['expression']
            self._data = self.args.get('data', {})
            if set(self.args) - set(['expression', 'data']):
                raise KeyError
        except (KeyError, TypeError):
            example = ('''%s:
              expression: $.data.var1.sum()
              data:
                var1: [3, 2, 1]''') % self.fn_name
            raise KeyError(_('"%(name)s" syntax should be %(example)s') % {
                'name': self.fn_name, 'example': example})
コード例 #36
0
ファイル: test_engine.py プロジェクト: OndrejVojta/murano
    def test_evaluate(self):
        yaql_value = mock.Mock(spec=yaql_expression.YaqlExpression,
                               evaluate=lambda context: 'atom')
        complex_value = {yaql_value: ['some', (1, yaql_value), lambda: 'hi!'],
                         'sample': [yaql_value, xrange(5)]}
        complex_literal = {'atom': ['some', (1, 'atom'), 'hi!'],
                           'sample': ['atom', [0, 1, 2, 3, 4]]}
        # tuple(evaluate(list)) transformation adds + 1
        complex_literal_depth = 3 + 1

        context = yaql.create_context(False)
        evaluated_value = helpers.evaluate(yaql_value, context, 1)
        non_evaluated_value = helpers.evaluate(yaql_value, context, 0)
        evaluated_complex_value = helpers.evaluate(complex_value, context)
        non_evaluated_complex_value = helpers.evaluate(
            complex_value, context, complex_literal_depth)

        self.assertEqual('atom', evaluated_value)
        self.assertNotEqual('atom', non_evaluated_value)
        self.assertEqual(complex_literal, evaluated_complex_value)
        self.assertNotEqual(complex_literal, non_evaluated_complex_value)
コード例 #37
0
ファイル: expression_utils.py プロジェクト: openstack/mistral
def get_yaql_context(data_context):
    global ROOT_YAQL_CONTEXT

    if not ROOT_YAQL_CONTEXT:
        ROOT_YAQL_CONTEXT = yaql.create_context()

        _register_yaql_functions(ROOT_YAQL_CONTEXT)

    new_ctx = ROOT_YAQL_CONTEXT.create_child_context()

    new_ctx['$'] = (
        data_context if not cfg.CONF.yaql.convert_input_data
        else yaql_utils.convert_input_data(data_context)
    )

    if isinstance(data_context, dict):
        new_ctx['__env'] = data_context.get('__env')
        new_ctx['__execution'] = data_context.get('__execution')
        new_ctx['__task_execution'] = data_context.get('__task_execution')

    return new_ctx
コード例 #38
0
    def __init__(self):
        self._taskflow = _TaskFlow()
        self._yaql_engine = \
            _yaql.YaqlFactory(allow_delegates=True) \
            .create(
                options={
                    # 'yaql.convertInputData': False
                    'yaql.convertInputData': True
                })
        self._yaql_ctx = _yaql.create_context(
            convention=_yaql.language.conventions.PythonConvention(),
            delegates=True)
        self._yaql_expr = _Obj(
            dict((k, self._yaql_engine(v)) for k, v in [
                ("merge_yaml_consts",
                 "call(let, [none], $.aggregate($1.merge_with($2.yaml.get(const, {})), {}))"
                 ),
                ("merge_dict_list", "$.aggregate($1.merge_with($2), {})"),
            ]))

        _std_function_registry.fill_context(self._yaql_ctx)

        for i in _YAQL_PREPARE_CTX:
            self._yaql_ctx = self._yaql_engine(i).evaluate(
                context=self._yaql_ctx, data=None)

        self._taskflow.add_tasks([
            ('init', _init_task_init),
            ('load_yamls', _init_task_load_yamls),
            ('merge_yaml_consts', _init_task_merge_yaml_consts),
            ('py_deps', _init_task_py_deps),
            ('load_plugins', _init_task_load_plugins),
            ('load_py', _init_task_load_py),
            ('setup_yaql_extras', _init_task_setup_yaql_extras),
            ('setup_jinja', _init_task_setup_jinja),
            ('fragments', _init_task_fragments),
            ('prepare', _init_task_prepare),
            ('produce', _init_task_produce),
            ('result', _init_task_result),
        ])
コード例 #39
0
ファイル: run.py プロジェクト: thomasgoirand/yaql
def main():
    p = optparse.OptionParser()
    p.add_option('--data', '-d')
    p.add_option('-t', action='store_true', dest='tokens')

    options, arguments = p.parse_args()
    if options.data:
        try:
            json_str = open(options.data).read()
            decoder = JSONDecoder()
            data = decoder.decode(json_str)
        except:
            print("Unable to load data from " + options.data)
            return
    else:
        data = None

    context = yaql.create_context()
    cli_functions.register_in_context(context)
    if options.tokens:
        yaql.parse('__main(true)').evaluate(data, context)
    else:
        yaql.parse('__main(false)').evaluate(data, context)
コード例 #40
0
def main():
    p = optparse.OptionParser()
    p.add_option('--data', '-d')
    p.add_option('-t', action='store_true', dest='tokens')
    p.add_option('--legacy', action='store_true', dest='legacy')

    options, arguments = p.parse_args()
    if options.data:
        try:
            with open(options.data) as f:
                data = json.load(f)
        except Exception:
            print('Unable to load data from ' + options.data)
            return
    else:
        data = None

    engine_options = {
        'yaql.limitIterators': 100,
        'yaql.convertSetsToLists': True,
        'yaql.memoryQuota': 10000
    }

    if options.legacy:
        factory = yaql.legacy.YaqlFactory()
        context = yaql.legacy.create_context()
        context['legacy'] = True
    else:
        factory = yaql.YaqlFactory()
        context = yaql.create_context()

    parser = factory.create(options=engine_options)
    cli_functions.register_in_context(context, parser)
    if options.tokens:
        parser('__main(true)').evaluate(data, context)
    else:
        parser('__main(false)').evaluate(data, context)
コード例 #41
0
 def evaluate(self, data=utils.NO_VALUE, context=None):
     if context is None or context is utils.NO_VALUE:
         context = yaql.create_context()
     if data is not utils.NO_VALUE:
         context['$'] = utils.convert_input_data(data)
     return self(utils.NO_VALUE, context, self.engine)
コード例 #42
0
ファイル: fields.py プロジェクト: alexvassel/sbrf
 def validator_func(value):
     context = yaql.create_context()
     context.set_data(value)
     if not expr.evaluate(context=context):
         raise forms.ValidationError(message)
コード例 #43
0
ファイル: runner.py プロジェクト: istalker2/yaql
#
#         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 collections
from examples import testdata, ns_functions
import yaql

# DEPRECATED. Use cli to run samples

context = yaql.create_context()
ns_functions.register_in_context(context)
data = testdata.data

expression_list = [
    "$.services.join($.users, $1.yaql:owner=$2.id, dict('Service Name'=>$1.yaql:name,'User email'=>$2.email, 'Parent type'=>$1.yaql:parent_service))[$.'Parent type'=ex:Service0]",
    "range(0, 10 * random()).select(dict(num => $)).list()",
    "range(0).select(random()).takeWhile($ < 0.9).sum()",
]

parsed_list = [yaql.parse(expr) for expr in expression_list]
results = [expr.evaluate(data, context) for expr in parsed_list]

i = 1
for res in results:
    print "result #{0}".format(i)
コード例 #44
0
ファイル: __init__.py プロジェクト: PaulReiber/yaql
 def setUp(self):
     self.context = yaql.create_context()
コード例 #45
0
class YAQLEvaluator(expr_base.Evaluator):
    _type = 'yaql'
    _delimiter = '<%>'
    _regex_pattern = '<%.*?%>'
    _regex_parser = re.compile(_regex_pattern)

    _regex_dot_pattern = '[a-zA-Z0-9_\'"\.\[\]\(\)]*'
    _regex_ctx_pattern_1 = 'ctx\(\)\.%s' % _regex_dot_pattern
    _regex_ctx_pattern_2 = 'ctx\([\'|"]?{0}[\'|"]?\)[\.{0}]?'.format(
        _regex_dot_pattern)
    _regex_var_pattern = '.*?(%s|%s).*?' % (_regex_ctx_pattern_1,
                                            _regex_ctx_pattern_2)
    _regex_var_parser = re.compile(_regex_var_pattern)

    _regex_dot_extract = '([a-zA-Z0-9_\-]*)'
    _regex_ctx_extract_1 = 'ctx\(\)\.%s' % _regex_dot_extract
    _regex_ctx_extract_2 = 'ctx\([\'|"]?%s(%s)' % (_regex_dot_extract,
                                                   _regex_dot_pattern)
    _regex_var_extracts = [
        '%s\.?' % _regex_ctx_extract_1,
        '%s\.?' % _regex_ctx_extract_2
    ]

    _engine = yaql.language.factory.YaqlFactory().create()
    _root_ctx = yaql.create_context()
    _custom_functions = register_functions(_root_ctx)

    @classmethod
    def contextualize(cls, data):
        ctx = cls._root_ctx.create_child_context()
        ctx['__vars'] = data or {}
        ctx['__state'] = ctx['__vars'].get('__state')
        ctx['__current_task'] = ctx['__vars'].get('__current_task')
        ctx['__current_item'] = ctx['__vars'].get('__current_item')

        return ctx

    @classmethod
    def get_statement_regex(cls):
        return cls._regex_pattern

    @classmethod
    def has_expressions(cls, text):
        exprs = cls._regex_parser.findall(text)

        return exprs is not None and len(exprs) > 0

    @classmethod
    def get_var_extraction_regexes(cls):
        return cls._regex_var_extracts

    @classmethod
    def validate(cls, text):
        if not isinstance(text, six.string_types):
            raise ValueError('Text to be evaluated is not typeof string.')

        errors = []

        for expr in cls._regex_parser.findall(text):
            try:
                cls._engine(cls.strip_delimiter(expr))
            except (yaql_exc.YaqlException, ValueError, TypeError) as e:
                errors.append(expr_util.format_error(cls._type, expr, e))

        return errors

    @classmethod
    def evaluate(cls, text, data=None):
        if not isinstance(text, six.string_types):
            raise ValueError('Text to be evaluated is not typeof string.')

        if data and not isinstance(data, dict):
            raise ValueError('Provided data is not typeof dict.')

        output = str_util.unicode(text)
        exprs = cls._regex_parser.findall(text)
        ctx = cls.contextualize(data)

        try:
            for expr in exprs:
                stripped = cls.strip_delimiter(expr)
                result = cls._engine(stripped).evaluate(context=ctx)

                if inspect.isgenerator(result):
                    result = list(result)

                if isinstance(result, six.string_types):
                    result = cls.evaluate(result, data)

                if len(exprs) > 1 or len(output) > len(expr):
                    output = output.replace(
                        expr, str_util.unicode(result, force=True))
                else:
                    output = str_util.unicode(result)

        except KeyError as e:
            error = str(getattr(e, 'message', e)).strip("'")
            msg = "Unable to resolve key '%s' in expression '%s' from context."
            raise YaqlEvaluationException(msg % (error, expr))
        except (yaql_exc.YaqlException, ValueError, TypeError) as e:
            msg = "Unable to evaluate expression '%s'. %s: %s"
            raise YaqlEvaluationException(
                msg % (expr, e.__class__.__name__, str(e).strip("'")))
        except Exception as e:
            msg = "Unable to evaluate expression '%s'. %s: %s"
            raise YaqlEvaluationException(msg %
                                          (expr, e.__class__.__name__, str(e)))

        return output

    @classmethod
    def extract_vars(cls, text):
        if not isinstance(text, six.string_types):
            raise ValueError('Text to be evaluated is not typeof string.')

        variables = []

        for expr in cls._regex_parser.findall(text):
            variables.extend(cls._regex_var_parser.findall(expr))

        return sorted(list(set(variables)))
コード例 #46
0
ファイル: __init__.py プロジェクト: numvc/LuxoftBot
 def context(self):
     if self._context is None:
         self._context = yaql.create_context(delegates=True)
     return self._context
コード例 #47
0
ファイル: run.py プロジェクト: openstack/yaql
def main():
    p = optparse.OptionParser()
    p.add_option('--data', '-d', help="input file")
    p.add_option('--string', '-s', action='store_true',
                 help="input is a string")
    p.add_option('--native', '-n', action='store_true',
                 help="output data in Python native format")
    p.add_option('--array', '-a', action='store_true',
                 help="read input line by line")
    p.add_option('--tokens', '-t', action='store_true', dest='tokens',
                 help="print lexical tokens info")
    p.add_option('--legacy', '-l', action='store_true', dest='legacy',
                 help="enable legacy v0.2 compatibility mode")
    p.add_option('--limit', '--limit-iterators', '-L', type=int,
                 dest='limit_iterators',
                 default=1000, help="limit iterators by the given number of "
                                    "elements (-1 means infinity)")
    p.add_option('--sets-to-lists', '-S', action="store_true",
                 dest='sets_to_lists', default=True,
                 help="convert all sets in results to lists")
    p.add_option('--tuples-to-lists', '-T', action="store_true",
                 dest='tuples_to_lists', default=False,
                 help="convert all tuples in results to lists")
    p.add_option('--iterable-dicts', '-D', action="store_true",
                 dest='iterable_dicts', default=False,
                 help="consider dictionaries to be iterable over their keys")
    p.add_option('--memory-quota', '--mem', '-m', type=int, dest='memory',
                 default=100000, help="change memory usage quota (in bytes) "
                                      "for all data produced by expressions "
                                      "(-1 means infinity)")
    p.add_option('--keyword-operator', '-k', type=str, dest='keyword_operator',
                 default="=>", help="configure keyword/mapping symbol "
                                    "(empty string means disabled)")
    p.add_option('--allow-delegates', '-A', action="store_true",
                 dest='allow_delegates', default=False,
                 help="enable delegate expression parsing")

    options, arguments = p.parse_args()
    if options.data:
        try:
            if options.data == '-':
                data = read_data(sys.stdin, options)
            else:
                with open(options.data) as f:
                    data = read_data(f, options)
        except Exception:
            print('Unable to load data from ' + options.data, file=sys.stderr)
            exit(1)
    else:
        data = None

    engine_options = {
        'yaql.limitIterators': options.limit_iterators,
        'yaql.convertSetsToLists': options.sets_to_lists,
        'yaql.convertTuplesToLists': options.tuples_to_lists,
        'yaql.iterableDicts': options.iterable_dicts,
        'yaql.memoryQuota': options.memory
    }

    if options.legacy:
        factory = yaql.legacy.YaqlFactory(
            allow_delegates=options.allow_delegates
        )
        context = yaql.legacy.create_context()
        context['legacy'] = True
    else:
        factory = yaql.YaqlFactory(
            allow_delegates=options.allow_delegates,
            keyword_operator=options.keyword_operator
        )
        context = yaql.create_context()

    if options.native:
        context['#nativeOutput'] = True

    parser = factory.create(options=engine_options)
    cli_functions.register_in_context(context, parser)

    if len(arguments) > 0:
        for arg in arguments:
            cli_functions.evaluate(arg, parser, data, context)
    elif options.tokens:
        parser('__main(true)').evaluate(data, context)
    else:
        parser('__main(false)').evaluate(data, context)
コード例 #48
0
ファイル: class_loader.py プロジェクト: schaertelr/murano
 def create_root_context(self):
     return yaql.create_context(True)
コード例 #49
0
def main():
    p = optparse.OptionParser()
    p.add_option('--data', '-d', help="input file")
    p.add_option('--string',
                 '-s',
                 action='store_true',
                 help="input is a string")
    p.add_option('--native',
                 '-n',
                 action='store_true',
                 help="output data in Python native format")
    p.add_option('--array',
                 '-a',
                 action='store_true',
                 help="read input line by line")
    p.add_option('--tokens',
                 '-t',
                 action='store_true',
                 dest='tokens',
                 help="print lexical tokens info")
    p.add_option('--legacy',
                 action='store_true',
                 dest='legacy',
                 help="enable legacy v0.2 compatibility mode")

    options, arguments = p.parse_args()
    if options.data:
        try:
            if options.data == '-':
                data = read_data(sys.stdin, options)
            else:
                with open(options.data) as f:
                    data = read_data(f, options)
        except Exception:
            print('Unable to load data from ' + options.data, file=sys.stderr)
            exit(1)
    else:
        data = None

    engine_options = {
        'yaql.limitIterators': 1000,
        'yaql.convertSetsToLists': True,
        'yaql.memoryQuota': 100000
    }

    if options.legacy:
        factory = yaql.legacy.YaqlFactory()
        context = yaql.legacy.create_context()
        context['legacy'] = True
    else:
        factory = yaql.YaqlFactory()
        context = yaql.create_context()

    if options.native:
        context['#nativeOutput'] = True

    parser = factory.create(options=engine_options)
    cli_functions.register_in_context(context, parser)

    if len(arguments) > 0:
        for arg in arguments:
            cli_functions.evaluate(arg, parser, data, context)
    elif options.tokens:
        parser('__main(true)').evaluate(data, context)
    else:
        parser('__main(false)').evaluate(data, context)
コード例 #50
0
 def validator_func(value):
     context = yaql.create_context()
     context['$'] = value
     if not expr.evaluate(context=context):
         raise forms.ValidationError(message)
コード例 #51
0
               if runtime_version <= constants.RUNTIME_VERSION_1_1 else
               ENGINE_12_OPTIONS)
    return engine_factory.create(options=options)


@specs.name('#finalize')
def _finalize(obj, context):
    return helpers.evaluate(obj, context)


CONVENTION = conventions.CamelCaseConvention()
ENGINE_10 = _create_engine(constants.RUNTIME_VERSION_1_0)
ENGINE_12 = _create_engine(constants.RUNTIME_VERSION_1_2)
ROOT_CONTEXT_10 = legacy.create_context(convention=CONVENTION,
                                        finalizer=_finalize)
ROOT_CONTEXT_12 = yaql.create_context(convention=CONVENTION,
                                      finalizer=_finalize)


class ContractedValue(yaqltypes.GenericType):
    def __init__(self, value_spec, with_check=False):
        def converter(value, receiver, context, *args, **kwargs):
            if isinstance(receiver, dsl_types.MuranoObject):
                this = receiver.real_this
            else:
                this = receiver
            return value_spec.transform(value, this,
                                        context[constants.CTX_ARGUMENT_OWNER],
                                        context)

        def checker(value, context, *args, **kwargs):
            return value_spec.check_type(value, context)
コード例 #52
0
ファイル: yaql_utils.py プロジェクト: kantorv/mistral
def create_yaql_context():
    ctx = yaql.create_context()

    _register_functions(ctx)

    return ctx
コード例 #53
0
    options = (ENGINE_10_OPTIONS
               if runtime_version <= constants.RUNTIME_VERSION_1_1
               else ENGINE_12_OPTIONS)
    return engine_factory.create(options=options)


@specs.name('#finalize')
def _finalize(obj, context):
    return helpers.evaluate(obj, context)

CONVENTION = conventions.CamelCaseConvention()
ENGINE_10 = _create_engine(constants.RUNTIME_VERSION_1_0)
ENGINE_12 = _create_engine(constants.RUNTIME_VERSION_1_2)
ROOT_CONTEXT_10 = legacy.create_context(
    convention=CONVENTION, finalizer=_finalize)
ROOT_CONTEXT_12 = yaql.create_context(
    convention=CONVENTION, finalizer=_finalize)


class ContractedValue(yaqltypes.GenericType):
    def __init__(self, value_spec):
        self._value_spec = value_spec
        self._last_result = False

        super(ContractedValue, self).__init__(
            True, None,
            lambda value, sender, context, *args, **kwargs:
                self._value_spec.validate(
                    value, sender.real_this,
                    context[constants.CTX_ARGUMENT_OWNER], context))

    def convert(self, value, *args, **kwargs):
コード例 #54
0
def main():
    p = optparse.OptionParser()
    p.add_option('--data', '-d', help="input file")
    p.add_option('--string',
                 '-s',
                 action='store_true',
                 help="input is a string")
    p.add_option('--native',
                 '-n',
                 action='store_true',
                 help="output data in Python native format")
    p.add_option('--array',
                 '-a',
                 action='store_true',
                 help="read input line by line")
    p.add_option('--tokens',
                 '-t',
                 action='store_true',
                 dest='tokens',
                 help="print lexical tokens info")
    p.add_option('--legacy',
                 '-l',
                 action='store_true',
                 dest='legacy',
                 help="enable legacy v0.2 compatibility mode")
    p.add_option('--limit',
                 '--limit-iterators',
                 '-L',
                 type=int,
                 dest='limit_iterators',
                 default=1000,
                 help="limit iterators by the given number of "
                 "elements (-1 means infinity)")
    p.add_option('--sets-to-lists',
                 '-S',
                 action="store_true",
                 dest='sets_to_lists',
                 default=True,
                 help="convert all sets in results to lists")
    p.add_option('--tuples-to-lists',
                 '-T',
                 action="store_true",
                 dest='tuples_to_lists',
                 default=False,
                 help="convert all tuples in results to lists")
    p.add_option('--iterable-dicts',
                 '-D',
                 action="store_true",
                 dest='iterable_dicts',
                 default=False,
                 help="consider dictionaries to be iterable over their keys")
    p.add_option('--memory-quota',
                 '--mem',
                 '-m',
                 type=int,
                 dest='memory',
                 default=100000,
                 help="change memory usage quota (in bytes) "
                 "for all data produced by expressions "
                 "(-1 means infinity)")
    p.add_option('--keyword-operator',
                 '-k',
                 type=str,
                 dest='keyword_operator',
                 default="=>",
                 help="configure keyword/mapping symbol "
                 "(empty string means disabled)")
    p.add_option('--allow-delegates',
                 '-A',
                 action="store_true",
                 dest='allow_delegates',
                 default=False,
                 help="enable delegate expression parsing")

    options, arguments = p.parse_args()
    if options.data:
        try:
            if options.data == '-':
                data = read_data(sys.stdin, options)
            else:
                with open(options.data) as f:
                    data = read_data(f, options)
        except Exception:
            print('Unable to load data from ' + options.data, file=sys.stderr)
            exit(1)
    else:
        data = None

    engine_options = {
        'yaql.limitIterators': options.limit_iterators,
        'yaql.convertSetsToLists': options.sets_to_lists,
        'yaql.convertTuplesToLists': options.tuples_to_lists,
        'yaql.iterableDicts': options.iterable_dicts,
        'yaql.memoryQuota': options.memory
    }

    if options.legacy:
        factory = yaql.legacy.YaqlFactory(
            allow_delegates=options.allow_delegates)
        context = yaql.legacy.create_context()
        context['legacy'] = True
    else:
        factory = yaql.YaqlFactory(allow_delegates=options.allow_delegates,
                                   keyword_operator=options.keyword_operator)
        context = yaql.create_context()

    if options.native:
        context['#nativeOutput'] = True

    parser = factory.create(options=engine_options)
    cli_functions.register_in_context(context, parser)

    if len(arguments) > 0:
        for arg in arguments:
            cli_functions.evaluate(arg, parser, data, context)
    elif options.tokens:
        parser('__main(true)').evaluate(data, context)
    else:
        parser('__main(false)').evaluate(data, context)
コード例 #55
0
ファイル: expressions.py プロジェクト: ativelkov/yaql-1
 def evaluate(self, data=utils.NO_VALUE, context=None):
     if context is None or context is utils.NO_VALUE:
         context = yaql.create_context()
     if data is not utils.NO_VALUE:
         context['$'] = utils.convert_input_data(data)
     return self(utils.NO_VALUE, context, self.engine)
コード例 #56
0
def create_yaql_context():
    ctx = yaql.create_context()

    _register_functions(ctx)

    return ctx