def parse_string_value(display, item, **context): if not isinstance(item, str): return (item,) if item.startswith("@") or "://" in item: if item[0] == "@": item = item[1:] urltype = "path" if "://" in item: urltype, item = item.split("://") if urltype == "eq": return sakstig.QuerySet([context]).execute(item) elif urltype == "resource": pkg, item = item.split("/", 1) with pkg_resources.resource_stream(pkg, item) as f: return (f.read(),) elif urltype == "data": return(item.encode("utf-8"),) #elif urltype == "path": else: with open(item, "rb") as f: return (f.read(),) try: if item[0] in '0123456789-': if "." in item: return (float(item),) else: return (int(item),) except: pass return (item,)
def __call__(self, context): if self.match is not None and not sakstig.QuerySet([context]).execute(self.match): return False if self.template is not None: return sakform.transform(context, self.template)[0] return context["body"]
def transform(global_qs, local_qs, template): if sakstig.is_dict(template): if '$' in template: result = path(global_qs, local_qs, template["$"]) if set(template.keys()) - set(('$', "_")): result = sakstig.QuerySet( {key: value[0] for key, value in ((key, transform(global_qs, sakstig.QuerySet([item]), value)) for key, value in template.items() if key not in ("$", "_")) if value} for item in result) else: if set(template.keys()) - set(('$', "_")): result = sakstig.QuerySet([ {key: value[0] for key, value in ((key, transform(global_qs, local_qs, value)) for key, value in template.items() if key not in ("$", "_")) if value}]) else: result = sakstig.QuerySet([{}]) if '_' in template: result = transform(global_qs, result, template["_"]) return result elif sakstig.is_list(template): return sakstig.QuerySet([list(sakstig.QuerySet( transform(global_qs, local_qs, item) for item in template).flatten())]) else: return sakstig.QuerySet([template])
def __call__(self, context): if self.match is not None and not sakstig.QuerySet([context]).execute(self.match): return False if self.document is not None: res = sakform.transform(context, self.document)[0] if "response" in res and not isinstance(res["response"], (str, bytes)): res["response"] = json.dumps(res["response"]) flask.abort(flask.Response(**res)) flask.abort(403)
def __call__(self, context): if self.match is not None and not sakstig.QuerySet([context]).execute(self.match): return False if self.filter is not None: context['body']["query"] = {"bool": {"must": context["query"], "filter": sakform.transform( context, self.filter) }} return context['body']
def call(self, global_qs, local_qs, args): args = sakstig.QuerySet(args).flatten() kwargs = {} if sakstig.is_dict(args[-1]): kwargs = args[-1] args = args[:-1] method = kwargs.pop("method", "get") r = getattr(requests, method)(*args, **kwargs) try: content = r.json() except: content = r.content return sakstig.QuerySet([{ "status": r.status_code, "content": content, "headers": r.headers }])
def transform(data, template, debug=False, debug_path=False): """Transforms data according to the template. """ def path(global_qs, local_qs, expr): if debug_path: print("path(\n%s,\n%s)" % (repr(data), repr(expr))) global_qs._global_template_qs = template_qs global_qs._local_template_qs = local_qs res = local_qs.execute(expr, global_qs) if debug_path: print("=> %s" % repr(res)) return res def transform(global_qs, local_qs, template): if sakstig.is_dict(template): if '$' in template: result = path(global_qs, local_qs, template["$"]) if set(template.keys()) - set(('$', "_")): result = sakstig.QuerySet( {key: value[0] for key, value in ((key, transform(global_qs, sakstig.QuerySet([item]), value)) for key, value in template.items() if key not in ("$", "_")) if value} for item in result) else: if set(template.keys()) - set(('$', "_")): result = sakstig.QuerySet([ {key: value[0] for key, value in ((key, transform(global_qs, local_qs, value)) for key, value in template.items() if key not in ("$", "_")) if value}]) else: result = sakstig.QuerySet([{}]) if '_' in template: result = transform(global_qs, result, template["_"]) return result elif sakstig.is_list(template): return sakstig.QuerySet([list(sakstig.QuerySet( transform(global_qs, local_qs, item) for item in template).flatten())]) else: return sakstig.QuerySet([template]) if debug: print("transform(\n%s,\n%s)" % (repr(data), repr(template))) template_qs = sakstig.QuerySet([template]) if not isinstance(data, sakstig.QuerySet): data = sakstig.QuerySet([data]) res = transform(data, template_qs, template) if debug: print("=> %s" % repr(res)) return res