paulb@599 | 1 | #!/usr/bin/env python |
paulb@599 | 2 | |
paulb@599 | 3 | """ |
paulb@599 | 4 | Resources which "select" other resources, sometimes causing desirable |
paulb@599 | 5 | side-effects. |
paulb@599 | 6 | |
paulb@599 | 7 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@599 | 8 | |
paulb@599 | 9 | This library is free software; you can redistribute it and/or |
paulb@599 | 10 | modify it under the terms of the GNU Lesser General Public |
paulb@599 | 11 | License as published by the Free Software Foundation; either |
paulb@599 | 12 | version 2.1 of the License, or (at your option) any later version. |
paulb@599 | 13 | |
paulb@599 | 14 | This library is distributed in the hope that it will be useful, |
paulb@599 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
paulb@599 | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paulb@599 | 17 | Lesser General Public License for more details. |
paulb@599 | 18 | |
paulb@599 | 19 | You should have received a copy of the GNU Lesser General Public |
paulb@599 | 20 | License along with this library; if not, write to the Free Software |
paulb@599 | 21 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
paulb@599 | 22 | """ |
paulb@599 | 23 | |
paulb@599 | 24 | class PathSelector: |
paulb@599 | 25 | |
paulb@599 | 26 | "Set a request's current path and processed path info on an attribute." |
paulb@599 | 27 | |
paulb@599 | 28 | def __init__(self, resource, add_slash=1, attribute_name="root", path_encoding="utf-8"): |
paulb@599 | 29 | |
paulb@599 | 30 | """ |
paulb@599 | 31 | Initialise the selector with a 'resource' (to which all requests shall |
paulb@599 | 32 | be forwarded), specifying whether a "/" character shall be added to |
paulb@599 | 33 | stored paths using the optional 'add_slash' parameter (default is true), |
paulb@599 | 34 | along with an optional 'attribute_name' (indicating the name of the |
paulb@599 | 35 | attribute on which the path information shall be stored), and the |
paulb@599 | 36 | optional 'path_encoding' for interpreting URL-encoded path values. |
paulb@599 | 37 | """ |
paulb@599 | 38 | |
paulb@599 | 39 | self.resource = resource |
paulb@599 | 40 | self.add_slash = add_slash |
paulb@599 | 41 | self.attribute_name = attribute_name |
paulb@599 | 42 | self.path_encoding = path_encoding |
paulb@599 | 43 | |
paulb@599 | 44 | def _slash(self): |
paulb@599 | 45 | if self.add_slash: |
paulb@599 | 46 | return "/" |
paulb@599 | 47 | else: |
paulb@599 | 48 | return "" |
paulb@599 | 49 | |
paulb@599 | 50 | def respond(self, trans): |
paulb@599 | 51 | |
paulb@599 | 52 | """ |
paulb@599 | 53 | Respond to the transaction 'trans' by storing the current path and |
paulb@599 | 54 | processed virtual path info on the named transaction attribute. |
paulb@599 | 55 | """ |
paulb@599 | 56 | |
paulb@599 | 57 | pwi = trans.get_path_without_info(self.path_encoding) |
paulb@599 | 58 | |
paulb@599 | 59 | # Make a note of the path given the following general rule: |
paulb@599 | 60 | # path_without_info + path_info |
paulb@599 | 61 | # == path_without_info + processed_virtual_path_info + virtual_path_info |
paulb@599 | 62 | |
paulb@599 | 63 | attributes = trans.get_attributes() |
paulb@599 | 64 | attributes[self.attribute_name] = trans.encode_path( |
paulb@599 | 65 | pwi + trans.get_processed_virtual_path_info(self.path_encoding) + self._slash(), |
paulb@599 | 66 | self.path_encoding |
paulb@599 | 67 | ) |
paulb@599 | 68 | |
paulb@599 | 69 | self.resource.respond(trans) |
paulb@599 | 70 | |
paulb@599 | 71 | # vim: tabstop=4 expandtab shiftwidth=4 |