mirror of
https://github.com/zhigang1992/mitmproxy.git
synced 2026-04-24 20:34:56 +08:00
use types.SimpleNamespace instead of custom NS class
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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", []):
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user