1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/WebStack/Resources/Selectors.py Sat Jan 06 20:30:32 2007 +0000
1.3 @@ -0,0 +1,71 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Resources which "select" other resources, sometimes causing desirable
1.8 +side-effects.
1.9 +
1.10 +Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
1.11 +
1.12 +This library is free software; you can redistribute it and/or
1.13 +modify it under the terms of the GNU Lesser General Public
1.14 +License as published by the Free Software Foundation; either
1.15 +version 2.1 of the License, or (at your option) any later version.
1.16 +
1.17 +This library is distributed in the hope that it will be useful,
1.18 +but WITHOUT ANY WARRANTY; without even the implied warranty of
1.19 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.20 +Lesser General Public License for more details.
1.21 +
1.22 +You should have received a copy of the GNU Lesser General Public
1.23 +License along with this library; if not, write to the Free Software
1.24 +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
1.25 +"""
1.26 +
1.27 +class PathSelector:
1.28 +
1.29 + "Set a request's current path and processed path info on an attribute."
1.30 +
1.31 + def __init__(self, resource, add_slash=1, attribute_name="root", path_encoding="utf-8"):
1.32 +
1.33 + """
1.34 + Initialise the selector with a 'resource' (to which all requests shall
1.35 + be forwarded), specifying whether a "/" character shall be added to
1.36 + stored paths using the optional 'add_slash' parameter (default is true),
1.37 + along with an optional 'attribute_name' (indicating the name of the
1.38 + attribute on which the path information shall be stored), and the
1.39 + optional 'path_encoding' for interpreting URL-encoded path values.
1.40 + """
1.41 +
1.42 + self.resource = resource
1.43 + self.add_slash = add_slash
1.44 + self.attribute_name = attribute_name
1.45 + self.path_encoding = path_encoding
1.46 +
1.47 + def _slash(self):
1.48 + if self.add_slash:
1.49 + return "/"
1.50 + else:
1.51 + return ""
1.52 +
1.53 + def respond(self, trans):
1.54 +
1.55 + """
1.56 + Respond to the transaction 'trans' by storing the current path and
1.57 + processed virtual path info on the named transaction attribute.
1.58 + """
1.59 +
1.60 + pwi = trans.get_path_without_info(self.path_encoding)
1.61 +
1.62 + # Make a note of the path given the following general rule:
1.63 + # path_without_info + path_info
1.64 + # == path_without_info + processed_virtual_path_info + virtual_path_info
1.65 +
1.66 + attributes = trans.get_attributes()
1.67 + attributes[self.attribute_name] = trans.encode_path(
1.68 + pwi + trans.get_processed_virtual_path_info(self.path_encoding) + self._slash(),
1.69 + self.path_encoding
1.70 + )
1.71 +
1.72 + self.resource.respond(trans)
1.73 +
1.74 +# vim: tabstop=4 expandtab shiftwidth=4