X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/62ec1b07870ed00d93fbb3a7e4093c34af302bc4..737cfb613092fd64855052f0dbf67365bbd366fa:/twitter/util.py diff --git a/twitter/util.py b/twitter/util.py index aa7a837..7e4d6ab 100644 --- a/twitter/util.py +++ b/twitter/util.py @@ -10,6 +10,7 @@ from __future__ import print_function import contextlib import re import sys +import textwrap import time try: @@ -88,15 +89,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 @@ -108,7 +109,7 @@ def follow_redirects(link, sites= None): self, req, fp, code, msg, hdrs, newurl) r.get_method = lambda : 'HEAD' return r - + if not follow(link): return link redirect_handler = RedirectHandler() @@ -133,4 +134,13 @@ 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 - + + +def align_text(text, left_margin=16, max_width=80): + lines = [] + for line in text.split('\n'): + temp_lines = textwrap.wrap(line, max_width - left_margin) + temp_lines = [(' ' * left_margin + line) for line in temp_lines] + lines.append('\n'.join(temp_lines)) + ret = '\n'.join(lines) + return ret.lstrip()