Debian Codegraph @

gavo/svcs/renderers.py

raw
1 """
2 A manual registry of renderers, plus some helper code to produce HTML.
3
4 Renderers are the glue between a core and some output. A service is the
5 combination of a subset of renderers and a core.
6
7 Renderers are actually defined in web.grend, but we need some way to
8 get at them from svcs and above, so the registry is kept here.
9
10 We also have doctypedStan here and CommonRenderers; that's admittedly
11 mainly for convenience.
12 """
13
14 #c Copyright 2008-2025, the GAVO project <[email protected]>
15 #c
16 #c This program is free software, covered by the GNU GPL. See the
17 #c COPYING file in the source distribution.
18
19
20 import functools
21 import re
22 from urllib import parse as urlparse
23
24 from twisted.web.template import tags as T
25
26 from twisted.web import template
27
28 from gavo import base
29 from gavo import utils
30
31 JSEXT = ".js"
32
33 RENDERER_REGISTRY = {
34 'admin': ("web.adminrender", "AdminRenderer"),
35 'api': ("web.vodal", "APIRenderer"),
36 'async': ("web.asyncrender", "DALIAsyncRenderer"),
37 'availability': ("web.vosi", "VOSIAvailabilityRenderer"),
38 'biblinks.json': ("web.vodal","BiblinksRenderer"),
39 'capabilities': ("web.vosi", "VOSICapabilityRenderer"),
40 'coverage': ("web.metarender", "CoverageRenderer"),
41 'custom': ("web.customrender", "CustomRenderer"),
42 'dali': ("web.syncrender", "DALIRenderer"),
43 'dlget': ("web.vodal", "DatalinkGetDataRenderer"),
44 'dlasync': ("web.asyncrender", "DatalinkAsyncRenderer"),
45 'dlmeta': ("web.vodal", "DatalinkGetMetaRenderer"),
46 'edition': ("web.metarender", "EditionRenderer"),
47 'examples': ("web.examplesrender", "Examples"),
48 'external': ("web.metarender", "ExternalRenderer"),
49 'fixed': ("web.constantrender", "FixedPageRenderer"),
50 'form': ("web.formrender", "FormRenderer"),
51 'get': ("web.productrender", "ProductRenderer"),
52 'hips': ("web.constantrender", "HiPSRenderer"),
53 'howtocite': ("web.metarender", "HowToCiteRenderer"),
54 'info': ("web.metarender", "ServiceInfoRenderer"),
55 'mupload': ("web.uploadservice", "MachineUploader"),
56 'pubreg.xml': ("web.vodal", "RegistryRenderer"),
57 'qp': ("web.qprenderer", "QPRenderer"),
58 'rdinfo': ("web.metarender", "RDInfoRenderer"),
59 'scs.xml': ("web.vodal", "SCSRenderer"),
60 'siap.xml': ("web.vodal", "SIAPRenderer"),
61 'siap2.xml': ("web.vodal", "SIAP2Renderer"),
62 'slap.xml': ("web.vodal", "SLAPRenderer"),
63 'soap': ("web.soaprender", "SOAPRenderer"),
64 'ssap.xml': ("web.vodal", "SSAPRenderer"),
65 'static': ("web.constantrender", "StaticRenderer"),
66 'sync': ("web.syncrender", "SyncRenderer"),
67 'tableinfo': ("web.metarender", "TableInfoRenderer"),
68 'tableMetadata': ("web.vosi", "VOSITablesetRenderer"),
69 'tables': ("web.vosi", "VOSITablesetRenderer"),
70 'tablenote': ("web.metarender", "TableNoteRenderer"),
71 'user_tables': ("web.tap_uploads", "PersistentUploadRenderer"),
72 'upload': ("web.uploadservice", "Uploader"),
73 'uws.xml': ("web.asyncrender", "DALIAsyncRenderer"),
74 'voevent': ("web.metarender", "VOEventRenderer"),
75 'volatile': ("web.constantrender", "VolatilePageRenderer"),
76 }
77
78
79 @functools.lru_cache()
80 def getRenderer(name):
81 if name not in RENDERER_REGISTRY:
82 raise base.NotFoundError(name, "renderer", "registered renderers")
83 cls = utils.loadInternalObject(*RENDERER_REGISTRY[name])
84
85 if cls.name!=name and not name in cls.aliases:
86 raise base.ReportableError("Internal Error: Renderer %s is registered"
87 " under the wrong name."%name,
88 hint="This is probably a typo in svcs.renderers; it needs"
89 " to be fixed there")
90 return cls
91
92
93 class CommonRenderers(object):
94 """A base for renderer (python) mixins within the DaCHS
95
96 Including standard stylesheets/js/whatever:
97 <head n:render="commonhead">...</head>
98
99 Rendering internal links (important for off-root operation):
100
101 * <tag href|src="/foo" n:render="rootlink"/>
102
103 """
104 @template.renderer
105 def unicode(self, request, tag):
106 """returns unicode(data); this is deprecated in favour of string.
107 """
108 return tag[str(tag.slotData)]
109
110 @template.renderer
111 def rootlink(self, request, tag):
112 def munge(key):
113 if key in tag.attributes:
114 tag.attributes[key] = base.makeSitePath(tag.attributes[key])
115 munge("src")
116 munge("href")
117 return tag
118
119 @template.renderer
120 def commonhead(self, request, tag):
121 # we do not want to blindly append these things to the tag since user-
122 # provided items in the header should have access to this environment,
123 # in particular to jquery; thus, this stuff must be as early as possible.
124 originalChildren = tag.children[:]
125 tag.clear()
126 res = tag[
127 T.meta(**{"http-equiv": "Content-type",
128 "content": "text/html;charset=UTF-8"}),
129 T.link(rel="stylesheet",
130 href=base.makeSitePath("/static/css/formal.css"),
131 type="text/css"),
132 T.link(rel="stylesheet", href=base.makeSitePath(
133 "/static/css/gavo_dc.css"), type="text/css"),
134 T.script(src=base.makeSitePath("/static/js/jquery-gavo.js"),
135 type="text/javascript"),
136 T.script(src=base.makeSitePath("/static/js/gavo"+JSEXT),
137 type="text/javascript"),
138 originalChildren,
139 ]
140 if base.getConfig("web", "operatorCSS"):
141 res[
142 T.link(rel="stylesheet", type="text/css",
143 href=base.getConfig("web", "operatorCSS"))]
144 return res
145
146 @template.renderer
147 def urlescape(self, request, tag):
148 """renders data as a url-escaped string.
149 """
150 data = tag.slotData
151 return urlparse.quote(data)
152
153 @template.renderer
154 def getconfig(self, request, tag):
155 """looks up the text child in the DaCHS configuration and inserts
156 the value as a (unicode) string.
157
158 The config key is either [section]item or just item for something
159 in [general]. Behaviour for undefined config items is undefined.
160 """
161 configKey = tag.children[0].strip()
162 mat = re.match(r"\[([^]]*)\](.*)", configKey)
163 if mat:
164 content = base.getConfig(mat.group(1), mat.group(2))
165 else:
166 content = base.getConfig(configKey)
167 tag.clear()
168 return tag[content]
169
170
171 class doctypedStan(template.TagLoader):
172 """is the TagLoader loader that arranges for a an XHTML doctype and
173 namespace.
174
175 As this relies on a magic attribute gavo_useDoctype, this will only
176 work if the template will be rendered through nevowc.TemplatedPage.
177 """
178
179 DOCTYPE = ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
180 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
181
182 def __init__(self, rootEl):
183 rootEl(xmlns="http://www.w3.org/1999/xhtml")
184 rootEl.gavo_useDoctype = self.DOCTYPE
185 template.TagLoader.__init__(self, rootEl)