General cleanup.

Cut out unused variables and code, generally shut up pychecker as much as is
reasonable.
This commit is contained in:
Aldo Cortesi 2011-08-02 16:14:33 +12:00
parent 17835b9b78
commit 357502fe03
11 changed files with 33 additions and 84 deletions

View File

@ -16,9 +16,8 @@
import mailcap, mimetypes, tempfile, os, subprocess, glob, time
import os.path, sys
import cStringIO
import urwid.raw_display
import urwid
import controller, utils, filt, proxy, flow, encoding
import controller, utils, filt, proxy, flow
VIEW_CUTOFF = 1024*100
EVENTLOG_SIZE = 500
@ -96,7 +95,6 @@ def format_flow(f, focus, extended=False, padding=2):
txt.append("\n")
txt.append(("text", ts))
txt.append(" "*(padding+2))
met = ""
if f.response:
txt.append(
@ -384,7 +382,7 @@ class ConnectionView(WWrap):
cmd = [c, name]
self.master.ui.stop()
try:
ret = subprocess.call(cmd)
subprocess.call(cmd)
except:
self.master.statusbar.message("Can't start editor: %s" % c)
self.master.ui.start()
@ -902,7 +900,6 @@ class BodyPile(urwid.Pile):
# This is essentially a copypasta from urwid.Pile's keypress handler.
# So much for "closed for modification, but open for extension".
maxcol = size[0]
item_rows = None
if len(size)==2:
item_rows = self.get_item_rows( size, focus=True )
@ -1024,11 +1021,11 @@ class ConsoleMaster(flow.FlowMaster):
def _view_conn_binary(self, content):
txt = []
for offset, hex, s in utils.hexdump(content[:VIEW_CUTOFF]):
for offset, hexa, s in utils.hexdump(content[:VIEW_CUTOFF]):
txt.append(urwid.Text([
("offset", offset),
" ",
("text", hex),
("text", hexa),
" ",
("text", s),
]))
@ -1070,7 +1067,6 @@ class ConsoleMaster(flow.FlowMaster):
]
def _find_pretty_view(self, content, hdrItems):
txt = []
ctype = None
for i in hdrItems:
if i[0].lower() == "content-type":
@ -1167,7 +1163,7 @@ class ConsoleMaster(flow.FlowMaster):
c = os.environ.get("PAGER") or os.environ.get("EDITOR")
cmd = [c, name]
self.ui.stop()
ret = subprocess.call(cmd, shell=shell)
subprocess.call(cmd, shell=shell)
self.ui.start()
os.unlink(name)
@ -1555,7 +1551,7 @@ class ConsoleMaster(flow.FlowMaster):
def loop(self):
changed = True
try:
while not controller.exit:
while not controller.should_exit:
startloop = time.time()
if changed:
self.statusbar.redraw()

View File

@ -16,7 +16,7 @@
import sys
import Queue, threading
exit = False
should_exit = False
#begin nocover
@ -36,7 +36,7 @@ class Msg:
self.acked = False
try:
masterq.put(self, timeout=3)
while not exit:
while not should_exit:
try:
g = self.q.get(timeout=0.5)
except Queue.Empty:
@ -84,7 +84,7 @@ class Master:
if self.server:
slave = Slave(self.masterq, self.server)
slave.start()
while not exit:
while not should_exit:
self.tick(self.masterq)
self.shutdown()
@ -97,8 +97,8 @@ class Master:
msg.ack()
def shutdown(self):
global exit
if not exit:
exit = True
global should_exit
if not should_exit:
should_exit = True
if self.server:
self.server.shutdown()

View File

@ -1,4 +1,4 @@
import sys, os, traceback
import sys, os
import flow, filt, utils
class DumpError(Exception): pass
@ -225,6 +225,6 @@ class DumpMaster(flow.FlowMaster):
return
try:
return flow.FlowMaster.run(self)
except BaseException, v:
except BaseException:
self.shutdown()
raise

View File

@ -315,6 +315,6 @@ def parse(s):
return bnf.parseString(s, parseAll=True)[0]
except pp.ParseException:
return None
except ValueError, e:
except ValueError:
return None

View File

@ -2,7 +2,7 @@
This module provides more sophisticated flow tracking. These match requests
with their responses, and provide filtering and interception facilities.
"""
import subprocess, base64, sys, json, hashlib, Cookie, cookielib, copy
import subprocess, sys, json, hashlib, Cookie, cookielib
import proxy, threading, netstring, filt
import controller, version
@ -78,7 +78,6 @@ class ServerPlaybackState:
self.fmap = {}
for i in flows:
if i.response:
h = self._hash(i)
l = self.fmap.setdefault(self._hash(i), [])
l.append(i)
@ -150,7 +149,6 @@ class StickyCookieState:
def handle_request(self, f):
if f.match(self.flt):
cs = []
for i in self.jar.keys():
match = [
cookielib.domain_match(i[0], f.request.host),
@ -334,7 +332,7 @@ class Flow:
return c
class State:
class State(object):
def __init__(self):
self._flow_map = {}
self._flow_list = []
@ -387,8 +385,11 @@ class State:
Add an error response to the state. Returns the matching flow, or
None if there isn't one.
"""
f = self._flow_map.get(err.request) if err.request else None
if not f:
if err.request:
f = self._flow_map.get(err.request)
if not f:
return None
else:
return None
f.error = err
if f.match(self._limit) and not f in self.view:

View File

@ -853,7 +853,6 @@ def certificate_option_group(parser):
def process_certificate_option_group(parser, options):
conf = {}
if options.cert:
options.cert = os.path.expanduser(options.cert)
if not os.path.exists(options.cert):

View File

@ -5,10 +5,9 @@
convenience functions to make loading and returning data from scripts
simple.
"""
import sys, base64
import sys
import flow
#begin nocover
def load_flow():
"""

View File

@ -12,7 +12,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re, os, subprocess, datetime, textwrap, errno
import re, os, subprocess, datetime, textwrap
import time, functools, copy, cgi
import json
@ -145,31 +145,6 @@ def hexdump(s):
return parts
def isStringLike(anobj):
try:
# Avoid succeeding expensively if anobj is large.
anobj[:0]+''
except:
return 0
else:
return 1
def isSequenceLike(anobj):
"""
Is anobj a non-string sequence type (list, tuple, iterator, or
similar)? Crude, but mostly effective.
"""
if not hasattr(anobj, "next"):
if isStringLike(anobj):
return 0
try:
anobj[:0]
except:
return 0
return 1
def try_del(dict, key):
try:
del dict[key]
@ -207,7 +182,6 @@ class Headers:
def __setitem__(self, k, hdrs):
k = self._kconv(k)
first = None
new = self._filter_lst(k, self.lst)
for i in hdrs:
new.append((k, i))
@ -328,7 +302,7 @@ class Data:
if not os.path.exists(fullpath):
raise ValueError, "dataPath: %s does not exist."%fullpath
return fullpath
data = Data(__name__)
pkg_data = Data(__name__)
def dummy_ca(path):
@ -353,7 +327,7 @@ def dummy_ca(path):
"req",
"-new",
"-x509",
"-config", data.path("resources/ca.cnf"),
"-config", pkg_data.path("resources/ca.cnf"),
"-nodes",
"-days", CERT_EXPIRY,
"-out", path,
@ -425,8 +399,10 @@ def dummy_cert(certdir, ca, commonname):
confpath = os.path.join(certdir, commonname + ".cnf")
reqpath = os.path.join(certdir, commonname + ".req")
template = open(data.path("resources/cert.cnf")).read()
f = open(confpath, "w").write(template%(dict(commonname=commonname)))
template = open(pkg_data.path("resources/cert.cnf")).read()
f = open(confpath, "w")
f.write(template%(dict(commonname=commonname)))
f.close()
if ca:
# Create a dummy signed certificate. Uses same key as the signing CA

View File

@ -489,7 +489,7 @@ class uFlowMaster(libpry.AutoTree):
assert fm.do_server_playback(tutils.tflow())
q = Queue.Queue()
fm.tick(q)
assert controller.exit
assert controller.should_exit
fm.stop_server_playback()
assert not fm.server_playback

View File

@ -40,7 +40,7 @@ class upretty_size(libpry.AutoTree):
class uData(libpry.AutoTree):
def test_nonexistent(self):
libpry.raises("does not exist", utils.data.path, "nonexistent")
libpry.raises("does not exist", utils.pkg_data.path, "nonexistent")
class uHeaders(libpry.AutoTree):
@ -147,26 +147,6 @@ class uHeaders(libpry.AutoTree):
]
class uisStringLike(libpry.AutoTree):
def test_all(self):
assert utils.isStringLike("foo")
assert not utils.isStringLike([1, 2, 3])
assert not utils.isStringLike((1, 2, 3))
assert not utils.isStringLike(["1", "2", "3"])
class uisSequenceLike(libpry.AutoTree):
def test_all(self):
assert utils.isSequenceLike([1, 2, 3])
assert utils.isSequenceLike((1, 2, 3))
assert not utils.isSequenceLike("foobar")
assert utils.isSequenceLike(["foobar", "foo"])
x = iter([1, 2, 3])
assert utils.isSequenceLike(x)
assert not utils.isSequenceLike(1)
class upretty_xmlish(libpry.AutoTree):
def test_tagre(self):
def f(s):
@ -315,8 +295,6 @@ tests = [
uisXML(),
uhexdump(),
upretty_size(),
uisStringLike(),
uisSequenceLike(),
uHeaders(),
uData(),
upretty_xmlish(),

View File

@ -66,7 +66,7 @@ class TestMaster(controller.Master):
class ProxyThread(threading.Thread):
def __init__(self, port, testq):
self.tmaster = TestMaster(port, testq)
controller.exit = False
controller.should_exit = False
threading.Thread.__init__(self)
def run(self):