[wwwsearch-commits] r27808 - wwwsearch/mechanize/trunk/mechanize
jjlee at codespeak.net
jjlee at codespeak.net
Sun May 28 23:45:12 CEST 2006
Author: jjlee
Date: Sun May 28 23:45:11 2006
New Revision: 27808
Modified:
wwwsearch/mechanize/trunk/mechanize/_clientcookie.py
wwwsearch/mechanize/trunk/mechanize/_headersutil.py
wwwsearch/mechanize/trunk/mechanize/_lwpcookiejar.py
wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py
wwwsearch/mechanize/trunk/mechanize/_msiecookiejar.py
wwwsearch/mechanize/trunk/mechanize/_opener.py
wwwsearch/mechanize/trunk/mechanize/_request.py
wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py
wwwsearch/mechanize/trunk/mechanize/_util.py
Log:
Remove most use of module string from code (not from tests, yet)
Modified: wwwsearch/mechanize/trunk/mechanize/_clientcookie.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_clientcookie.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_clientcookie.py Sun May 28 23:45:11 2006
@@ -32,7 +32,7 @@
"""
-import sys, re, urlparse, string, copy, time, struct, urllib, types, logging
+import sys, re, urlparse, copy, time, struct, urllib, types, logging
try:
import threading
_threading = threading; del threading
@@ -105,13 +105,13 @@
"""
# Note that, if A or B are IP addresses, the only relevant part of the
# definition of the domain-match algorithm is the direct string-compare.
- A = string.lower(A)
- B = string.lower(B)
+ A = A.lower()
+ B = B.lower()
if A == B:
return True
if not is_HDN(A):
return False
- i = string.rfind(A, B)
+ i = A.rfind(B)
has_form_nb = not (i == -1 or i == 0)
return (
has_form_nb and
@@ -133,8 +133,8 @@
A and B may be host domain names or IP addresses.
"""
- A = string.lower(A)
- B = string.lower(B)
+ A = A.lower()
+ B = B.lower()
if not (liberal_is_HDN(A) and liberal_is_HDN(B)):
if A == B:
# equal IP addresses
@@ -162,7 +162,7 @@
# remove port, if present
host = cut_port_re.sub("", host, 1)
- return string.lower(host)
+ return host.lower()
def eff_request_host(request):
"""Return a tuple (request-host, effective request-host name).
@@ -171,7 +171,7 @@
"""
erhn = req_host = request_host(request)
- if string.find(req_host, ".") == -1 and not IPV4_RE.search(req_host):
+ if req_host.find(".") == -1 and not IPV4_RE.search(req_host):
erhn = req_host + ".local"
return req_host, erhn
@@ -192,7 +192,7 @@
def request_port(request):
host = request.get_host()
- i = string.find(host, ':')
+ i = host.find(':')
if i >= 0:
port = host[i+1:]
try:
@@ -209,7 +209,7 @@
HTTP_PATH_SAFE = "%/;:@&=+$,!~*'()"
ESCAPED_CHAR_RE = re.compile(r"%([0-9a-fA-F][0-9a-fA-F])")
def uppercase_escaped_char(match):
- return "%%%s" % string.upper(match.group(1))
+ return "%%%s" % match.group(1).upper()
def escape_path(path):
"""Escape any invalid characters in HTTP URL, and uppercase all escapes."""
# There's no knowing what character encoding was used to create URLs
@@ -252,11 +252,11 @@
'.local'
"""
- i = string.find(h, ".")
+ i = h.find(".")
if i >= 0:
#a = h[:i] # this line is only here to show what a is
b = h[i+1:]
- i = string.find(b, ".")
+ i = b.find(".")
if is_HDN(h) and (i >= 0 or b == "local"):
return "."+b
return h
@@ -344,7 +344,7 @@
self.port = port
self.port_specified = port_specified
# normalise case, as per RFC 2965 section 3.3.3
- self.domain = string.lower(domain)
+ self.domain = domain.lower()
self.domain_specified = domain_specified
# Sigh. We need to know whether the domain given in the
# cookie-attribute had an initial dot, in order to follow RFC 2965
@@ -397,7 +397,7 @@
args.append("%s=%s" % (name, repr(attr)))
args.append("rest=%s" % repr(self._rest))
args.append("rfc2109=%s" % repr(self.rfc2109))
- return "Cookie(%s)" % string.join(args, ", ")
+ return "Cookie(%s)" % ", ".join(args)
class CookiePolicy:
@@ -728,12 +728,12 @@
domain = cookie.domain
# since domain was specified, we know that:
assert domain.startswith(".")
- if string.count(domain, ".") == 2:
+ if domain.count(".") == 2:
# domain like .foo.bar
- i = string.rfind(domain, ".")
+ i = domain.rfind(".")
tld = domain[i+1:]
sld = domain[1:i]
- if (string.lower(sld) in [
+ if (sld.lower() in [
"co", "ac",
"com", "edu", "org", "net", "gov", "mil", "int",
"aero", "biz", "cat", "coop", "info", "jobs", "mobi",
@@ -761,7 +761,7 @@
undotted_domain = domain[1:]
else:
undotted_domain = domain
- embedded_dots = (string.find(undotted_domain, ".") >= 0)
+ embedded_dots = (undotted_domain.find(".") >= 0)
if not embedded_dots and domain != ".local":
debug(" non-local domain %s contains no embedded dot",
domain)
@@ -783,7 +783,7 @@
if (cookie.version > 0 or
(self.strict_ns_domain & self.DomainStrictNoDots)):
host_prefix = req_host[:-len(domain)]
- if (string.find(host_prefix, ".") >= 0 and
+ if (host_prefix.find(".") >= 0 and
not IPV4_RE.search(req_host)):
debug(" host prefix %s for domain %s contains a dot",
host_prefix, domain)
@@ -797,7 +797,7 @@
req_port = "80"
else:
req_port = str(req_port)
- for p in string.split(cookie.port, ","):
+ for p in cookie.port.split(","):
try:
int(p)
except ValueError:
@@ -867,7 +867,7 @@
req_port = request_port(request)
if req_port is None:
req_port = "80"
- for p in string.split(cookie.port, ","):
+ for p in cookie.port.split(","):
if p == req_port:
break
else:
@@ -1137,8 +1137,7 @@
attrs = self._cookie_attrs(cookies)
if attrs:
if not request.has_header("Cookie"):
- request.add_unredirected_header(
- "Cookie", string.join(attrs, "; "))
+ request.add_unredirected_header("Cookie", "; ".join(attrs))
# if necessary, advertise that we know RFC 2965
if self._policy.rfc2965 and not self._policy.hide_cookie2:
@@ -1188,7 +1187,7 @@
standard = {}
rest = {}
for k, v in cookie_attrs[1:]:
- lc = string.lower(k)
+ lc = k.lower()
# don't lose case distinction for unknown fields
if lc in value_attrs or lc in boolean_attrs:
k = lc
@@ -1205,7 +1204,7 @@
bad_cookie = True
break
# RFC 2965 section 3.3.3
- v = string.lower(v)
+ v = v.lower()
if k == "expires":
if max_age_set:
# Prefer max-age to expires (like Mozilla)
@@ -1272,7 +1271,7 @@
else:
path_specified = False
path = request_path(request)
- i = string.rfind(path, "/")
+ i = path.rfind("/")
if i != -1:
if version == 0:
# Netscape spec parts company from reality here
@@ -1550,12 +1549,12 @@
def __repr__(self):
r = []
for cookie in self: r.append(repr(cookie))
- return "<%s[%s]>" % (self.__class__, string.join(r, ", "))
+ return "<%s[%s]>" % (self.__class__, ", ".join(r))
def __str__(self):
r = []
for cookie in self: r.append(str(cookie))
- return "<%s[%s]>" % (self.__class__, string.join(r, ", "))
+ return "<%s[%s]>" % (self.__class__, ", ".join(r))
class LoadError(Exception): pass
Modified: wwwsearch/mechanize/trunk/mechanize/_headersutil.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_headersutil.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_headersutil.py Sun May 28 23:45:11 2006
@@ -9,7 +9,7 @@
"""
-import os, re, string, urlparse
+import os, re, urlparse
from types import StringType
from types import UnicodeType
STRING_TYPES = StringType, UnicodeType
@@ -113,14 +113,14 @@
if m: # unquoted value
text = unmatched(m)
value = m.group(1)
- value = string.rstrip(value)
+ value = value.rstrip()
else:
# no value, a lone token
value = None
pairs.append((name, value))
- elif startswith(string.lstrip(text), ","):
+ elif startswith(text.lstrip(), ","):
# concatenated headers, as per RFC 2616 section 4.2
- text = string.lstrip(text)[1:]
+ text = text.lstrip()[1:]
if pairs: result.append(pairs)
pairs = []
else:
@@ -159,8 +159,8 @@
else:
k = "%s=%s" % (k, v)
attr.append(k)
- if attr: headers.append(string.join(attr, "; "))
- return string.join(headers, ", ")
+ if attr: headers.append("; ".join(attr))
+ return ", ".join(headers)
def parse_ns_headers(ns_headers):
"""Ad-hoc parser for Netscape protocol cookie-attributes.
@@ -188,15 +188,15 @@
params = re.split(r";\s*", ns_header)
for ii in range(len(params)):
param = params[ii]
- param = string.rstrip(param)
+ param = param.rstrip()
if param == "": continue
if "=" not in param:
k, v = param, None
else:
k, v = re.split(r"\s*=\s*", param, 1)
- k = string.lstrip(k)
+ k = k.lstrip()
if ii != 0:
- lc = string.lower(k)
+ lc = k.lower()
if lc in known_attrs:
k = lc
if k == "version":
Modified: wwwsearch/mechanize/trunk/mechanize/_lwpcookiejar.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_lwpcookiejar.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_lwpcookiejar.py Sun May 28 23:45:11 2006
@@ -18,7 +18,7 @@
"""
-import time, re, string, logging
+import time, re, logging
from _clientcookie import reraise_unmasked_exceptions, FileCookieJar, Cookie, \
MISSING_FILENAME_TEXT, LoadError
@@ -89,7 +89,7 @@
debug(" Not saving %s: expired", cookie.name)
continue
r.append("Set-Cookie3: %s" % lwp_cookie_str(cookie))
- return string.join(r+[""], "\n")
+ return "\n".join(r+[""])
def save(self, filename=None, ignore_discard=False, ignore_expires=False):
if filename is None:
@@ -129,7 +129,7 @@
if line == "": break
if not startswith(line, header):
continue
- line = string.strip(line[len(header):])
+ line = line[len(header):].strip()
for data in split_header_words([line]):
name, value = data[0]
@@ -139,7 +139,7 @@
standard[k] = False
for k, v in data[1:]:
if k is not None:
- lc = string.lower(k)
+ lc = k.lower()
else:
lc = None
# don't lose case distinction for unknown fields
Modified: wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_mozillacookiejar.py Sun May 28 23:45:11 2006
@@ -9,7 +9,7 @@
"""
-import re, string, time, logging
+import re, time, logging
from _clientcookie import reraise_unmasked_exceptions, FileCookieJar, Cookie, \
MISSING_FILENAME_TEXT, LoadError
@@ -75,13 +75,13 @@
if endswith(line, "\n"): line = line[:-1]
# skip comments and blank lines XXX what is $ for?
- if (startswith(string.strip(line), "#") or
- startswith(string.strip(line), "$") or
- string.strip(line) == ""):
+ if (startswith(line.strip(), "#") or
+ startswith(line.strip(), "$") or
+ line.strip() == ""):
continue
domain, domain_specified, path, secure, expires, name, value = \
- string.split(line, "\t")
+ line.split("\t")
secure = (secure == "TRUE")
domain_specified = (domain_specified == "TRUE")
if name == "":
@@ -153,8 +153,8 @@
name = cookie.name
value = cookie.value
f.write(
- string.join([cookie.domain, initial_dot, cookie.path,
- secure, expires, name, value], "\t")+
+ "\t".join([cookie.domain, initial_dot, cookie.path,
+ secure, expires, name, value])+
"\n")
finally:
f.close()
Modified: wwwsearch/mechanize/trunk/mechanize/_msiecookiejar.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_msiecookiejar.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_msiecookiejar.py Sun May 28 23:45:11 2006
@@ -11,7 +11,7 @@
# XXX names and comments are not great here
-import os, re, string, time, struct, logging
+import os, re, time, struct, logging
if os.name == "nt":
import _winreg
@@ -50,7 +50,7 @@
return divmod((filetime - WIN32_EPOCH), 10000000L)[0]
def binary_to_char(c): return "%02X" % ord(c)
-def binary_to_str(d): return string.join(map(binary_to_char, list(d)), "")
+def binary_to_str(d): return "".join(map(binary_to_char, list(d)))
class MSIEBase:
magic_re = re.compile(r"Client UrlCache MMF Ver \d\.\d.*")
@@ -201,7 +201,7 @@
now = int(time.time())
if username is None:
- username = string.lower(os.environ['USERNAME'])
+ username = os.environ['USERNAME'].lower()
cookie_dir = os.path.dirname(filename)
Modified: wwwsearch/mechanize/trunk/mechanize/_opener.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_opener.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_opener.py Sun May 28 23:45:11 2006
@@ -9,7 +9,7 @@
"""
-import urllib2, string, bisect, urlparse
+import urllib2, bisect, urlparse
from _util import startswith, isstringlike
from _request import Request
@@ -72,7 +72,7 @@
condition = meth[ii+1:]
if startswith(condition, "error"):
- jj = string.find(meth[ii+1:], "_") + ii + 1
+ jj = meth[ii+1:].find("_") + ii + 1
kind = meth[jj+1:]
try:
kind = int(kind)
Modified: wwwsearch/mechanize/trunk/mechanize/_request.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_request.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_request.py Sun May 28 23:45:11 2006
@@ -8,7 +8,7 @@
"""
-import urllib2, string
+import urllib2
from _clientcookie import request_host
@@ -39,7 +39,7 @@
def add_unredirected_header(self, key, val):
"""Add a header that will not be added to a redirected request."""
- self.unredirected_hdrs[string.capitalize(key)] = val
+ self.unredirected_hdrs[key.capitalize()] = val
def has_header(self, header_name):
"""True iff request has named header (regular or unredirected)."""
Modified: wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_urllib2_support.py Sun May 28 23:45:11 2006
@@ -12,7 +12,7 @@
"""
import copy, time, tempfile, htmlentitydefs, re, logging, types, \
- string, socket, urlparse, urllib2, urllib, httplib, sgmllib
+ socket, urlparse, urllib2, urllib, httplib, sgmllib
from urllib2 import URLError, HTTPError, BaseHandler
from cStringIO import StringIO
try:
@@ -482,10 +482,10 @@
if code == 200 and hdrs.has_key("refresh"):
refresh = hdrs.getheaders("refresh")[0]
- ii = string.find(refresh, ";")
+ ii = refresh.find(";")
if ii != -1:
pause, newurl_spec = float(refresh[:ii]), refresh[ii+1:]
- jj = string.find(newurl_spec, "=")
+ jj = newurl_spec.find("=")
if jj != -1:
key, newurl = newurl_spec[:jj], newurl_spec[jj+1:]
if key.strip().lower() != "url":
Modified: wwwsearch/mechanize/trunk/mechanize/_util.py
==============================================================================
--- wwwsearch/mechanize/trunk/mechanize/_util.py (original)
+++ wwwsearch/mechanize/trunk/mechanize/_util.py Sun May 28 23:45:11 2006
@@ -59,7 +59,7 @@
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
months_lower = []
-for month in months: months_lower.append(string.lower(month))
+for month in months: months_lower.append(month.lower())
def time2isoz(t=None):
@@ -117,7 +117,7 @@
# translate month name to number
# month numbers start with 1 (January)
try:
- mon = months_lower.index(string.lower(mon))+1
+ mon = months_lower.index(mon.lower())+1
except ValueError:
# maybe it's already a number
try:
@@ -158,7 +158,7 @@
# adjust time using timezone string, to get absolute time since epoch
if tz is None:
tz = "UTC"
- tz = string.upper(tz)
+ tz = tz.upper()
offset = offset_from_tz_string(tz)
if offset is None:
return None
@@ -220,7 +220,7 @@
m = strict_re.search(text)
if m:
g = m.groups()
- mon = months_lower.index(string.lower(g[1])) + 1
+ mon = months_lower.index(g[1].lower()) + 1
tt = (int(g[2]), mon, int(g[0]),
int(g[3]), int(g[4]), float(g[5]))
return my_timegm(tt)
@@ -228,7 +228,7 @@
# No, we need some messy parsing...
# clean up
- text = string.lstrip(text)
+ text = text.lstrip()
text = wkday_re.sub("", text, 1) # Useless weekday
# tz is time zone specifier string
@@ -273,7 +273,7 @@
"""
# clean up
- text = string.lstrip(text)
+ text = text.lstrip()
# tz is time zone specifier string
day, mon, yr, hr, min, sec, tz = [None]*7
More information about the wwwsearch-commits
mailing list