From 357502fe03a5f86c1c29d35c5d607541df157640 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Tue, 2 Aug 2011 16:14:33 +1200 Subject: [PATCH] General cleanup. Cut out unused variables and code, generally shut up pychecker as much as is reasonable. --- libmproxy/console.py | 16 ++++++---------- libmproxy/controller.py | 12 ++++++------ libmproxy/dump.py | 4 ++-- libmproxy/filt.py | 2 +- libmproxy/flow.py | 13 +++++++------ libmproxy/proxy.py | 1 - libmproxy/script.py | 3 +-- libmproxy/utils.py | 38 +++++++------------------------------- test/test_flow.py | 2 +- test/test_utils.py | 24 +----------------------- test/tutils.py | 2 +- 11 files changed, 33 insertions(+), 84 deletions(-) diff --git a/libmproxy/console.py b/libmproxy/console.py index 82ddc6d91..ac90bc541 100644 --- a/libmproxy/console.py +++ b/libmproxy/console.py @@ -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() diff --git a/libmproxy/controller.py b/libmproxy/controller.py index 6fed201ff..e3d5c8342 100644 --- a/libmproxy/controller.py +++ b/libmproxy/controller.py @@ -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() diff --git a/libmproxy/dump.py b/libmproxy/dump.py index 23c804528..86af8be5d 100644 --- a/libmproxy/dump.py +++ b/libmproxy/dump.py @@ -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 diff --git a/libmproxy/filt.py b/libmproxy/filt.py index 40cf7358b..c3de6c111 100644 --- a/libmproxy/filt.py +++ b/libmproxy/filt.py @@ -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 diff --git a/libmproxy/flow.py b/libmproxy/flow.py index def52caa7..090c539a0 100644 --- a/libmproxy/flow.py +++ b/libmproxy/flow.py @@ -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: diff --git a/libmproxy/proxy.py b/libmproxy/proxy.py index 6707b65bf..3ecd1f320 100644 --- a/libmproxy/proxy.py +++ b/libmproxy/proxy.py @@ -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): diff --git a/libmproxy/script.py b/libmproxy/script.py index 897ebb725..92563cde0 100644 --- a/libmproxy/script.py +++ b/libmproxy/script.py @@ -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(): """ diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 3dbeb620b..677d3b222 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -12,7 +12,7 @@ # # You should have received a copy of the GNU General Public License # along with this program. If not, see . -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 diff --git a/test/test_flow.py b/test/test_flow.py index b87951b62..2e1975d9b 100644 --- a/test/test_flow.py +++ b/test/test_flow.py @@ -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 diff --git a/test/test_utils.py b/test/test_utils.py index e22ec0391..06e5fed36 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -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(), diff --git a/test/tutils.py b/test/tutils.py index 2327bdb98..19887fa97 100644 --- a/test/tutils.py +++ b/test/tutils.py @@ -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):