PyXWF.Nodes – Nodes for the sitemap tree

class PyXWF.Nodes.Node(site, parent, node, **kwargs)[source]

Baseclass for a tree node in the site tree. Use this to create your own site tree plugins. It is highly recommended that you have a look at the existing plugins implementation.

site must be a Site instance. parent must be a Node instance (or None for the root) which is the parent of the node which is to be created.

node may be either a lxml.etree._Element instance or None. This changes the behaviour of the node initialization as follows:

  • If node is not None:

    The attributes @id (default None), @name (default "") and @template (default None) are stored in their respective private object attribute.

    The path of the node is calculated by taking the parents path (if any) and appending a / and the nodes name to it.

  • Otherwise: The private id, name, template and path attributes are initialized to None.

To create a node class you have to use the PyXWF.Registry.NodeMeta class, which requires some attributes:

class CoolNode(Node):
    __metaclass__ = Registry.NodeMeta

    # xml namespace
    namespace = "http://example.com/cool-node"

    # list of xml local-names to register for in the above namespace
    # if you don't want to do something fancy, just use ["node"]
    names = ["node"]

    # your methods go here, like so:
    def do_GET(self, ctx):
        return self.arbitrary_document

    # define which methods handle which HTTP request method
    request_handlers = {
        "GET": do_GET
    }

    # if you have one method to rule them all you can also do:
    request_handlers = do_GET

    # if you want to deny instanciation of your class (not useful):
    request_handlers = None

    # if you want to return a MethodNotAllowed on each request:
    request_handlers = {}

Note that this is not all you need to create a node. For a complete tutorial on how to create nodes see <create-a-plugin-node>.

ID None[source]

Unique string ID of the node (initialized from the @id attribute of the original XML node). This can be used as an argument to get_node() to retrieve the node.

Name None[source]

Path segment name of this node.

Path None[source]

The full path from the application root to this node.

Warning

Do not assume that, while this property is writable, changing it will make the node available at a given path. Path resolution takes place along the chain of nodes from the tree root downwards. So this property is just informational and will be initialized correctly by the node itself or the parent node respectively (if the parent node does something fancy)

Template None[source]

Return the name of the template which is to be used to render the document. This is either the value of the @template attribute of the XML node which initialized this tree node, or the parents Template value.

This is None if @template was not set and the node has no parent or that parent itself has a None value for Template.

get_content_type(ctx)[source]

Return the MIME type of the document returned by this node in the given request context ctx.

get_navigation_info(ctx)[source]

This must be implemented by subclasses which are to be mounted in the tree and return a valid PyXWF.Navigation.Info instance.

handle(ctx)[source]

Handle the request represented by the Context instance ctx.

This tries to look up the value of the Method used for the request in the request_handlers dict. If that fails, MethodNotAllowed is raised. Otherwise, the result (which must be a callable) is called with ctx is the parameter and the result of the call is returned.

iter_upwards(stop_at=None)[source]

Return an iterable which yields the nodes walking tree upwards (following the parent attribute) until that attribute is either None or stop_at (both are not yielded).

resolve_path(ctx, relpath)[source]

Resolve the path relpath relative to the current node using the request Context ctx. In the default implementation, this checks whether relpath is the empty string and returns the node. If relpath is not the empty string, NotFound is raised.

Subclasses override this method to add children to the path, see DirectoryResolutionBehaviour for an example.

class PyXWF.Nodes.DirectoryResolutionBehaviour[source]

Mixin to make a node behave like a directory, regarding the working of resolve_path. For this, the method _get_child must be implemented.

If the path points to the node using this behaviour and is missing a trailing /, a redirect is issued. Otherwise, the next path segment is taken (i.e. the part behind the / which follows the path to the current node but in front of the next /, if any) and a lookup using _get_child is attempted.

Warning

When using this mixin, make sure you mix it into your inheritance hierarchy before the Node class! Otherwise the mixin will not work. Example:

# good
class AwesomeDirectory(DirectoryResolutionBehaviour, Node):
    "stuff goes here"

# bad!
class AwesomeDirectory(Node, DirectoryResolutionBehaviour):
    "stuff goes here"
class PyXWF.Nodes.NodeMeta[source]

Metaclass which deals with enforcing the existance of the request_handlers attribute and its contents.

Note

You don’t use this metaclass usually. To create a complete node class, you need the PyXWF.Registry.NodeMeta metaclass.

Previous topic

PyXWF.Tweaks – Obtaining and using site-wide configuration

Next topic

PyXWF.Crumbs – Crumb baseclass

This Page