gavo.formal.nevowc module

Some classes adapting twisted.web (t.w) to make it “basically” work with twisted templates and rend.Pages.

See porting guide in the README.

class gavo.formal.nevowc.CommonRenderers[source]

Bases: object

A container for some basic renderers we want on all active elements.

This is basically what nevow had.


returns data[keyName]; of course, this only works if the current data is a dict.


returns a callable (request, tag) -> something.

If name is a number, this will be tag.slotData[number]. If name contains a blank, name will be split, and data_name will be called with the parts[1:] as arguments to obtain the callable. Else this will just return data_name.

mapping(request, tag)[source]
passthrough(request, tag)[source]

inserts the current data into the stan tree.

That’s nevow’s “data” renderer; but it’s more limited in that t.w’s flattener is more limited.

sequence(request, tag)[source]
string(request, tag)[source]
xml(request, tag)[source]
class gavo.formal.nevowc.MyToStan(sourceFilename)[source]

Bases: _ToStan

A SAX parser unifying nevow and twisted.web namespaces.

We also map invisible (nevow) to transparent (t.w).

startElementNS(namespaceAndName, qName, attrs)[source]

Gets called when we encounter a new xmlns attribute.

@param namespaceAndName: a (namespace, name) tuple, where name

determines which type of action to take, if the namespace matches L{TEMPLATE_NAMESPACE}.

@param qname: ignored. @param attrs: attributes on the element being started.

startPrefixMapping(prefix, uri)[source]

Set up the prefix mapping, which maps fully qualified namespace URIs onto namespace prefixes.

This gets called before startElementNS whenever an C{xmlns} attribute is seen.

class gavo.formal.nevowc.NevowcElement(loader: ~typing.Optional[<InterfaceClass twisted.web.iweb.ITemplateLoader>] = None)[source]

Bases: CommonRenderers, Element

a template.Element that has all our basic renderer functions.

This is probably what you want to base your own elements on.

exception gavo.formal.nevowc.NoDataError[source]

Bases: Exception

is raised when no data can be found for a tag during flattening.

class gavo.formal.nevowc.Passthrough(content)[source]

Bases: Raw

a stan-like tag that inserts its content literally into the target document.

No escaping will we done, so if what you pass in is not XML, you’ll have a malformed result.

This is tags.xml from nevow born again; hence, use it as T.xml from template.tags.

class gavo.formal.nevowc.Redirect(destURL, code=302)[source]

Bases: Resource


Render a given resource. See L{IResource}’s render method.

I delegate to methods of self with the form ‘render_METHOD’ where METHOD is the HTTP that was used to make the request. Examples: render_GET, render_HEAD, render_POST, and so on. Generally you should implement those methods instead of overriding this one.

render_METHOD methods are expected to return a byte string which will be the rendered page, unless the return value is C{server.NOT_DONE_YET}, in which case it is this class’s responsibility to write the results using C{request.write(data)} and then call C{request.finish()}.

Old code that overrides render() directly is likewise expected to return a byte string or NOT_DONE_YET.

@see: L{IResource.render}

class gavo.formal.nevowc.TemplatedPage[source]

Bases: Resource, CommonRenderers

A t.w Resource rendering from its loader attribute.

To actually have the full feature set, be sure to use the XMLFile loader from this module as the loader.

It does not restore renderHTTP or locateChild, as there’s no sane way to keep the interface. Port to t.w-style getChild; if you override render_GET, you probably want to end it with return TemplatedPage.render_GET(self, request).

This will look for a gavo_useDoctype attribute on the template and use an XHTML doctype if it’s not present.

You can override handleError(failure, request) to override the error document (this will finish the request, too).

If you need extra callbacks, define your own render method and use the renderDocument method to obtain a “naked” deferred.

defaultDoctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">'
finishRequest(ignored, request)[source]
getChild(name, request)[source]

Retrieve a ‘child’ resource from me.

Implement this to create dynamic resource generation – resources which are always available may be registered with self.putChild().

This will not be called if the class-level variable ‘isLeaf’ is set in your subclass; instead, the ‘postpath’ attribute of the request will be left as a list of the remaining path elements.

For example, the URL /foo/bar/baz will normally be:

| site.resource.getChild('foo').getChild('bar').getChild('baz').

However, if the resource returned by ‘bar’ has isLeaf set to true, then the getChild call will never be made on it.

Parameters and return value have the same meaning and requirements as those defined by L{IResource.getChildWithDefault}.

handleError(failure, request)[source]
loader = None

returns a deferred firing when the document is written.

Use this method rather than render if you need to introduce extra callbacks.

This does not finish the request by itself.

class gavo.formal.nevowc.XMLFile(path: FilePath)[source]

Bases: XMLFile

a t.w.template.XMLFile able to load nevow templates

…to some extent; we accept both nevow and twisted.web namespace We also unify namespaces prefix on attributes for both to nevow: for simpler handling in later processing. Yes, this will break if someone binds nevow: to some other namespace. Don’t do that, then.

class gavo.formal.nevowc.XMLString(s)[source]

Bases: XMLString

as XMLFile, just for strings.

Again, we override to let us pass in our own parser.

gavo.formal.nevowc.addNevowAttributes(tag, **kwargs)[source]

adds kwargs as n:key to a clone of tag’s attrs.

This is so you can add n:pattern, n:data and friends even within a stan DOM.

gavo.formal.nevowc.elementFromTag(tag, rendererFactory)[source]

returns a t.w.template element rendering tag but using a rendererFactory (a class).

This is used here to furnish the template element with externally defined renderers. You probably won’t need this outside of testing code, as TemplatedPage already arranges everything if you use loader.

gavo.formal.nevowc.flattenSync(element, request=None)[source]

returns a string representation of element synchronously.

This, of course, only works if there are no deferreds within element.

gavo.formal.nevowc.flattenSyncToString(element, request=None)[source]

returns elemented flattened to a string (as opposed to bytes)

gavo.formal.nevowc.iterChildren(tag, stopRenders=frozenset({'mapping', 'sequence'}), stopAtData=False)[source]

yields the Tag-typed descendents of tag preorder.

stopRenderer is a set of renderer names at which traversal does not recurse; this is generally where data “changes” externally for most of our use cases here.

gavo.formal.nevowc.locatePatterns(tag, searchPatterns=[], stopRenders=['sequence', 'mapping'])[source]

returns all descendents of tags for which nevow:pattern is in searchPatterns.

The return value is a dictionary mapping each item in searchPatterns to the pattern name.

This recursively traverses the children of tag, but recursion will stop when tags have nevow:render attributes in stopRenders.