summaryrefslogtreecommitdiff
path: root/webob/cachecontrol.py
diff options
context:
space:
mode:
Diffstat (limited to 'webob/cachecontrol.py')
-rw-r--r--webob/cachecontrol.py42
1 files changed, 33 insertions, 9 deletions
diff --git a/webob/cachecontrol.py b/webob/cachecontrol.py
index 59cc2aa..ba4df4e 100644
--- a/webob/cachecontrol.py
+++ b/webob/cachecontrol.py
@@ -1,15 +1,18 @@
"""
Represents the Cache-Control header
"""
-
import re
+
class UpdateDict(dict):
"""
- Dict that has a callback on all updates
+ Dict that has a callback on all updates
"""
+
+ #@@ should these really be module globals?
updated = None
updated_args = None
+
def _updated(self):
"""
Assign to new_dict.updated to track updates
@@ -20,36 +23,46 @@ class UpdateDict(dict):
if args is None:
args = (self,)
updated(*args)
+
def __setitem__(self, key, item):
dict.__setitem__(self, key, item)
self._updated()
+
def __delitem__(self, key):
dict.__delitem__(self, key)
self._updated()
+
def clear(self):
dict.clear(self)
self._updated()
+
def update(self, *args, **kw):
dict.update(self, *args, **kw)
self._updated()
+
def setdefault(self, key, failobj=None):
- dict.setdefault(self, key, failobj)
- self._updated()
- def pop(self):
- v = dict.pop(self)
- self._updated()
+ val = dict.setdefault(self, key, failobj)
+ if val is failobj:
+ self._updated()
+ return val
+
+ def pop(self, default=None):
+ v = dict.pop(self, default)
+ if v is not default:
+ self._updated()
return v
+
def popitem(self):
v = dict.popitem(self)
self._updated()
return v
-
token_re = re.compile(
r'([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?')
need_quote_re = re.compile(r'[^a-zA-Z0-9._-]')
+
class exists_property(object):
"""
Represents a property that either is listed in the Cache-Control
@@ -63,19 +76,23 @@ class exists_property(object):
if obj is None:
return self
return self.prop in obj.properties
+
def __set__(self, obj, value):
if (self.type is not None
and self.type != obj.type):
raise AttributeError(
"The property %s only applies to %s Cache-Control" % (self.prop, self.type))
+
if value:
obj.properties[self.prop] = None
else:
if self.prop in obj.properties:
del obj.properties[self.prop]
+
def __delete__(self, obj):
self.__set__(obj, False)
+
class value_property(object):
"""
Represents a property that has a value in the Cache-Control header.
@@ -87,6 +104,7 @@ class value_property(object):
self.default = default
self.none = none
self.type = type
+
def __get__(self, obj, type=None):
if obj is None:
return self
@@ -98,6 +116,7 @@ class value_property(object):
return value
else:
return self.default
+
def __set__(self, obj, value):
if (self.type is not None
and self.type != obj.type):
@@ -110,10 +129,12 @@ class value_property(object):
obj.properties[self.prop] = None # Empty value, but present
else:
obj.properties[self.prop] = value
+
def __delete__(self, obj):
if self.prop in obj.properties:
del obj.properties[self.prop]
+
class CacheControl(object):
"""
@@ -124,6 +145,8 @@ class CacheControl(object):
only apply to requests or responses).
"""
+ update_dict = UpdateDict
+
def __init__(self, properties, type):
self.properties = properties
self.type = type
@@ -137,7 +160,7 @@ class CacheControl(object):
``updates_to``, if that is given.
"""
if updates_to:
- props = UpdateDict()
+ props = cls.update_dict()
props.updated = updates_to
else:
props = {}
@@ -188,6 +211,7 @@ class CacheControl(object):
"""
return self.__class__(self.properties.copy(), type=self.type)
+
def serialize_cache_control(properties):
if isinstance(properties, CacheControl):
properties = properties.properties