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