Skip to content

Commit

Permalink
remove trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
az0 committed Jan 23, 2013
1 parent aa3d6d7 commit be5f32d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
26 changes: 13 additions & 13 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_find_links():
assert find_links("http://abc") == ("%s", ["http://abc"])
assert find_links("t http://abc") == ("t %s", ["http://abc"])
assert find_links("http://abc t") == ("%s t", ["http://abc"])
assert find_links("1 http://a 2 http://b 3") == ("1 %s 2 %s 3",
assert find_links("1 http://a 2 http://b 3") == ("1 %s 2 %s 3",
["http://a", "http://b"])
assert find_links("%") == ("%%", [])
assert find_links("(http://abc)") == ("(%s)", ["http://abc"])
Expand All @@ -30,11 +30,11 @@ def test_find_links():
def start_server(*resp):
"""HTTP server replying with the given responses to the expected
requests."""
def url(port, path):
def url(port, path):
return 'http://%s:%s%s' % (socket.gethostname(), port, path)

responses = list(reversed(resp))

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(self):
response = responses.pop()
Expand All @@ -43,15 +43,15 @@ def do_HEAD(self):
for header, value in list(response.headers.items()):
self.send_header(header, value)
self.end_headers()

httpd = SocketServer.TCPServer(("", 0), MyHandler)
t = threading.Thread(target=httpd.serve_forever)
t.setDaemon(True)
t.start()
port = httpd.server_address[1]
yield functools.partial(url, port)
httpd.shutdown()

def test_follow_redirects_direct_link():
link = "/resource"
with start_server(Response(link, 200, {})) as url:
Expand All @@ -61,10 +61,10 @@ def test_follow_redirects_redirected_link():
redirected = "/redirected"
link = "/resource"
with start_server(
Response(link, 301, {"Location": redirected}),
Response(link, 301, {"Location": redirected}),
Response(redirected, 200, {})) as url:
assert url(redirected) == follow_redirects(url(link))

def test_follow_redirects_unavailable():
link = "/resource"
with start_server(Response(link, 404, {})) as url:
Expand All @@ -74,15 +74,15 @@ def test_follow_redirects_link_to_last_available():
unavailable = "/unavailable"
link = "/resource"
with start_server(
Response(link, 301, {"Location": unavailable}),
Response(link, 301, {"Location": unavailable}),
Response(unavailable, 404, {})) as url:
assert url(unavailable) == follow_redirects(url(link))


def test_follow_redirects_no_where():
link = "http://links.nowhere/"
assert link == follow_redirects(link)

def test_follow_redirects_link_to_nowhere():
unavailable = "http://links.nowhere/"
link = "/resource"
Expand All @@ -101,7 +101,7 @@ def test_follow_redirects_filtered_by_site_after_redirect():
redirected = "/redirected"
filtered = "http://dont-follow/"
with start_server(
Response(link, 301, {"Location": redirected}),
Response(link, 301, {"Location": redirected}),
Response(redirected, 301, {"Location": filtered})) as url:
hosts = [socket.gethostname()]
assert filtered == follow_redirects(url(link), hosts)
Expand All @@ -110,7 +110,7 @@ def test_follow_redirects_filtered_by_site_allowed():
redirected = "/redirected"
link = "/resource"
with start_server(
Response(link, 301, {"Location": redirected}),
Response(link, 301, {"Location": redirected}),
Response(redirected, 200, {})) as url:
hosts = [socket.gethostname()]
assert url(redirected) == follow_redirects(url(link), hosts)
Expand All @@ -119,7 +119,7 @@ def test_expand_line():
redirected = "/redirected"
link = "/resource"
with start_server(
Response(link, 301, {"Location": redirected}),
Response(link, 301, {"Location": redirected}),
Response(redirected, 200, {})) as url:
fmt = "before %s after"
line = fmt % url(link)
Expand Down
4 changes: 2 additions & 2 deletions twitter/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def __call__(self, **kwargs):
_id = kwargs.pop('_id', None)
if _id:
kwargs['id'] = _id

# If an _timeout is specified in kwargs, use it
_timeout = kwargs.pop('_timeout', None)

Expand Down Expand Up @@ -262,7 +262,7 @@ class Twitter(TwitterCall):
# Note how the magic `_` method can be used to insert data
# into the middle of a call. You can also use replacement:
t.user.list.members(user="tamtar", list="things-that-are-rad")
# An *optional* `_timeout` parameter can also be used for API
# calls which take much more time than normal or twitter stops
# responding for some reasone
Expand Down
2 changes: 1 addition & 1 deletion twitter/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def main(args=sys.argv[1:]):
format_text = functools.partial(expand_format_text, hosts)
else:
format_text = direct_format_text

# save own timeline or mentions (the user used in OAuth)
if options['timeline'] or options['mentions']:
if isinstance(auth, NoAuth):
Expand Down
2 changes: 1 addition & 1 deletion twitter/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,5 @@ def urlencode_noplus(query):
new_query.append((k, v))
query = new_query
return urlencode(query).replace("+", "%20")

return urlencode(query, safe='~').replace("+", "%20")
10 changes: 5 additions & 5 deletions twitter/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ def find_links(line):
l = line.replace("%", "%%")
regex = "(https?://[^ )]+)"
return (
re.sub(regex, "%s", l),
re.sub(regex, "%s", l),
[m.group(1) for m in re.finditer(regex, l)])

def follow_redirects(link, sites= None):
"""Follow directs for the link as long as the redirects are on the given
sites and return the resolved link."""
def follow(url):
return sites == None or urlparse.urlparse(url).hostname in sites

class RedirectHandler(urllib2.HTTPRedirectHandler):
def __init__(self):
self.last_url = None
Expand All @@ -108,7 +108,7 @@ def redirect_request(self, req, fp, code, msg, hdrs, newurl):
self, req, fp, code, msg, hdrs, newurl)
r.get_method = lambda : 'HEAD'
return r

if not follow(link):
return link
redirect_handler = RedirectHandler()
Expand All @@ -133,4 +133,4 @@ def parse_host_list(list_of_hosts):
p = set(
m.group(1) for m in re.finditer("\s*([^,\s]+)\s*,?\s*", list_of_hosts))
return p

0 comments on commit be5f32d

Please sign in to comment.