#
####
#
-# $Id$
+# Id: Cookie.py,v 2.29 2000/08/23 05:28:49 timo Exp
# by Timothy O'Malley <timo@alum.mit.edu>
#
# Cookie.py is a Python module for the handling of HTTP
# information on cookies.
#
# The original idea to treat Cookies as a dictionary came from
-# Dave Mitchel (davem@magnet.com) in 1995, when he released the
+# Dave Mitchell (davem@magnet.com) in 1995, when he released the
# first version of nscookie.py.
#
####
>>> C = Cookie.SmartCookie()
>>> C["fig"] = "newton"
>>> C["sugar"] = "wafer"
- >>> C
+ >>> print C
Set-Cookie: sugar=wafer;
Set-Cookie: fig=newton;
>>> C = Cookie.SmartCookie()
>>> C.load("chips=ahoy; vienna=finger")
- >>> C
+ >>> print C
Set-Cookie: vienna=finger;
Set-Cookie: chips=ahoy;
>>> C = Cookie.SmartCookie()
>>> C.load('keebler="E=everybody; L=\\"Loves\\"; fudge=\\012;";')
- >>> C
+ >>> print C
Set-Cookie: keebler="E=everybody; L=\"Loves\"; fudge=\012;";
Each element of the Cookie also supports all of the RFC 2109
>>> C = Cookie.SmartCookie()
>>> C["oreo"] = "doublestuff"
>>> C["oreo"]["path"] = "/"
- >>> C
+ >>> print C
Set-Cookie: oreo="doublestuff"; Path=/;
Each dictionary element has a 'value' attribute, which gives you
'7'
>>> C["string"].value
'seven'
- >>> C
+ >>> print C
Set-Cookie: number=7;
Set-Cookie: string=seven;
7
>>> C["string"].value
'seven'
- >>> C
+ >>> print C
Set-Cookie: number="I7\012.";
Set-Cookie: string="S'seven'\012p1\012.";
7
>>> C["string"].value
'seven'
- >>> C
+ >>> print C
Set-Cookie: number="I7\012.";
Set-Cookie: string=seven;
}
def _quote(str, LegalChars=_LegalChars,
- join=string.join, idmap=string._idmap, translate=string.translate):
+ join=string.join, idmap=string._idmap, translate=string.translate):
#
# If the string does not need to be double-quoted,
# then just return the string. Otherwise, surround
# special characters.
#
if "" == translate(str, idmap, LegalChars):
- return str
+ return str
else:
- return '"' + join( map(_Translator.get, str, str), "" ) + '"'
+ return '"' + join( map(_Translator.get, str, str), "" ) + '"'
# end _quote
# end __setitem__
def isReservedKey(self, K):
- return string.lower(K) in self._reserved_keys
+ return string.lower(K) in self._reserved_keys
# end isReservedKey
def set(self, key, val, coded_val,
def output(self, attrs=None, header = "Set-Cookie:"):
return "%s %s" % ( header, self.OutputString(attrs) )
- __repr__ = output
+ __str__ = output
+ def __repr__(self):
+ return '<%s: %s=%s>' % (self.__class__.__name__,
+ self.key, repr(self.value) )
+
def js_output(self, attrs=None):
# Print javascript
return """
if attrs == None:
attrs = self._reserved_keys
for K,V in self.items():
- if not V: continue
+ if V == "": continue
if K not in attrs: continue
if K == "expires" and type(V) == type(1):
RA("%s=%s;" % (self._reserved[K], _getdate(V)))
return string.join(result, sep)
# end output
- __repr__ = output
-
+ __str__ = output
+
+ def __repr__(self):
+ L = []
+ for K,V in self.items():
+ L.append( '%s=%s' % (K,repr(V.value) ) )
+ return '<%s: %s>' % (self.__class__.__name__, string.join(L))
+
def js_output(self, attrs=None):
"""Return a string suitable for JavaScript."""
result = []
M[ K[1:] ] = V
elif string.lower(K) in Morsel._reserved_keys:
if M:
- M[ K ] = V
+ M[ K ] = _unquote(V)
else:
rval, cval = self.value_decode(V)
self.__set(K, rval, cval)
M = self[K]
- return
+ return
# end __ParseString
# end BaseCookie class
#
# should add a test routine?
#
+
+#Local Variables:
+#tab-width: 4
+#end: