ยปCore Development>Code coverage>Lib/_LWPCookieJar.py

Python code coverage for Lib/_LWPCookieJar.py

#countcontent
1n/a"""Load / save to libwww-perl (LWP) format files.
2n/a
3n/aActually, the format is slightly extended from that used by LWP's
4n/a(libwww-perl's) HTTP::Cookies, to avoid losing some RFC 2965 information
5n/anot recorded by LWP.
6n/a
7n/aIt uses the version string "2.0", though really there isn't an LWP Cookies
8n/a2.0 format. This indicates that there is extra information in here
9n/a(domain_dot and # port_spec) while still being compatible with
10n/alibwww-perl, I hope.
11n/a
121"""
13n/a
141import time, re
151from cookielib import (_warn_unhandled_exception, FileCookieJar, LoadError,
16n/a Cookie, MISSING_FILENAME_TEXT,
17n/a join_header_words, split_header_words,
18n/a iso2time, time2isoz)
19n/a
201def lwp_cookie_str(cookie):
21n/a """Return string representation of Cookie in an the LWP cookie file format.
22n/a
23n/a Actually, the format is extended a bit -- see module docstring.
24n/a
25n/a """
268 h = [(cookie.name, cookie.value),
278 ("path", cookie.path),
288 ("domain", cookie.domain)]
298 if cookie.port is not None: h.append(("port", cookie.port))
308 if cookie.path_specified: h.append(("path_spec", None))
318 if cookie.port_specified: h.append(("port_spec", None))
328 if cookie.domain_initial_dot: h.append(("domain_dot", None))
338 if cookie.secure: h.append(("secure", None))
348 if cookie.expires: h.append(("expires",
352 time2isoz(float(cookie.expires))))
368 if cookie.discard: h.append(("discard", None))
378 if cookie.comment: h.append(("comment", cookie.comment))
388 if cookie.comment_url: h.append(("commenturl", cookie.comment_url))
39n/a
408 keys = cookie._rest.keys()
418 keys.sort()
428 for k in keys:
430 h.append((k, str(cookie._rest[k])))
44n/a
458 h.append(("version", str(cookie.version)))
46n/a
478 return join_header_words([h])
48n/a
492class LWPCookieJar(FileCookieJar):
50n/a """
51n/a The LWPCookieJar saves a sequence of"Set-Cookie3" lines.
52n/a "Set-Cookie3" is the format used by the libwww-perl libary, not known
53n/a to be compatible with any browser, but which is easy to read and
54n/a doesn't lose information about RFC 2965 cookies.
55n/a
56n/a Additional methods
57n/a
58n/a as_lwp_str(ignore_discard=True, ignore_expired=True)
59n/a
601 """
61n/a
621 def as_lwp_str(self, ignore_discard=True, ignore_expires=True):
63n/a """Return cookies as a string of "\n"-separated "Set-Cookie3" headers.
64n/a
65n/a ignore_discard and ignore_expires: see docstring for FileCookieJar.save
66n/a
67n/a """
682 now = time.time()
692 r = []
709 for cookie in self:
717 if not ignore_discard and cookie.discard:
720 continue
737 if not ignore_expires and cookie.is_expired(now):
740 continue
757 r.append("Set-Cookie3: %s" % lwp_cookie_str(cookie))
762 return "\n".join(r+[""])
77n/a
781 def save(self, filename=None, ignore_discard=False, ignore_expires=False):
792 if filename is None:
800 if self.filename is not None: filename = self.filename
810 else: raise ValueError(MISSING_FILENAME_TEXT)
82n/a
832 f = open(filename, "w")
842 try:
85n/a # There really isn't an LWP Cookies 2.0 format, but this indicates
86n/a # that there is extra information in here (domain_dot and
87n/a # port_spec) while still being compatible with libwww-perl, I hope.
882 f.write("#LWP-Cookies-2.0\n")
892 f.write(self.as_lwp_str(ignore_discard, ignore_expires))
90n/a finally:
912 f.close()
92n/a
931 def _really_load(self, f, filename, ignore_discard, ignore_expires):
943 magic = f.readline()
953 if not re.search(self.magic_re, magic):
961 msg = ("%r does not look like a Set-Cookie3 (LWP) format "
971 "file" % filename)
981 raise LoadError(msg)
99n/a
1002 now = time.time()
101n/a
1022 header = "Set-Cookie3:"
1030 boolean_attrs = ("port_spec", "path_spec", "domain_dot",
1042 "secure", "discard")
1050 value_attrs = ("version",
1060 "port", "path", "domain",
1070 "expires",
1082 "comment", "commenturl")
109n/a
1102 try:
1112 while 1:
1129 line = f.readline()
1139 if line == "": break
1147 if not line.startswith(header):
1150 continue
1167 line = line[len(header):].strip()
117n/a
11814 for data in split_header_words([line]):
1197 name, value = data[0]
1207 standard = {}
1217 rest = {}
12242 for k in boolean_attrs:
12335 standard[k] = False
12445 for k, v in data[1:]:
12538 if k is not None:
12638 lc = k.lower()
127n/a else:
1280 lc = None
129n/a # don't lose case distinction for unknown fields
13038 if (lc in value_attrs) or (lc in boolean_attrs):
13138 k = lc
13238 if k in boolean_attrs:
13312 if v is None: v = True
13412 standard[k] = v
13526 elif k in value_attrs:
13626 standard[k] = v
137n/a else:
1380 rest[k] = v
139n/a
1407 h = standard.get
1417 expires = h("expires")
1427 discard = h("discard")
1437 if expires is not None:
1442 expires = iso2time(expires)
1457 if expires is None:
1465 discard = True
1477 domain = h("domain")
1487 domain_specified = domain.startswith(".")
1497 c = Cookie(h("version"), name, value,
1507 h("port"), h("port_spec"),
1517 domain, domain_specified, h("domain_dot"),
1527 h("path"), h("path_spec"),
1537 h("secure"),
1547 expires,
1557 discard,
1567 h("comment"),
1577 h("commenturl"),
1587 rest)
1597 if not ignore_discard and c.discard:
1600 continue
1617 if not ignore_expires and c.is_expired(now):
1620 continue
1637 self.set_cookie(c)
164n/a
1650 except IOError:
1660 raise
1670 except Exception:
1680 _warn_unhandled_exception()
1690 raise LoadError("invalid Set-Cookie3 format file %r: %r" %
1700 (filename, line))