Beispiel #1
0
def build_list(*args):
    res = []
    for arg in args:
        if isinstance(arg, types.GeneratorType):
            arg = limit(arg)
        res.append(arg)
    return res
Beispiel #2
0
 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
Beispiel #3
0
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")
Beispiel #4
0
def _list(self):
    return limit(self)