PyXWF.Tweaks – Obtaining and using site-wide configuration

class PyXWF.Tweaks.TweakSitleton(site, tweak_ns=None, tweak_names=[], tweak_hooks=[], **kwargs)[source]

Baseclass for a Sitleton which hooks into tweak nodes from the sitemap. Do not instanciate this directly, it’s of not much use for you.

tweak_ns must be the xml namespace URI of the namespace for which you want to register nodes. Then you have two options of registering hooks for nodes, which can be combined:

  1. tweak_names takes a list of strings and is by default the empty list. Each string you pass will be combined with tweak_ns and passed to the TweakRegistry instance of the site together with the method tweak() _you_ have to declare and implement.

    It is not declared as abstractmethod in this class as this won’t be neccessary for successful use if you use the second method below.

  2. tweak_hooks must be a list of tuples, with the first element being the local-name of the node you want to register a hook for. This is again combined with the tweak_ns to form a fully qualified XML node name. The second element must be a callable which will be called whenever the Site instance hits a node with the respective tag.

Both tweak() and the callables you pass in tweak_hooks must take exactly one positional argument, which will be the node which was encountered. You may do all nasty things with that node, it’s discarded afterwards.

Example:

import PyXWF.Registry as Registry
import PyXWF.Tweaks as Tweaks

class MyFancySitleton(Tweaks.TweakSitleton):
    __metaclass__ = Registry.SitletonMeta  # do not forget this!

    namespace = "http://example.com/my-fancy-sitleton"

    def __init__(self, site):
        super(MyFancySitleton, site).__init__(site,
            tweak_ns=self.namespace,
            tweak_names=["nodeA", "nodeC"]
            tweak_hooks=[
                ("nodeB", self.nodeB)
            ]
        )

    def tweak(self, node):
        # this will be called for each nodeA and nodeC in the namespace
        # encountered in the <site:tweaks /> node of the site.
        pass

    def nodeB(self, node):
        # this will be called for each nodeB in our namespace
        # encountered in the <site:tweaks /> node of the site.
        pass

Previous topic

PyXWF.Sitleton – One instance per site

Next topic

PyXWF.Nodes – Nodes for the sitemap tree

This Page