def stub(*args, **kwargs): context = self.context args = utils.convert_input_data(args) kwargs = utils.convert_input_data(kwargs) limit_func = context('#iter', self.engine) return utils.convert_output_data( context(item, self.engine, self.sender)(*args, **kwargs), limit_func, self.engine)
def __call__(self, __expression, *args, **kwargs): context = self.context.create_child_context() args = utils.convert_input_data(args) for i, arg_value in enumerate(args): context['$' + str(i + 1)] = arg_value kwargs = utils.convert_input_data(kwargs) for arg_name, arg_value in six.iteritems(kwargs): context['$' + arg_name] = arg_value parsed = self.engine(__expression) res = parsed.evaluate(context=context) limit_func = context('#iter', self.engine) return utils.convert_output_data(res, limit_func, self.engine)
def _setup_context(data, context, finalizer, convention): if context is None: context = contexts.Context( convention=convention or conventions.CamelCaseConvention()) if finalizer is None: @specs.parameter('iterator', yaqltypes.Iterable()) @specs.name('#iter') def limit(iterator): return iterator @specs.inject('limiter', yaqltypes.Delegate('#iter')) @specs.inject('engine', yaqltypes.Engine()) @specs.name('#finalize') def finalize(obj, limiter, engine): return utils.convert_output_data(obj, limiter, engine) context.register_function(limit) context.register_function(finalize) else: context.register_function(finalizer) if data is not utils.NO_VALUE: context['$'] = utils.convert_input_data(data) return context
def _setup_context(data, context, finalizer, convention): if context is None: context = contexts.Context( convention=convention or conventions.CamelCaseConvention()) if finalizer is None: @specs.parameter('iterator', yaqltypes.Iterable()) @specs.name('#iter') def limit(iterator): return iterator @specs.inject('limiter', yaqltypes.Delegate('#iter')) @specs.inject('engine', yaqltypes.Engine()) @specs.name('#finalize') def finalize(obj, limiter, engine): if engine.options.get('yaql.convertOutputData', True): return utils.convert_output_data(obj, limiter, engine) return obj context.register_function(limit) context.register_function(finalize) else: context.register_function(finalizer) if data is not utils.NO_VALUE: context['$'] = utils.convert_input_data(data) return context
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)
def load_data(data_file, context): try: json_str = open(os.path.expanduser(data_file)).read() except IOError as e: print("Unable to read data file '{0}': {1}".format(data_file, e.strerror)) return try: data = json.loads(json_str) except Exception as e: print('Unable to parse data: ' + e.message) return context['$'] = utils.convert_input_data(data) print('Data from file {0} loaded into context'.format(data_file))
def contextualize(cls, data): ctx = cls._root_ctx.create_child_context() # Some yaql expressions (e.g. distinct()) refer to hash value of variable. # But some built-in Python type values (e.g. list and dict) don't have __hash__() method. # The convert_input_data method parses specified variable and convert it to hashable one. if isinstance(data, yaql_utils.SequenceType) or isinstance(data, yaql_utils.MappingType): ctx["__vars"] = yaql_utils.convert_input_data(data) else: 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
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['$'] = 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
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
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)