def validate(cls, expression): LOG.debug("Validating YAQL expression [expression='%s']", expression) try: yaql.parse(expression) except (yaql_exc.YaqlException, KeyError, ValueError, TypeError) as e: raise exc.YaqlEvaluationException(e.message)
def match(expr): if not isinstance(expr, types.StringTypes): return False if re.match('^[\s\w\d.:]*$', expr): return False try: yaql.parse(expr) return True except yaql.exceptions.YaqlGrammarException: return False except yaql.exceptions.YaqlLexicalException: return False
def eval(self, expression, data=None, context=None): res = yaql.parse(expression).evaluate(data=data, context=context or self.context) if isinstance(res, types.GeneratorType): return limit(res) else: return res
def __init__(self, name, version, input_spec=None, outputs_spec=None, task_list=None, heartbeat_timeout='60', schedule_to_close_timeout='518400', schedule_to_start_timeout='43200', start_to_close_timeout='432000'): self.name = name self.version = version if task_list is None: task_list = '{name}-{version}'.format( name=self.name, version=self.version ) self.task_list = task_list self.heartbeat_timeout = heartbeat_timeout self.schedule_to_close_timeout = schedule_to_close_timeout self.schedule_to_start_timeout = schedule_to_start_timeout self.start_to_close_timeout = start_to_close_timeout self._input_validator = SchemaValidator(input_spec) if outputs_spec: try: self._outputs_spec = {key: yaql.parse(expr) for key, expr in outputs_spec.items()} except Exception as err: _LOGGER.critical('Invalid YAQL expression in Activity %r: %r', name, err) raise else: self._outputs_spec = {}
def __init__(self, expression): if isinstance(expression, (yaql_expression.YaqlExpression, yaql.expressions.Expression)): self._expression = expression else: self._expression = yaql.parse(str(expression)) self._current_obj = None self._current_obj_name = None
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)
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)
def test_needs_evaluation(self): testee = helpers.needs_evaluation parsed_expr = yaql.parse("string") yaql_expr = yaql_expression.YaqlExpression("string") self.assertTrue(testee(parsed_expr)) self.assertTrue(testee(yaql_expr)) self.assertTrue(testee({yaql_expr: 1})) self.assertTrue(testee({'label': yaql_expr})) self.assertTrue(testee([yaql_expr]))
def main(context, show_tokens): print("Yet Another Query Language - command-line query tool") print("Version {0}".format(version)) print("Copyright (c) 2014 Mirantis, Inc") print("") if not context.get_data(): print("No data loaded into context ") print("Type '@load data-file.json' to load data") print("") readline.parse_and_bind('') comm = True while comm != 'exit': try: comm = raw_input(PROMPT) except EOFError: return if not comm: continue if comm[0] == '@': func_name, args = parse_service_command(comm) if func_name not in SERVICE_FUNCTIONS: print("Unknown command " + func_name) else: SERVICE_FUNCTIONS[func_name](args, context) continue try: if show_tokens: lexer.lexer.input(comm) tokens = [] while True: tok = lexer.lexer.token() if not tok: break tokens.append(tok) print("Tokens: " + str(tokens)) expr = yaql.parse(comm) except YaqlParsingException as ex: if ex.position: pointer_string = (" " * (ex.position + len(PROMPT))) + '^' print(pointer_string) print(ex.message) continue try: res = expr.evaluate(context=Context(context)) if isinstance(res, types.GeneratorType): res = limit(res) print(json.dumps(res, indent=4)) except Exception as ex: print("Execution exception:") if hasattr(ex, 'message'): print(ex.message) else: print("Unknown")
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)
def evaluate(cls, expression, data_context): LOG.debug("Evaluating YAQL expression [expression='%s', context=%s]" % (expression, data_context)) try: result = yaql.parse(expression).evaluate( data=data_context, context=yaql_utils.create_yaql_context()) except (KeyError, yaql_exc.YaqlException) as e: raise exc.YaqlEvaluationException( "Can not evaluate YAQL expression: %s, data = %s; error:" " %s" % (expression, data_context, str(e))) LOG.debug("YAQL expression result: %s" % result) return result if not inspect.isgenerator(result) else list(result)
def __init__(self, expression): if isinstance(expression, types.StringTypes): self._expression = encodeutils.safe_encode(expression) self._parsed_expression = yaql.parse(self._expression) self._file_position = None elif isinstance(expression, YaqlExpression): self._expression = expression._expression self._parsed_expression = expression._parsed_expression self._file_position = expression._file_position elif isinstance(expression, yaql.expressions.Expression): self._expression = str(expression) self._parsed_expression = expression self._file_position = None else: raise TypeError('expression is not of supported types')
def main(context): print "Yet Another Query Language - command-line query tool" print "Version {0}".format(version) print "Copyright (c) 2013 Mirantis, Inc" print if not context.get_data(): print "No data loaded into context " print "Type '@load data-file.json' to load data" print readline.parse_and_bind('') comm = True while comm != 'exit': try: comm = raw_input(PROMPT) except EOFError: return if not comm: continue if comm[0] == '@': funcName, args = parse_service_command(comm) if funcName not in SERVICE_FUNCTIONS: print "Unknown command " + funcName else: SERVICE_FUNCTIONS[funcName](args, context) continue try: expr = yaql.parse(comm) except YaqlParsingException as ex: if ex.position: pointer_string = (" " * (ex.position + len(PROMPT))) + '^' print pointer_string print ex.message continue try: res = expr.evaluate(context=Context(context)) if isinstance(res, types.GeneratorType): res = limit(res) print json.dumps(res, indent=4) except Exception as ex: print "Execution exception:" if hasattr(ex, 'message'): print ex.message else: print "Unknown"
def evaluate(cls, expression, data_context): LOG.debug("Evaluating YAQL expression [expression='%s', context=%s]" % (expression, data_context)) try: result = yaql.parse(expression).evaluate( data=data_context, context=yaql_utils.create_yaql_context() ) except (KeyError, yaql_exc.YaqlException) as e: raise exc.YaqlEvaluationException( "Can not evaluate YAQL expression: %s, data = %s; error:" " %s" % (expression, data_context, str(e)) ) LOG.debug("YAQL expression result: %s" % result) return result if not inspect.isgenerator(result) else list(result)
def main(context): print "Yet Another Query Language - command-line query tool" print "Copyright (c) 2013 Mirantis, Inc" print if not context.get_data(): print "No data loaded into context " print "Type '@load data-file.json' to load data" print comm = True while comm != 'exit': try: comm = raw_input(PROMPT) except EOFError: return if not comm: continue if comm[0] == '@': funcName, args = parse_service_command(comm) if funcName not in SERVICE_FUNCTIONS: print "Unknown command " + funcName else: SERVICE_FUNCTIONS[funcName](args, context) continue try: expr = yaql.parse(comm) except YaqlParsingException as ex: if ex.position: pointer_string = (" " * (ex.position + len(PROMPT))) + '^' print pointer_string print ex.message continue try: res = expr.evaluate(context=Context(context)) if isinstance(res, types.GeneratorType): res = list(res) print json.dumps(res, indent=4) except StandardError as ex: print "Execution exception:" if hasattr(ex, 'message'): print ex.message else: print "Unknown"
def __init__(self, name, version, input_spec=None, outputs_spec=None, task_list=None, heartbeat_timeout='60', schedule_to_close_timeout='518400', schedule_to_start_timeout='43200', start_to_close_timeout='432000'): self.name = name self.version = version if task_list is None: task_list = '{name}-{version}'.format(name=self.name, version=self.version) self.task_list = task_list self.heartbeat_timeout = heartbeat_timeout self.schedule_to_close_timeout = schedule_to_close_timeout self.schedule_to_start_timeout = schedule_to_start_timeout self.start_to_close_timeout = start_to_close_timeout self._input_validator = SchemaValidator(input_spec) if outputs_spec: try: self._outputs_spec = { key: yaql.parse(expr) for key, expr in outputs_spec.items() } except Exception as err: _LOGGER.critical('Invalid YAQL expression in Activity %r: %r', name, err) raise else: self._outputs_spec = {}
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) i += 1 if isinstance(res, collections.Iterable): for r in res: print r else: print res
def __init__(self, expression): self._expression = str(expression) self._parsed_expression = yaql.parse(self._expression)
def evaluate(cls, expression, context): LOG.debug("Evaluating YAQL expression [expression='%s', context=%s]") return yaql.parse(expression).evaluate(context)