1
2
3
4
5
6 import time
7 import urllib
8 import warnings
9
10 weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
11 monthname = [None,
12 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
13 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
14
16 """Return the current date and time formatted for a message header."""
17 if timestamp is None:
18 timestamp = time.time()
19 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
20 s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
21 weekdayname[wd],
22 day, monthname[month], year,
23 hh, mm, ss)
24 return s
25
27 if not isinstance(s, basestring):
28 raise TypeError("value should be a str or unicode")
29
30 if isinstance(s, unicode):
31 return s.encode('utf-8')
32 return s
33
35 """URL encode a single string with a given encoding."""
36 if isinstance(s, unicode):
37 s = s.encode(charset)
38 elif not isinstance(s, str):
39 s = str(s)
40 return urllib.quote(s, safe=safe)
41
42
43 -def url_encode(obj, charset="utf8", encode_keys=False):
44 items = []
45 if isinstance(obj, dict):
46 for k, v in list(obj.items()):
47 items.append((k, v))
48 else:
49 items = list(items)
50
51 tmp = []
52 for k, v in items:
53 if encode_keys:
54 k = encode(k, charset)
55
56 if not isinstance(v, (tuple, list)):
57 v = [v]
58
59 for v1 in v:
60 if v1 is None:
61 v1 = ''
62 elif callable(v1):
63 v1 = encode(v1(), charset)
64 else:
65 v1 = encode(v1, charset)
66 tmp.append('%s=%s' % (urllib.quote(k), urllib.quote_plus(v1)))
67 return '&'.join(tmp)
68
70 if isinstance(v, unicode):
71 v = v.encode(charset)
72 else:
73 v = str(v)
74 return v
75
77 """
78 Wraps a decorator, with a deprecation warning or error
79 """
80 - def __init__(self, decorator, attr, message, warning=True):
81 self.decorator = decorator
82 self.attr = attr
83 self.message = message
84 self.warning = warning
85
87 if obj is None:
88 return self
89 self.warn()
90 return self.decorator.__get__(obj, type)
91
95
99
101 return '<Deprecated attribute %s: %r>' % (
102 self.attr,
103 self.decorator)
104
106 if not self.warning:
107 raise DeprecationWarning(
108 'The attribute %s is deprecated: %s' % (self.attr, self.message))
109 else:
110 warnings.warn(
111 'The attribute %s is deprecated: %s' % (self.attr, self.message),
112 DeprecationWarning,
113 stacklevel=3)
114