| 2081 | (filename, line)) |
| 2082 | |
| 2083 | def save(self, filename=None, ignore_discard=False, ignore_expires=False): |
| 2084 | if filename is None: |
| 2085 | if self.filename is not None: filename = self.filename |
| 2086 | else: raise ValueError(MISSING_FILENAME_TEXT) |
| 2087 | |
| 2088 | with os.fdopen( |
| 2089 | os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC, 0o600), |
| 2090 | 'w', |
| 2091 | ) as f: |
| 2092 | f.write(NETSCAPE_HEADER_TEXT) |
| 2093 | now = time.time() |
| 2094 | for cookie in self: |
| 2095 | domain = cookie.domain |
| 2096 | if not ignore_discard and cookie.discard: |
| 2097 | continue |
| 2098 | if not ignore_expires and cookie.is_expired(now): |
| 2099 | continue |
| 2100 | if cookie.secure: secure = "TRUE" |
| 2101 | else: secure = "FALSE" |
| 2102 | if domain.startswith("."): initial_dot = "TRUE" |
| 2103 | else: initial_dot = "FALSE" |
| 2104 | if cookie.expires is not None: |
| 2105 | expires = str(cookie.expires) |
| 2106 | else: |
| 2107 | expires = "" |
| 2108 | if cookie.value is None: |
| 2109 | # cookies.txt regards 'Set-Cookie: foo' as a cookie |
| 2110 | # with no name, whereas http.cookiejar regards it as a |
| 2111 | # cookie with no value. |
| 2112 | name = "" |
| 2113 | value = cookie.name |
| 2114 | else: |
| 2115 | name = cookie.name |
| 2116 | value = cookie.value |
| 2117 | if cookie.has_nonstandard_attr(HTTPONLY_ATTR): |
| 2118 | domain = HTTPONLY_PREFIX + domain |
| 2119 | f.write( |
| 2120 | "\t".join([domain, initial_dot, cookie.path, |
| 2121 | secure, expires, name, value])+ |
| 2122 | "\n") |