# $Id: State.py,v 1.27 2002/08/26 13:52:28 jmp Exp $ # Copyright 2001, 2002 by Fle3 Team and contributors # # This file is part of Fle3. # # Fle3 is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Fle3 is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Fle3; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA """This module contains the State class, which is responsible for storing state information in the URL. Currently the format is something like: 4,11spamhello world3,3foo1,2 meaning: spam = 'hello world' (string) foo = ['1','2'] (list) Note that string does not contain any information which variable are strings and which are lists... it depends which functions you use to access them. """ import types import re from string import atoi from string import join import urllib # Note: Class State does not have any state ;-) (just methods...) # TODO: More documentation for this class. class State: """State""" # This is the only method that modifies REQUEST. def state_set_state_in_request(self, REQUEST, state): """Set state in REQUEST.""" REQUEST.set('state_url', state) # This is the only method that returns state string. # (At the moment there is no state_set_string() as # we don't need it.) def state_set_list(self, REQUEST, name, value): """Set list in a state, return new state string.""" state = self.__state_from_request(REQUEST) d = self.__state_string_to_dict(state) d[name] = join(value, ',') return self.__dict_to_state_string(d) def state_get_string(self, REQUEST, name): """Return value for given string variable.""" state = self.__state_from_request(REQUEST) try: d = self.__state_string_to_dict(state) return d[name] except: return '' def state_get_list(self, REQUEST, name): """Return value for given string variable.""" state = self.__state_from_request(REQUEST) try: d = self.__state_string_to_dict(state) return d[name].split(',') except: return [] def state_form(self, REQUEST, action, method, enctype='application/x-www-form-urlencoded', accept_charset='utf-8'): """Return HTML form and input tags.""" state = self.__state_from_request(REQUEST) return '