def xslt(req, *opts): """ Transform the working document using an XSLT file. :param req: The request :param opts: Options (unused) :return: the transformation result Apply an XSLT stylesheet to the working document. The xslt pipe takes a set of keyword arguments. The only required argument is 'stylesheet' which identifies the xslt resource. This is looked up either in the package or as a user-supplied file. The rest of the keyword arguments are made available as string parameters to the XSLT transform. **Examples** .. code-block:: yaml - xslt: sylesheet: foo.xsl x: foo y: bar """ stylesheet = req.args.get('stylesheet', None) if stylesheet is None: raise PipeException("xslt requires stylesheet") if req.t is None: raise PipeException("Your plumbing is missing a select statement.") params = dict((k, "\'%s\'" % v) for (k, v) in req.args.items()) del params['stylesheet'] ot = xslt_transform(req.t, stylesheet, params) #log.debug(ot) return ot
def xslt(req, *opts): """ Transform the working document using an XSLT file. :param req: The request :param opts: Options (unused) :return: the transformation result Apply an XSLT stylesheet to the working document. The xslt pipe takes a set of keyword arguments. The only required argument is 'stylesheet' which identifies the xslt resource. This is looked up either in the package or as a user-supplied file. The rest of the keyword arguments are made available as string parameters to the XSLT transform. **Examples** .. code-block:: yaml - xslt: sylesheet: foo.xsl x: foo y: bar """ if req.t is None: raise PipeException("Your plumbing is missing a select statement.") stylesheet = req.args.get('stylesheet', None) if stylesheet is None: raise PipeException("xslt requires stylesheet") params = dict((k, "\'%s\'" % v) for (k, v) in req.args.items()) del params['stylesheet'] try: return xslt_transform(req.t, stylesheet, params) # log.debug(ot) except Exception as ex: traceback.print_exc(ex) raise ex
def request(self, **kwargs): stats['MD Requests'] += 1 pfx = kwargs.get('pfx', None) path = kwargs.get('path', None) content_type = kwargs.get('content_type', None) log.debug("request pfx=%s, path=%s, content_type=%s" % (pfx, path, content_type)) def escape(m): st = m.group(0) if st == '<': return '<' if st == '>': return '>' return st def _d(x): if x is None or len(x) == 0: return None, None if x.startswith("{base64}"): x = x[8:].decode('base64') if '.' in x: (p, sep, ext) = x.rpartition('.') return p, ext else: return x, None _ctypes = {'xml': 'application/xml', 'json': 'application/json', 'htm': 'text/html', 'html': 'text/html', 'ds': 'text/html', 's': 'application/json'} alias = None if pfx: alias = pfx pfx = self.aliases.get(alias, None) if pfx is None: raise NotFound() path, ext = _d(path) if pfx and path: q = "{%s}%s" % (pfx, path) else: q = path logging.debug("request %s %s" % (path, ext)) log.debug(cherrypy.request.headers) accept = {} if content_type is None: if ext is not None and ext in _ctypes: accept = {_ctypes[ext]: True} else: accept = MDServer.MediaAccept() if ext is not None: path = "%s.%s" % (path, ext) else: accept = {content_type: True} with self.lock.readlock: if ext == 'ds': pdict = dict() pdict['http'] = cherrypy.request entityID = kwargs.get('entityID', None) if entityID is None: raise HTTPError(400, "400 Bad Request - missing entityID") pdict['sp'] = self.md.sha1_id(entityID) pdict['ret'] = kwargs.get('return', None) if not path: pdict['search'] = "/search/" else: pdict['search'] = "%s.s" % path if pdict['ret'] is None: raise HTTPError(400, "400 Bad Request - Missing 'return' parameter") pdict['returnIDParam'] = kwargs.get('returnIDParam', 'entityID') cherrypy.response.headers['Content-Type'] = 'text/html' pdict['style'] = '/static/css/style.css' # TODO figure out how to sensibly set this per request return template("ds.html").render(**pdict) elif ext == 's': paged = bool(kwargs.get('paged', False)) query = kwargs.get('query', None) page = kwargs.get('page', 0) page_limit = kwargs.get('page_limit', 10) entity_filter = kwargs.get('entity_filter', None) cherrypy.response.headers['Content-Type'] = 'application/json' if paged: res, more, total = self.md.search(query, path=q, page=int(page), page_limit=int(page_limit), entity_filter=entity_filter) log.debug(dumps({'entities': res, 'more': more, 'total': total})) return dumps({'entities': res, 'more': more, 'total': total}) else: return dumps(self.md.search(query, path=q, entity_filter=entity_filter)) elif accept.get('text/html'): if not q: if pfx: title = pfx else: title = "Metadata By Attributes" return template("index.html").render(http=cherrypy.request, md=self.md, alias=alias, aliases=self.aliases, title=title) else: entities = self.md.lookup(q) if not entities: raise NotFound() if len(entities) > 1: return template("metadata.html").render(http=cherrypy.request, md=self.md, entities=entities) else: entity = entities[0] t = html.fragment_fromstring(unicode(xslt_transform(entity, "entity2html.xsl"))) for c_elt in t.findall(".//code[@role='entity']"): c_txt = dumptree(entity, pretty_print=True, xml_declaration=False).decode("utf-8") p = c_elt.getparent() p.remove(c_elt) if p.text is not None: p.text += c_txt # re.sub(".",escape,c_txt) else: p.text = c_txt # re.sub(".",escape,c_txt) xml = dumptree(t, xml_declaration=False).decode('utf-8') return template("basic.html").render(http=cherrypy.request, content=xml) else: for p in self.plumbings: state = {'request': True, 'headers': {'Content-Type': 'text/xml'}, 'accept': accept, 'url': cherrypy.url(relative=False), 'select': q, 'stats': {}} r = p.process(self.md, state=state) if r is not None: cache_ttl = state.get('cache', 0) log.debug("caching for %d seconds" % cache_ttl) caching.expires(secs=cache_ttl) for k, v in state.get('headers', {}).iteritems(): cherrypy.response.headers[k] = v return r raise NotFound()
def request(self, **kwargs): """The main request processor. This code implements all rendering of metadata. """ stats['MD Requests'] += 1 if not self.ready: raise HTTPError(503, _("Service Unavailable (repository loading)")) pfx = kwargs.get('pfx', None) path = kwargs.get('path', None) content_type = kwargs.get('content_type', None) log.debug("MDServer pfx=%s, path=%s, content_type=%s" % (pfx, path, content_type)) def _d(x, do_split=True): if x is not None: x = x.strip() log.debug("_d(%s,%s)" % (x, do_split)) if x is None or len(x) == 0: return None, None if x.startswith("{base64}"): x = x[8:].decode('base64') if do_split and '.' in x: (pth, dot, extn) = x.rpartition('.') assert (dot == '.') if extn in _ctypes: return pth, extn return x, None _ctypes = {'xml': 'application/xml', 'json': 'application/json', 'htm': 'text/html', 'html': 'text/html', 'ds': 'text/html', 's': 'application/json'} alias = None if pfx: alias = pfx pfx = self.aliases.get(alias, None) if pfx is None: raise NotFound() path, ext = _d(path, content_type is None) if pfx and path: q = "{%s}%s" % (pfx, path) path = "/%s/%s" % (alias, path) else: q = path if ext is not None: log.debug("request path: %s.%s, headers: %s" % (path, ext, cherrypy.request.headers)) else: log.debug("request path: %s, headers: %s" % (path, cherrypy.request.headers)) accept = {} if content_type is None: if ext is not None and ext in _ctypes: accept = {_ctypes[ext]: True} else: accept = MDServer.MediaAccept() if ext is not None: path = "%s.%s" % (path, ext) else: accept = {content_type: True} with self.lock.readlock: if ext == 'ds': pdict = dict() entity_id = kwargs.get('entityID', None) if entity_id is None: raise HTTPError(400, _("400 Bad Request - missing entityID")) pdict['sp'] = self.md.sha1_id(entity_id) e = self.md.store.lookup(entity_id) if e is None or len(e) == 0: raise HTTPError(404) if len(e) > 1: raise HTTPError(400, _("400 Bad Request - multiple matches for") + " %s" % entity_id) pdict['entity'] = self.md.simple_summary(e[0]) if not path: pdict['search'] = "/search/" pdict['list'] = "/role/idp.json" else: pdict['search'] = "%s.s" % path pdict['list'] = "%s.json" % path cherrypy.response.headers['Content-Type'] = 'text/html' return render_template("ds.html", **pdict) elif ext == 's': paged = bool(kwargs.get('paged', False)) query = kwargs.get('query', None) page = kwargs.get('page', 0) page_limit = kwargs.get('page_limit', 10) entity_filter = kwargs.get('entity_filter', None) related = kwargs.get('related', None) cherrypy.response.headers['Content-Type'] = 'application/json' if query is None: log.debug("empty query - creating one") query = [cherrypy.request.remote.ip] referrer = cherrypy.request.headers.get('referrer', None) if referrer is not None: log.debug("including referrer: %s" % referrer) url = urlparse.urlparse(referrer) host = url.netloc if ':' in url.netloc: (host, port) = url.netloc.split(':') for host_part in host.rstrip(self.psl.get_public_suffix(host)).split('.'): if host_part is not None and len(host_part) > 0: query.append(host_part) log.debug("created query: %s" % ",".join(query)) if paged: res, more, total = self.md.search(query, path=q, page=int(page), page_limit=int(page_limit), entity_filter=entity_filter, related=related) # log.debug(dumps({'entities': res, 'more': more, 'total': total})) return dumps({'entities': res, 'more': more, 'total': total}) else: return dumps(self.md.search(query, path=q, entity_filter=entity_filter, related=related)) elif accept.get('text/html'): if not q: if pfx: title = pfx else: title = _("Metadata By Attributes") return render_template("index.html", md=self.md, alias=alias, aliases=self.aliases, title=title) else: entities = self.md.lookup(q) if not entities: raise NotFound() if len(entities) > 1: return render_template("metadata.html", md=self.md, subheading=q, entities=entities) else: entity = entities[0] t = html.fragment_fromstring(unicode(xslt_transform(entity, "entity2html.xsl"))) for c_elt in t.findall(".//code[@role='entity']"): c_txt = dumptree(entity) parser = etree.XMLParser(remove_blank_text=True) src = StringIO(c_txt) tree = etree.parse(src, parser) c_txt = dumptree(tree, pretty_print=True, xml_declaration=False).decode("utf-8") p = c_elt.getparent() p.remove(c_elt) if p.text is not None: p.text += c_txt else: p.text = c_txt xml = dumptree(t, xml_declaration=False).decode('utf-8') return render_template("entity.html", headline=self.md.display(entity).strip(), subheading=entity.get('entityID'), entity_id=entity.get('entityID'), content=xml) else: for p in self.plumbings: state = {'request': True, 'headers': {'Content-Type': 'text/xml'}, 'accept': accept, 'url': cherrypy.url(relative=False), 'select': q, 'path': path, 'stats': {}} r = p.process(self.md, state=state) if r is not None: cache_ttl = state.get('cache', 0) log.debug("caching for %d seconds" % cache_ttl) for k, v in state.get('headers', {}).iteritems(): cherrypy.response.headers[k] = v caching.expires(secs=cache_ttl) return r raise NotFound()
def request(self, **kwargs): """The main request processor. This code implements all rendering of metadata. """ stats['MD Requests'] += 1 pfx = kwargs.get('pfx', None) path = kwargs.get('path', None) content_type = kwargs.get('content_type', None) def escape(m): st = m.group(0) if st == '<': return '<' if st == '>': return '>' return st def _d(x): if x is None or len(x) == 0: return None, None if x.startswith("{base64}"): x = x[8:].decode('base64') if '.' in x: (pth, sep, extn) = x.rpartition('.') return pth, extn else: return x, None _ctypes = {'xml': 'application/xml', 'json': 'application/json', 'htm': 'text/html', 'html': 'text/html', 'ds': 'text/html', 's': 'application/json'} alias = None if pfx: alias = pfx pfx = self.aliases.get(alias, None) if pfx is None: raise NotFound() path, ext = _d(path) if pfx and path: q = "{%s}%s" % (pfx, path) else: q = path log.debug("request path: %s, ext: %s, headers: %s" % (path, ext, cherrypy.request.headers)) accept = {} if content_type is None: if ext is not None and ext in _ctypes: accept = {_ctypes[ext]: True} else: accept = MDServer.MediaAccept() if ext is not None: path = "%s.%s" % (path, ext) else: accept = {content_type: True} with self.lock.readlock: if ext == 'ds': pdict = dict() entity_id = kwargs.get('entityID', None) if entity_id is None: raise HTTPError(400, "400 Bad Request - missing entityID") pdict['sp'] = self.md.sha1_id(entity_id) pdict['ret'] = kwargs.get('return', None) if not path: pdict['search'] = "/search/" else: pdict['search'] = "%s.s" % path if pdict['ret'] is None: raise HTTPError(400, "400 Bad Request - Missing 'return' parameter") pdict['returnIDParam'] = kwargs.get('returnIDParam', 'entityID') cherrypy.response.headers['Content-Type'] = 'text/html' return render_template("ds.html", **pdict) elif ext == 's': paged = bool(kwargs.get('paged', False)) query = kwargs.get('query', None) page = kwargs.get('page', 0) page_limit = kwargs.get('page_limit', 10) entity_filter = kwargs.get('entity_filter', None) cherrypy.response.headers['Content-Type'] = 'application/json' if query is None: log.debug("empty query - creating one") query = [cherrypy.request.remote.ipgit] referrer = cherrypy.request.headers.get('referrer', None) if referrer is not None: log.debug("including referrer: %s" % referrer) url = urlparse.urlparse(referrer) host = url.netloc if ':' in url.netloc: (host, port) = url.netloc.split(':') for host_part in host.rstrip(self.psl.get_public_suffix(host)).split('.'): if host_part is not None and len(host_part) > 0: query.append(host_part) log.debug("created query: %s" % ",".join(query)) if paged: res, more, total = self.md.search(query, path=q, page=int(page), page_limit=int(page_limit), entity_filter=entity_filter) #log.debug(dumps({'entities': res, 'more': more, 'total': total})) return dumps({'entities': res, 'more': more, 'total': total}) else: return dumps(self.md.search(query, path=q, entity_filter=entity_filter)) elif accept.get('text/html'): if not q: if pfx: title = pfx else: title = _("Metadata By Attributes") return render_template("index.html", md=self.md, alias=alias, aliases=self.aliases, title=title) else: entities = self.md.lookup(q) if not entities: raise NotFound() if len(entities) > 1: return render_template("metadata.html", md=self.md, entities=entities) else: entity = entities[0] t = html.fragment_fromstring(unicode(xslt_transform(entity, "entity2html.xsl"))) for c_elt in t.findall(".//code[@role='entity']"): c_txt = dumptree(entity, pretty_print=True, xml_declaration=False).decode("utf-8") p = c_elt.getparent() p.remove(c_elt) if p.text is not None: p.text += c_txt # re.sub(".",escape,c_txt) else: p.text = c_txt # re.sub(".",escape,c_txt) xml = dumptree(t, xml_declaration=False).decode('utf-8') return render_template("basic.html", content=xml) else: for p in self.plumbings: state = {'request': True, 'headers': {'Content-Type': 'text/xml'}, 'accept': accept, 'url': cherrypy.url(relative=False), 'select': q, 'path': path, 'stats': {}} r = p.process(self.md, state=state) if r is not None: cache_ttl = state.get('cache', 0) log.debug("caching for %d seconds" % cache_ttl) for k, v in state.get('headers', {}).iteritems(): cherrypy.response.headers[k] = v caching.expires(secs=cache_ttl) return r raise NotFound()