_template_util.py 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. twisted.web.util and twisted.web.template merged to avoid cyclic deps
  5. """
  6. import io
  7. import linecache
  8. import warnings
  9. from collections import OrderedDict
  10. from html import escape
  11. from typing import (
  12. IO,
  13. Any,
  14. AnyStr,
  15. Callable,
  16. Dict,
  17. List,
  18. Mapping,
  19. Optional,
  20. Tuple,
  21. Union,
  22. cast,
  23. )
  24. from xml.sax import handler, make_parser
  25. from xml.sax.xmlreader import AttributesNSImpl, Locator
  26. from zope.interface import implementer
  27. from twisted.internet.defer import Deferred
  28. from twisted.logger import Logger
  29. from twisted.python import urlpath
  30. from twisted.python.failure import Failure
  31. from twisted.python.filepath import FilePath
  32. from twisted.python.reflect import fullyQualifiedName
  33. from twisted.web import resource
  34. from twisted.web._element import Element, renderer
  35. from twisted.web._flatten import Flattenable, flatten, flattenString
  36. from twisted.web._stan import CDATA, Comment, Tag, slot
  37. from twisted.web.iweb import IRenderable, IRequest, ITemplateLoader
  38. def _PRE(text):
  39. """
  40. Wraps <pre> tags around some text and HTML-escape it.
  41. This is here since once twisted.web.html was deprecated it was hard to
  42. migrate the html.PRE from current code to twisted.web.template.
  43. For new code consider using twisted.web.template.
  44. @return: Escaped text wrapped in <pre> tags.
  45. @rtype: C{str}
  46. """
  47. return f"<pre>{escape(text)}</pre>"
  48. def redirectTo(URL: bytes, request: IRequest) -> bytes:
  49. """
  50. Generate a redirect to the given location.
  51. @param URL: A L{bytes} giving the location to which to redirect.
  52. @param request: The request object to use to generate the redirect.
  53. @type request: L{IRequest<twisted.web.iweb.IRequest>} provider
  54. @raise TypeError: If the type of C{URL} a L{str} instead of L{bytes}.
  55. @return: A L{bytes} containing HTML which tries to convince the client
  56. agent
  57. to visit the new location even if it doesn't respect the I{FOUND}
  58. response code. This is intended to be returned from a render method,
  59. eg::
  60. def render_GET(self, request):
  61. return redirectTo(b"http://example.com/", request)
  62. """
  63. if not isinstance(URL, bytes):
  64. raise TypeError("URL must be bytes")
  65. request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
  66. request.redirect(URL)
  67. # FIXME: The URL should be HTML-escaped.
  68. # https://twistedmatrix.com/trac/ticket/9839
  69. content = b"""
  70. <html>
  71. <head>
  72. <meta http-equiv=\"refresh\" content=\"0;URL=%(url)s\">
  73. </head>
  74. <body bgcolor=\"#FFFFFF\" text=\"#000000\">
  75. <a href=\"%(url)s\">click here</a>
  76. </body>
  77. </html>
  78. """ % {
  79. b"url": escape(URL.decode("utf-8")).encode("utf-8")
  80. }
  81. return content
  82. class Redirect(resource.Resource):
  83. """
  84. Resource that redirects to a specific URL.
  85. @ivar url: Redirect target URL to put in the I{Location} response header.
  86. @type url: L{bytes}
  87. """
  88. isLeaf = True
  89. def __init__(self, url: bytes):
  90. super().__init__()
  91. self.url = url
  92. def render(self, request):
  93. return redirectTo(self.url, request)
  94. def getChild(self, name, request):
  95. return self
  96. class ParentRedirect(resource.Resource):
  97. """
  98. Redirect to the nearest directory and strip any query string.
  99. This generates redirects like::
  100. / \u2192 /
  101. /foo \u2192 /
  102. /foo?bar \u2192 /
  103. /foo/ \u2192 /foo/
  104. /foo/bar \u2192 /foo/
  105. /foo/bar?baz \u2192 /foo/
  106. However, the generated I{Location} header contains an absolute URL rather
  107. than a path.
  108. The response is the same regardless of HTTP method.
  109. """
  110. isLeaf = 1
  111. def render(self, request: IRequest) -> bytes:
  112. """
  113. Respond to all requests by redirecting to nearest directory.
  114. """
  115. here = str(urlpath.URLPath.fromRequest(request).here()).encode("ascii")
  116. return redirectTo(here, request)
  117. class DeferredResource(resource.Resource):
  118. """
  119. I wrap up a Deferred that will eventually result in a Resource
  120. object.
  121. """
  122. isLeaf = 1
  123. def __init__(self, d):
  124. resource.Resource.__init__(self)
  125. self.d = d
  126. def getChild(self, name, request):
  127. return self
  128. def render(self, request):
  129. self.d.addCallback(self._cbChild, request).addErrback(self._ebChild, request)
  130. from twisted.web.server import NOT_DONE_YET
  131. return NOT_DONE_YET
  132. def _cbChild(self, child, request):
  133. request.render(resource.getChildForRequest(child, request))
  134. def _ebChild(self, reason, request):
  135. request.processingFailed(reason)
  136. class _SourceLineElement(Element):
  137. """
  138. L{_SourceLineElement} is an L{IRenderable} which can render a single line of
  139. source code.
  140. @ivar number: A C{int} giving the line number of the source code to be
  141. rendered.
  142. @ivar source: A C{str} giving the source code to be rendered.
  143. """
  144. def __init__(self, loader, number, source):
  145. Element.__init__(self, loader)
  146. self.number = number
  147. self.source = source
  148. @renderer
  149. def sourceLine(self, request, tag):
  150. """
  151. Render the line of source as a child of C{tag}.
  152. """
  153. return tag(self.source.replace(" ", " \N{NO-BREAK SPACE}"))
  154. @renderer
  155. def lineNumber(self, request, tag):
  156. """
  157. Render the line number as a child of C{tag}.
  158. """
  159. return tag(str(self.number))
  160. class _SourceFragmentElement(Element):
  161. """
  162. L{_SourceFragmentElement} is an L{IRenderable} which can render several lines
  163. of source code near the line number of a particular frame object.
  164. @ivar frame: A L{Failure<twisted.python.failure.Failure>}-style frame object
  165. for which to load a source line to render. This is really a tuple
  166. holding some information from a frame object. See
  167. L{Failure.frames<twisted.python.failure.Failure>} for specifics.
  168. """
  169. def __init__(self, loader, frame):
  170. Element.__init__(self, loader)
  171. self.frame = frame
  172. def _getSourceLines(self):
  173. """
  174. Find the source line references by C{self.frame} and yield, in source
  175. line order, it and the previous and following lines.
  176. @return: A generator which yields two-tuples. Each tuple gives a source
  177. line number and the contents of that source line.
  178. """
  179. filename = self.frame[1]
  180. lineNumber = self.frame[2]
  181. for snipLineNumber in range(lineNumber - 1, lineNumber + 2):
  182. yield (snipLineNumber, linecache.getline(filename, snipLineNumber).rstrip())
  183. @renderer
  184. def sourceLines(self, request, tag):
  185. """
  186. Render the source line indicated by C{self.frame} and several
  187. surrounding lines. The active line will be given a I{class} of
  188. C{"snippetHighlightLine"}. Other lines will be given a I{class} of
  189. C{"snippetLine"}.
  190. """
  191. for lineNumber, sourceLine in self._getSourceLines():
  192. newTag = tag.clone()
  193. if lineNumber == self.frame[2]:
  194. cssClass = "snippetHighlightLine"
  195. else:
  196. cssClass = "snippetLine"
  197. loader = TagLoader(newTag(**{"class": cssClass}))
  198. yield _SourceLineElement(loader, lineNumber, sourceLine)
  199. class _FrameElement(Element):
  200. """
  201. L{_FrameElement} is an L{IRenderable} which can render details about one
  202. frame from a L{Failure<twisted.python.failure.Failure>}.
  203. @ivar frame: A L{Failure<twisted.python.failure.Failure>}-style frame object
  204. for which to load a source line to render. This is really a tuple
  205. holding some information from a frame object. See
  206. L{Failure.frames<twisted.python.failure.Failure>} for specifics.
  207. """
  208. def __init__(self, loader, frame):
  209. Element.__init__(self, loader)
  210. self.frame = frame
  211. @renderer
  212. def filename(self, request, tag):
  213. """
  214. Render the name of the file this frame references as a child of C{tag}.
  215. """
  216. return tag(self.frame[1])
  217. @renderer
  218. def lineNumber(self, request, tag):
  219. """
  220. Render the source line number this frame references as a child of
  221. C{tag}.
  222. """
  223. return tag(str(self.frame[2]))
  224. @renderer
  225. def function(self, request, tag):
  226. """
  227. Render the function name this frame references as a child of C{tag}.
  228. """
  229. return tag(self.frame[0])
  230. @renderer
  231. def source(self, request, tag):
  232. """
  233. Render the source code surrounding the line this frame references,
  234. replacing C{tag}.
  235. """
  236. return _SourceFragmentElement(TagLoader(tag), self.frame)
  237. class _StackElement(Element):
  238. """
  239. L{_StackElement} renders an L{IRenderable} which can render a list of frames.
  240. """
  241. def __init__(self, loader, stackFrames):
  242. Element.__init__(self, loader)
  243. self.stackFrames = stackFrames
  244. @renderer
  245. def frames(self, request, tag):
  246. """
  247. Render the list of frames in this L{_StackElement}, replacing C{tag}.
  248. """
  249. return [
  250. _FrameElement(TagLoader(tag.clone()), frame) for frame in self.stackFrames
  251. ]
  252. class _NSContext:
  253. """
  254. A mapping from XML namespaces onto their prefixes in the document.
  255. """
  256. def __init__(self, parent: Optional["_NSContext"] = None):
  257. """
  258. Pull out the parent's namespaces, if there's no parent then default to
  259. XML.
  260. """
  261. self.parent = parent
  262. if parent is not None:
  263. self.nss: Dict[Optional[str], Optional[str]] = OrderedDict(parent.nss)
  264. else:
  265. self.nss = {"http://www.w3.org/XML/1998/namespace": "xml"}
  266. def get(self, k: Optional[str], d: Optional[str] = None) -> Optional[str]:
  267. """
  268. Get a prefix for a namespace.
  269. @param d: The default prefix value.
  270. """
  271. return self.nss.get(k, d)
  272. def __setitem__(self, k: Optional[str], v: Optional[str]) -> None:
  273. """
  274. Proxy through to setting the prefix for the namespace.
  275. """
  276. self.nss.__setitem__(k, v)
  277. def __getitem__(self, k: Optional[str]) -> Optional[str]:
  278. """
  279. Proxy through to getting the prefix for the namespace.
  280. """
  281. return self.nss.__getitem__(k)
  282. TEMPLATE_NAMESPACE = "http://twistedmatrix.com/ns/twisted.web.template/0.1"
  283. class _ToStan(handler.ContentHandler, handler.EntityResolver):
  284. """
  285. A SAX parser which converts an XML document to the Twisted STAN
  286. Document Object Model.
  287. """
  288. def __init__(self, sourceFilename: Optional[str]):
  289. """
  290. @param sourceFilename: the filename the XML was loaded out of.
  291. """
  292. self.sourceFilename = sourceFilename
  293. self.prefixMap = _NSContext()
  294. self.inCDATA = False
  295. def setDocumentLocator(self, locator: Locator) -> None:
  296. """
  297. Set the document locator, which knows about line and character numbers.
  298. """
  299. self.locator = locator
  300. def startDocument(self) -> None:
  301. """
  302. Initialise the document.
  303. """
  304. # Depending on our active context, the element type can be Tag, slot
  305. # or str. Since mypy doesn't understand that context, it would be
  306. # a pain to not use Any here.
  307. self.document: List[Any] = []
  308. self.current = self.document
  309. self.stack: List[Any] = []
  310. self.xmlnsAttrs: List[Tuple[str, str]] = []
  311. def endDocument(self) -> None:
  312. """
  313. Document ended.
  314. """
  315. def processingInstruction(self, target: str, data: str) -> None:
  316. """
  317. Processing instructions are ignored.
  318. """
  319. def startPrefixMapping(self, prefix: Optional[str], uri: str) -> None:
  320. """
  321. Set up the prefix mapping, which maps fully qualified namespace URIs
  322. onto namespace prefixes.
  323. This gets called before startElementNS whenever an C{xmlns} attribute
  324. is seen.
  325. """
  326. self.prefixMap = _NSContext(self.prefixMap)
  327. self.prefixMap[uri] = prefix
  328. # Ignore the template namespace; we'll replace those during parsing.
  329. if uri == TEMPLATE_NAMESPACE:
  330. return
  331. # Add to a list that will be applied once we have the element.
  332. if prefix is None:
  333. self.xmlnsAttrs.append(("xmlns", uri))
  334. else:
  335. self.xmlnsAttrs.append(("xmlns:%s" % prefix, uri))
  336. def endPrefixMapping(self, prefix: Optional[str]) -> None:
  337. """
  338. "Pops the stack" on the prefix mapping.
  339. Gets called after endElementNS.
  340. """
  341. parent = self.prefixMap.parent
  342. assert parent is not None, "More prefix mapping ends than starts"
  343. self.prefixMap = parent
  344. def startElementNS(
  345. self,
  346. namespaceAndName: Tuple[str, str],
  347. qname: Optional[str],
  348. attrs: AttributesNSImpl,
  349. ) -> None:
  350. """
  351. Gets called when we encounter a new xmlns attribute.
  352. @param namespaceAndName: a (namespace, name) tuple, where name
  353. determines which type of action to take, if the namespace matches
  354. L{TEMPLATE_NAMESPACE}.
  355. @param qname: ignored.
  356. @param attrs: attributes on the element being started.
  357. """
  358. filename = self.sourceFilename
  359. lineNumber = self.locator.getLineNumber()
  360. columnNumber = self.locator.getColumnNumber()
  361. ns, name = namespaceAndName
  362. if ns == TEMPLATE_NAMESPACE:
  363. if name == "transparent":
  364. name = ""
  365. elif name == "slot":
  366. default: Optional[str]
  367. try:
  368. # Try to get the default value for the slot
  369. default = attrs[(None, "default")]
  370. except KeyError:
  371. # If there wasn't one, then use None to indicate no
  372. # default.
  373. default = None
  374. sl = slot(
  375. attrs[(None, "name")],
  376. default=default,
  377. filename=filename,
  378. lineNumber=lineNumber,
  379. columnNumber=columnNumber,
  380. )
  381. self.stack.append(sl)
  382. self.current.append(sl)
  383. self.current = sl.children
  384. return
  385. render = None
  386. ordered = OrderedDict(attrs)
  387. for k, v in list(ordered.items()):
  388. attrNS, justTheName = k
  389. if attrNS != TEMPLATE_NAMESPACE:
  390. continue
  391. if justTheName == "render":
  392. render = v
  393. del ordered[k]
  394. # nonTemplateAttrs is a dictionary mapping attributes that are *not* in
  395. # TEMPLATE_NAMESPACE to their values. Those in TEMPLATE_NAMESPACE were
  396. # just removed from 'attrs' in the loop immediately above. The key in
  397. # nonTemplateAttrs is either simply the attribute name (if it was not
  398. # specified as having a namespace in the template) or prefix:name,
  399. # preserving the xml namespace prefix given in the document.
  400. nonTemplateAttrs = OrderedDict()
  401. for (attrNs, attrName), v in ordered.items():
  402. nsPrefix = self.prefixMap.get(attrNs)
  403. if nsPrefix is None:
  404. attrKey = attrName
  405. else:
  406. attrKey = f"{nsPrefix}:{attrName}"
  407. nonTemplateAttrs[attrKey] = v
  408. if ns == TEMPLATE_NAMESPACE and name == "attr":
  409. if not self.stack:
  410. # TODO: define a better exception for this?
  411. raise AssertionError(
  412. f"<{{{TEMPLATE_NAMESPACE}}}attr> as top-level element"
  413. )
  414. if "name" not in nonTemplateAttrs:
  415. # TODO: same here
  416. raise AssertionError(
  417. f"<{{{TEMPLATE_NAMESPACE}}}attr> requires a name attribute"
  418. )
  419. el = Tag(
  420. "",
  421. render=render,
  422. filename=filename,
  423. lineNumber=lineNumber,
  424. columnNumber=columnNumber,
  425. )
  426. self.stack[-1].attributes[nonTemplateAttrs["name"]] = el
  427. self.stack.append(el)
  428. self.current = el.children
  429. return
  430. # Apply any xmlns attributes
  431. if self.xmlnsAttrs:
  432. nonTemplateAttrs.update(OrderedDict(self.xmlnsAttrs))
  433. self.xmlnsAttrs = []
  434. # Add the prefix that was used in the parsed template for non-template
  435. # namespaces (which will not be consumed anyway).
  436. if ns != TEMPLATE_NAMESPACE and ns is not None:
  437. prefix = self.prefixMap[ns]
  438. if prefix is not None:
  439. name = f"{self.prefixMap[ns]}:{name}"
  440. el = Tag(
  441. name,
  442. attributes=OrderedDict(
  443. cast(Mapping[Union[bytes, str], str], nonTemplateAttrs)
  444. ),
  445. render=render,
  446. filename=filename,
  447. lineNumber=lineNumber,
  448. columnNumber=columnNumber,
  449. )
  450. self.stack.append(el)
  451. self.current.append(el)
  452. self.current = el.children
  453. def characters(self, ch: str) -> None:
  454. """
  455. Called when we receive some characters. CDATA characters get passed
  456. through as is.
  457. """
  458. if self.inCDATA:
  459. self.stack[-1].append(ch)
  460. return
  461. self.current.append(ch)
  462. def endElementNS(self, name: Tuple[str, str], qname: Optional[str]) -> None:
  463. """
  464. A namespace tag is closed. Pop the stack, if there's anything left in
  465. it, otherwise return to the document's namespace.
  466. """
  467. self.stack.pop()
  468. if self.stack:
  469. self.current = self.stack[-1].children
  470. else:
  471. self.current = self.document
  472. def startDTD(self, name: str, publicId: str, systemId: str) -> None:
  473. """
  474. DTDs are ignored.
  475. """
  476. def endDTD(self, *args: object) -> None:
  477. """
  478. DTDs are ignored.
  479. """
  480. def startCDATA(self) -> None:
  481. """
  482. We're starting to be in a CDATA element, make a note of this.
  483. """
  484. self.inCDATA = True
  485. self.stack.append([])
  486. def endCDATA(self) -> None:
  487. """
  488. We're no longer in a CDATA element. Collect up the characters we've
  489. parsed and put them in a new CDATA object.
  490. """
  491. self.inCDATA = False
  492. comment = "".join(self.stack.pop())
  493. self.current.append(CDATA(comment))
  494. def comment(self, content: str) -> None:
  495. """
  496. Add an XML comment which we've encountered.
  497. """
  498. self.current.append(Comment(content))
  499. def _flatsaxParse(fl: Union[IO[AnyStr], str]) -> List["Flattenable"]:
  500. """
  501. Perform a SAX parse of an XML document with the _ToStan class.
  502. @param fl: The XML document to be parsed.
  503. @return: a C{list} of Stan objects.
  504. """
  505. parser = make_parser()
  506. parser.setFeature(handler.feature_validation, 0)
  507. parser.setFeature(handler.feature_namespaces, 1)
  508. parser.setFeature(handler.feature_external_ges, 0)
  509. parser.setFeature(handler.feature_external_pes, 0)
  510. s = _ToStan(getattr(fl, "name", None))
  511. parser.setContentHandler(s)
  512. parser.setEntityResolver(s)
  513. parser.setProperty(handler.property_lexical_handler, s)
  514. parser.parse(fl)
  515. return s.document
  516. @implementer(ITemplateLoader)
  517. class XMLString:
  518. """
  519. An L{ITemplateLoader} that loads and parses XML from a string.
  520. """
  521. def __init__(self, s: Union[str, bytes]):
  522. """
  523. Run the parser on a L{io.StringIO} copy of the string.
  524. @param s: The string from which to load the XML.
  525. @type s: L{str}, or a UTF-8 encoded L{bytes}.
  526. """
  527. if not isinstance(s, str):
  528. s = s.decode("utf8")
  529. self._loadedTemplate: List["Flattenable"] = _flatsaxParse(io.StringIO(s))
  530. """The loaded document."""
  531. def load(self) -> List["Flattenable"]:
  532. """
  533. Return the document.
  534. @return: the loaded document.
  535. """
  536. return self._loadedTemplate
  537. class FailureElement(Element):
  538. """
  539. L{FailureElement} is an L{IRenderable} which can render detailed information
  540. about a L{Failure<twisted.python.failure.Failure>}.
  541. @ivar failure: The L{Failure<twisted.python.failure.Failure>} instance which
  542. will be rendered.
  543. @since: 12.1
  544. """
  545. loader = XMLString(
  546. """
  547. <div xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
  548. <style type="text/css">
  549. div.error {
  550. color: red;
  551. font-family: Verdana, Arial, helvetica, sans-serif;
  552. font-weight: bold;
  553. }
  554. div {
  555. font-family: Verdana, Arial, helvetica, sans-serif;
  556. }
  557. div.stackTrace {
  558. }
  559. div.frame {
  560. padding: 1em;
  561. background: white;
  562. border-bottom: thin black dashed;
  563. }
  564. div.frame:first-child {
  565. padding: 1em;
  566. background: white;
  567. border-top: thin black dashed;
  568. border-bottom: thin black dashed;
  569. }
  570. div.location {
  571. }
  572. span.function {
  573. font-weight: bold;
  574. font-family: "Courier New", courier, monospace;
  575. }
  576. div.snippet {
  577. margin-bottom: 0.5em;
  578. margin-left: 1em;
  579. background: #FFFFDD;
  580. }
  581. div.snippetHighlightLine {
  582. color: red;
  583. }
  584. span.code {
  585. font-family: "Courier New", courier, monospace;
  586. }
  587. </style>
  588. <div class="error">
  589. <span t:render="type" />: <span t:render="value" />
  590. </div>
  591. <div class="stackTrace" t:render="traceback">
  592. <div class="frame" t:render="frames">
  593. <div class="location">
  594. <span t:render="filename" />:<span t:render="lineNumber" /> in
  595. <span class="function" t:render="function" />
  596. </div>
  597. <div class="snippet" t:render="source">
  598. <div t:render="sourceLines">
  599. <span class="lineno" t:render="lineNumber" />
  600. <code class="code" t:render="sourceLine" />
  601. </div>
  602. </div>
  603. </div>
  604. </div>
  605. <div class="error">
  606. <span t:render="type" />: <span t:render="value" />
  607. </div>
  608. </div>
  609. """
  610. )
  611. def __init__(self, failure, loader=None):
  612. Element.__init__(self, loader)
  613. self.failure = failure
  614. @renderer
  615. def type(self, request, tag):
  616. """
  617. Render the exception type as a child of C{tag}.
  618. """
  619. return tag(fullyQualifiedName(self.failure.type))
  620. @renderer
  621. def value(self, request, tag):
  622. """
  623. Render the exception value as a child of C{tag}.
  624. """
  625. return tag(str(self.failure.value).encode("utf8"))
  626. @renderer
  627. def traceback(self, request, tag):
  628. """
  629. Render all the frames in the wrapped
  630. L{Failure<twisted.python.failure.Failure>}'s traceback stack, replacing
  631. C{tag}.
  632. """
  633. return _StackElement(TagLoader(tag), self.failure.frames)
  634. def formatFailure(myFailure):
  635. """
  636. Construct an HTML representation of the given failure.
  637. Consider using L{FailureElement} instead.
  638. @type myFailure: L{Failure<twisted.python.failure.Failure>}
  639. @rtype: L{bytes}
  640. @return: A string containing the HTML representation of the given failure.
  641. """
  642. result = []
  643. flattenString(None, FailureElement(myFailure)).addBoth(result.append)
  644. if isinstance(result[0], bytes):
  645. # Ensure the result string is all ASCII, for compatibility with the
  646. # default encoding expected by browsers.
  647. return result[0].decode("utf-8").encode("ascii", "xmlcharrefreplace")
  648. result[0].raiseException()
  649. # Go read the definition of NOT_DONE_YET. For lulz. This is totally
  650. # equivalent. And this turns out to be necessary, because trying to import
  651. # NOT_DONE_YET in this module causes a circular import which we cannot escape
  652. # from. From which we cannot escape. Etc. glyph is okay with this solution for
  653. # now, and so am I, as long as this comment stays to explain to future
  654. # maintainers what it means. ~ C.
  655. #
  656. # See http://twistedmatrix.com/trac/ticket/5557 for progress on fixing this.
  657. NOT_DONE_YET = 1
  658. _moduleLog = Logger()
  659. @implementer(ITemplateLoader)
  660. class TagLoader:
  661. """
  662. An L{ITemplateLoader} that loads an existing flattenable object.
  663. """
  664. def __init__(self, tag: "Flattenable"):
  665. """
  666. @param tag: The object which will be loaded.
  667. """
  668. self.tag: "Flattenable" = tag
  669. """The object which will be loaded."""
  670. def load(self) -> List["Flattenable"]:
  671. return [self.tag]
  672. @implementer(ITemplateLoader)
  673. class XMLFile:
  674. """
  675. An L{ITemplateLoader} that loads and parses XML from a file.
  676. """
  677. def __init__(self, path: FilePath[Any]):
  678. """
  679. Run the parser on a file.
  680. @param path: The file from which to load the XML.
  681. """
  682. if not isinstance(path, FilePath):
  683. warnings.warn( # type: ignore[unreachable]
  684. "Passing filenames or file objects to XMLFile is deprecated "
  685. "since Twisted 12.1. Pass a FilePath instead.",
  686. category=DeprecationWarning,
  687. stacklevel=2,
  688. )
  689. self._loadedTemplate: Optional[List["Flattenable"]] = None
  690. """The loaded document, or L{None}, if not loaded."""
  691. self._path: FilePath[Any] = path
  692. """The file that is being loaded from."""
  693. def _loadDoc(self) -> List["Flattenable"]:
  694. """
  695. Read and parse the XML.
  696. @return: the loaded document.
  697. """
  698. if not isinstance(self._path, FilePath):
  699. return _flatsaxParse(self._path) # type: ignore[unreachable]
  700. else:
  701. with self._path.open("r") as f:
  702. return _flatsaxParse(f)
  703. def __repr__(self) -> str:
  704. return f"<XMLFile of {self._path!r}>"
  705. def load(self) -> List["Flattenable"]:
  706. """
  707. Return the document, first loading it if necessary.
  708. @return: the loaded document.
  709. """
  710. if self._loadedTemplate is None:
  711. self._loadedTemplate = self._loadDoc()
  712. return self._loadedTemplate
  713. # Last updated October 2011, using W3Schools as a reference. Link:
  714. # http://www.w3schools.com/html5/html5_reference.asp
  715. # Note that <xmp> is explicitly omitted; its semantics do not work with
  716. # t.w.template and it is officially deprecated.
  717. VALID_HTML_TAG_NAMES = {
  718. "a",
  719. "abbr",
  720. "acronym",
  721. "address",
  722. "applet",
  723. "area",
  724. "article",
  725. "aside",
  726. "audio",
  727. "b",
  728. "base",
  729. "basefont",
  730. "bdi",
  731. "bdo",
  732. "big",
  733. "blockquote",
  734. "body",
  735. "br",
  736. "button",
  737. "canvas",
  738. "caption",
  739. "center",
  740. "cite",
  741. "code",
  742. "col",
  743. "colgroup",
  744. "command",
  745. "datalist",
  746. "dd",
  747. "del",
  748. "details",
  749. "dfn",
  750. "dir",
  751. "div",
  752. "dl",
  753. "dt",
  754. "em",
  755. "embed",
  756. "fieldset",
  757. "figcaption",
  758. "figure",
  759. "font",
  760. "footer",
  761. "form",
  762. "frame",
  763. "frameset",
  764. "h1",
  765. "h2",
  766. "h3",
  767. "h4",
  768. "h5",
  769. "h6",
  770. "head",
  771. "header",
  772. "hgroup",
  773. "hr",
  774. "html",
  775. "i",
  776. "iframe",
  777. "img",
  778. "input",
  779. "ins",
  780. "isindex",
  781. "keygen",
  782. "kbd",
  783. "label",
  784. "legend",
  785. "li",
  786. "link",
  787. "map",
  788. "mark",
  789. "menu",
  790. "meta",
  791. "meter",
  792. "nav",
  793. "noframes",
  794. "noscript",
  795. "object",
  796. "ol",
  797. "optgroup",
  798. "option",
  799. "output",
  800. "p",
  801. "param",
  802. "pre",
  803. "progress",
  804. "q",
  805. "rp",
  806. "rt",
  807. "ruby",
  808. "s",
  809. "samp",
  810. "script",
  811. "section",
  812. "select",
  813. "small",
  814. "source",
  815. "span",
  816. "strike",
  817. "strong",
  818. "style",
  819. "sub",
  820. "summary",
  821. "sup",
  822. "table",
  823. "tbody",
  824. "td",
  825. "textarea",
  826. "tfoot",
  827. "th",
  828. "thead",
  829. "time",
  830. "title",
  831. "tr",
  832. "tt",
  833. "u",
  834. "ul",
  835. "var",
  836. "video",
  837. "wbr",
  838. }
  839. class _TagFactory:
  840. """
  841. A factory for L{Tag} objects; the implementation of the L{tags} object.
  842. This allows for the syntactic convenience of C{from twisted.web.template
  843. import tags; tags.a(href="linked-page.html")}, where 'a' can be basically
  844. any HTML tag.
  845. The class is not exposed publicly because you only ever need one of these,
  846. and we already made it for you.
  847. @see: L{tags}
  848. """
  849. def __getattr__(self, tagName: str) -> Tag:
  850. if tagName == "transparent":
  851. return Tag("")
  852. # allow for E.del as E.del_
  853. tagName = tagName.rstrip("_")
  854. if tagName not in VALID_HTML_TAG_NAMES:
  855. raise AttributeError(f"unknown tag {tagName!r}")
  856. return Tag(tagName)
  857. tags = _TagFactory()
  858. def renderElement(
  859. request: IRequest,
  860. element: IRenderable,
  861. doctype: Optional[bytes] = b"<!DOCTYPE html>",
  862. _failElement: Optional[Callable[[Failure], "Element"]] = None,
  863. ) -> object:
  864. """
  865. Render an element or other L{IRenderable}.
  866. @param request: The L{IRequest} being rendered to.
  867. @param element: An L{IRenderable} which will be rendered.
  868. @param doctype: A L{bytes} which will be written as the first line of
  869. the request, or L{None} to disable writing of a doctype. The argument
  870. should not include a trailing newline and will default to the HTML5
  871. doctype C{'<!DOCTYPE html>'}.
  872. @returns: NOT_DONE_YET
  873. @since: 12.1
  874. """
  875. if doctype is not None:
  876. request.write(doctype)
  877. request.write(b"\n")
  878. if _failElement is None:
  879. _failElement = FailureElement
  880. d = flatten(request, element, request.write)
  881. def eb(failure: Failure) -> Optional[Deferred[None]]:
  882. _moduleLog.failure(
  883. "An error occurred while rendering the response.", failure=failure
  884. )
  885. site = getattr(request, "site", None)
  886. if site is not None and site.displayTracebacks:
  887. assert _failElement is not None
  888. return flatten(request, _failElement(failure), request.write)
  889. else:
  890. request.write(
  891. b'<div style="font-size:800%;'
  892. b"background-color:#FFF;"
  893. b"color:#F00"
  894. b'">An error occurred while rendering the response.</div>'
  895. )
  896. return None
  897. def finish(result: object, *, request: IRequest = request) -> object:
  898. request.finish()
  899. return result
  900. d.addErrback(eb)
  901. d.addBoth(finish)
  902. return NOT_DONE_YET