Esempio n. 1
0
def query(jpath_query, json={}, vars={}, interpreter=None, as_tuple=False):
    if interpreter is None:
        interpreter = Interpreter()
    module = interpreter.get_binder("jpath").create_query(jpath_query)
    wrapped_vars = utils.singleton(translate.json_to_jpath(vars))
    result = module.call_with_values(
            context.DynamicContext(translate.json_to_jpath(json), 1, 1), 
            [wrapped_vars])
    if len(result) == 0 and not as_tuple:
        return None
    elif len(result) == 1 and not as_tuple:
        return translate.jpath_to_json(utils.get_single(result))
    else:
        return tuple(translate.jpath_to_json(v) for v in result)
Esempio n. 2
0
 def call(self, module_name, function_name, *args):
     """
     Calls the named function in the named module with the specified JSON
     arguments. The JSON result is then returned.
     """
     interpreter = self.interpreter_constructor()
     module = interpreter.bind_module("jpath", module_name)
     function = module.get_function(function_name)
     wrapped_args = [translate.json_to_jpath(v) for v in args]
     result = function.call_with_values(self.make_context(), wrapped_args)
     return translate.jpath_to_json(result)
Esempio n. 3
0
 def process_result(self, result, update):
     if update is None:
         update = True
         for value in result: # result is a Sequence
             if not isinstance(result, d.Update):
                 update = False
                 break
     if update:
         self.storage.apply_updates(result)
         result = None
     else:
         if len(result) == 0: # Result is the empty sequence
             result = None
         else: # Try to get the single resulting item
             result = translate.jpath_to_json(utils.get_single(result))
     self.storage.commit()
     return result