From 6f902ffbb3067350da3da01d923e4351d63f604a Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sun, 20 Mar 2016 19:56:22 +0100 Subject: [PATCH] py3++: iteritems --- examples/har_extractor.py | 3 ++- mitmproxy/filt.py | 20 ++++++++++---------- mitmproxy/protocol/http2.py | 3 ++- mitmproxy/stateobject.py | 7 +++++-- mitmproxy/tnetstring.py | 5 +++-- mitmproxy/web/app.py | 10 ++++++---- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/examples/har_extractor.py b/examples/har_extractor.py index 25661f7c7..15e1ef301 100644 --- a/examples/har_extractor.py +++ b/examples/har_extractor.py @@ -2,6 +2,7 @@ This inline script utilizes harparser.HAR from https://github.com/JustusW/harparser to generate a HAR log object. """ +import six from harparser import HAR from datetime import datetime @@ -111,7 +112,7 @@ def response(context, flow): # HAR timings are integers in ms, so we have to re-encode the raw timings to # that format. - timings = dict([(k, int(1000 * v)) for k, v in timings_raw.iteritems()]) + timings = dict([(k, int(1000 * v)) for k, v in six.iteritems(timings_raw)]) # The full_time is the sum of all timings. # Timings set to -1 will be ignored as per spec. diff --git a/mitmproxy/filt.py b/mitmproxy/filt.py index aa62b7176..5e0e13cce 100644 --- a/mitmproxy/filt.py +++ b/mitmproxy/filt.py @@ -31,19 +31,19 @@ ~c CODE Response code. rex Equivalent to ~u rex """ -from __future__ import absolute_import +from __future__ import absolute_import, print_function import re import sys import pyparsing as pp -class _Token: - +class _Token(object): def dump(self, indent=0, fp=sys.stdout): - print >> fp, "\t" * indent, self.__class__.__name__, - if hasattr(self, "expr"): - print >> fp, "(%s)" % self.expr, - print >> fp + print("{spacing}{name}{expr}".format( + spacing="\t" * indent, + name=self.__class__.__name__, + expr=getattr(self, "expr", "") + ), file=fp) class _Action(_Token): @@ -283,7 +283,7 @@ class FAnd(_Token): self.lst = lst def dump(self, indent=0, fp=sys.stdout): - print >> fp, "\t" * indent, self.__class__.__name__ + super(FAnd, self).dump(indent, fp) for i in self.lst: i.dump(indent + 1, fp) @@ -297,7 +297,7 @@ class FOr(_Token): self.lst = lst def dump(self, indent=0, fp=sys.stdout): - print >> fp, "\t" * indent, self.__class__.__name__ + super(FOr, self).dump(indent, fp) for i in self.lst: i.dump(indent + 1, fp) @@ -311,7 +311,7 @@ class FNot(_Token): self.itm = itm[0] def dump(self, indent=0, fp=sys.stdout): - print >> fp, "\t" * indent, self.__class__.__name__ + super(FNot, self).dump(indent, fp) self.itm.dump(indent + 1, fp) def __call__(self, f): diff --git a/mitmproxy/protocol/http2.py b/mitmproxy/protocol/http2.py index aa0d78250..1773729d3 100644 --- a/mitmproxy/protocol/http2.py +++ b/mitmproxy/protocol/http2.py @@ -5,6 +5,7 @@ import time import Queue import h2 +import six from h2.connection import H2Connection from netlib.tcp import ssl_read_select @@ -172,7 +173,7 @@ class Http2Layer(Layer): if other_stream_id is not None: other_conn.h2.safe_reset_stream(other_stream_id, event.error_code) elif isinstance(event, h2.events.RemoteSettingsChanged): - new_settings = dict([(id, cs.new_value) for (id, cs) in event.changed_settings.iteritems()]) + new_settings = dict([(id, cs.new_value) for (id, cs) in six.iteritems(event.changed_settings)]) other_conn.h2.safe_update_settings(new_settings) elif isinstance(event, h2.events.ConnectionTerminated): # Do not immediately terminate the other connection. diff --git a/mitmproxy/stateobject.py b/mitmproxy/stateobject.py index a4a1ffda3..fff6e116a 100644 --- a/mitmproxy/stateobject.py +++ b/mitmproxy/stateobject.py @@ -1,4 +1,7 @@ from __future__ import absolute_import + +import six + from netlib.utils import Serializable @@ -23,7 +26,7 @@ class StateObject(Serializable): Retrieve object state. """ state = {} - for attr, cls in self._stateobject_attributes.iteritems(): + for attr, cls in six.iteritems(self._stateobject_attributes): val = getattr(self, attr) if hasattr(val, "get_state"): state[attr] = val.get_state() @@ -36,7 +39,7 @@ class StateObject(Serializable): Load object state from data returned by a get_state call. """ state = state.copy() - for attr, cls in self._stateobject_attributes.iteritems(): + for attr, cls in six.iteritems(self._stateobject_attributes): if state.get(attr) is None: setattr(self, attr, state.pop(attr)) else: diff --git a/mitmproxy/tnetstring.py b/mitmproxy/tnetstring.py index c5c185c64..d9d61258b 100644 --- a/mitmproxy/tnetstring.py +++ b/mitmproxy/tnetstring.py @@ -67,6 +67,7 @@ like so:: u'\u03b1' """ +import six __ver_major__ = 0 __ver_minor__ = 2 @@ -172,7 +173,7 @@ def _rdumpq(q, size, value, encoding=None): if isinstance(value, dict): write("}") init_size = size = size + 1 - for (k, v) in value.iteritems(): + for (k, v) in six.iteritems(value): size = _rdumpq(q, size, v, encoding) size = _rdumpq(q, size, k, encoding) span = str(size - init_size) @@ -236,7 +237,7 @@ def _gdumps(value, encoding): yield "]" elif isinstance(value, (dict,)): sub = [] - for (k, v) in value.iteritems(): + for (k, v) in six.iteritems(value): sub.extend(_gdumps(k)) sub.extend(_gdumps(v)) sub = "".join(sub) diff --git a/mitmproxy/web/app.py b/mitmproxy/web/app.py index 63b7bf1a1..2cac2ab9e 100644 --- a/mitmproxy/web/app.py +++ b/mitmproxy/web/app.py @@ -1,5 +1,7 @@ import os.path import re + +import six import tornado.web import tornado.websocket import logging @@ -185,11 +187,11 @@ class FlowHandler(RequestHandler): def put(self, flow_id): flow = self.flow flow.backup() - for a, b in self.json.iteritems(): + for a, b in six.iteritems(self.json): if a == "request": request = flow.request - for k, v in b.iteritems(): + for k, v in six.iteritems(b): if k in ["method", "scheme", "host", "path", "http_version"]: setattr(request, k, str(v)) elif k == "port": @@ -201,7 +203,7 @@ class FlowHandler(RequestHandler): elif a == "response": response = flow.response - for k, v in b.iteritems(): + for k, v in six.iteritems(b): if k == "msg": response.msg = str(v) elif k == "code": @@ -293,7 +295,7 @@ class Settings(RequestHandler): def put(self): update = {} - for k, v in self.json.iteritems(): + for k, v in six.iteritems(self.json): if k == "intercept": self.state.set_intercept(v) update[k] = v