use types.SimpleNamespace instead of custom NS class

This commit is contained in:
Maximilian Hils
2016-12-13 14:09:06 +01:00
parent b39380b00f
commit 44f94c8844
3 changed files with 4 additions and 23 deletions

View File

@@ -4,6 +4,7 @@ import shlex
import sys
import threading
import traceback
import types
from mitmproxy import exceptions
from mitmproxy import ctx
@@ -14,19 +15,6 @@ import watchdog.events
from watchdog.observers import polling
class NS:
def __init__(self, ns):
self.__dict__["ns"] = ns
def __getattr__(self, key):
if key not in self.ns:
raise AttributeError("No such element: %s", key)
return self.ns[key]
def __setattr__(self, key, value):
self.__dict__["ns"][key] = value
def parse_command(command):
"""
Returns a (path, args) tuple.
@@ -113,8 +101,8 @@ def load_script(path, args):
return
ns = {'__file__': os.path.abspath(path)}
with scriptenv(path, args):
exec(code, ns, ns)
return NS(ns)
exec(code, ns)
return types.SimpleNamespace(**ns)
class ReloadHandler(watchdog.events.FileSystemEventHandler):

View File

@@ -18,13 +18,6 @@ import watchdog.events
from .. import tutils as ttutils
def test_ns():
n = script.NS({})
n.one = "one"
assert n.one == "one"
assert n.__dict__["ns"]["one"] == "one"
def test_scriptenv():
with taddons.context() as tctx:
with script.scriptenv("path", []):

View File

@@ -150,7 +150,7 @@ class TestHARDump:
def test_format_cookies(self):
m, sc = tscript("complex/har_dump.py", "-")
format_cookies = sc.ns.ns["format_cookies"]
format_cookies = sc.ns.format_cookies
CA = cookies.CookieAttrs