mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 08:11:00 +00:00
Various changes to address PR comments
Made a change to make `CommandManager.execute` the main entry point for executing commands and made `call_strings` into a private method.
This commit is contained in:
parent
373cc945c0
commit
7779eef572
@ -220,7 +220,7 @@ class CommandManager(mitmproxy.types._CommandBase):
|
|||||||
raise exceptions.CommandError("Unknown command: %s" % path)
|
raise exceptions.CommandError("Unknown command: %s" % path)
|
||||||
return self.commands[path].func(*args)
|
return self.commands[path].func(*args)
|
||||||
|
|
||||||
def call_strings(self, path: str, args: typing.Sequence[str]) -> typing.Any:
|
def _call_strings(self, path: str, args: typing.Sequence[str]) -> typing.Any:
|
||||||
"""
|
"""
|
||||||
Call a command using a list of string arguments. May raise CommandError.
|
Call a command using a list of string arguments. May raise CommandError.
|
||||||
"""
|
"""
|
||||||
@ -236,12 +236,13 @@ class CommandManager(mitmproxy.types._CommandBase):
|
|||||||
parts, _ = self.parse_partial(cmdstr)
|
parts, _ = self.parse_partial(cmdstr)
|
||||||
params = []
|
params = []
|
||||||
for p in parts:
|
for p in parts:
|
||||||
|
if p.value.strip() != '':
|
||||||
params.append(p.value)
|
params.append(p.value)
|
||||||
|
|
||||||
if len(parts) == 0:
|
if len(parts) == 0:
|
||||||
raise exceptions.CommandError("Invalid command: %s" % cmdstr)
|
raise exceptions.CommandError("Invalid command: %s" % cmdstr)
|
||||||
|
|
||||||
return self.call_strings(params[0], params[1:])
|
return self._call_strings(params[0], params[1:])
|
||||||
|
|
||||||
def dump(self, out=sys.stdout) -> None:
|
def dump(self, out=sys.stdout) -> None:
|
||||||
cmds = list(self.commands.values())
|
cmds = list(self.commands.values())
|
||||||
|
@ -271,7 +271,7 @@ class ConsoleAddon:
|
|||||||
command, then invoke another command with all occurrences of {choice}
|
command, then invoke another command with all occurrences of {choice}
|
||||||
replaced by the choice the user made.
|
replaced by the choice the user made.
|
||||||
"""
|
"""
|
||||||
choices = ctx.master.commands.call_strings(choicecmd, [])
|
choices = ctx.master.commands.execute(choicecmd)
|
||||||
|
|
||||||
def callback(opt):
|
def callback(opt):
|
||||||
# We're now outside of the call context...
|
# We're now outside of the call context...
|
||||||
@ -535,10 +535,8 @@ class ConsoleAddon:
|
|||||||
raise exceptions.CommandError("Invalid flowview mode.")
|
raise exceptions.CommandError("Invalid flowview mode.")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.master.commands.call_strings(
|
cmd = 'view.settings.setval @focus flowview_mode_%s %s' % (idx, mode)
|
||||||
"view.settings.setval",
|
self.master.commands.execute(cmd)
|
||||||
["@focus", "flowview_mode_%s" % idx, mode]
|
|
||||||
)
|
|
||||||
except exceptions.CommandError as e:
|
except exceptions.CommandError as e:
|
||||||
signals.status_message.send(message=str(e))
|
signals.status_message.send(message=str(e))
|
||||||
|
|
||||||
@ -558,14 +556,9 @@ class ConsoleAddon:
|
|||||||
if not fv:
|
if not fv:
|
||||||
raise exceptions.CommandError("Not viewing a flow.")
|
raise exceptions.CommandError("Not viewing a flow.")
|
||||||
idx = fv.body.tab_offset
|
idx = fv.body.tab_offset
|
||||||
return self.master.commands.call_strings(
|
|
||||||
"view.settings.getval",
|
cmd = 'view.settings.getval @focus flowview_mode_%s %s' % (idx, self.master.options.console_default_contentview)
|
||||||
[
|
return self.master.commands.execute(cmd)
|
||||||
"@focus",
|
|
||||||
"flowview_mode_%s" % idx,
|
|
||||||
self.master.options.console_default_contentview,
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
@command.command("console.key.contexts")
|
@command.command("console.key.contexts")
|
||||||
def key_contexts(self) -> typing.Sequence[str]:
|
def key_contexts(self) -> typing.Sequence[str]:
|
||||||
|
@ -47,7 +47,7 @@ class Choice:
|
|||||||
class _CommandBase:
|
class _CommandBase:
|
||||||
commands: typing.MutableMapping[str, typing.Any] = {}
|
commands: typing.MutableMapping[str, typing.Any] = {}
|
||||||
|
|
||||||
def call_strings(self, path: str, args: typing.Sequence[str]) -> typing.Any:
|
def _call_strings(self, path: str, args: typing.Sequence[str]) -> typing.Any:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def execute(self, cmd: str) -> typing.Any:
|
def execute(self, cmd: str) -> typing.Any:
|
||||||
@ -337,7 +337,7 @@ class _FlowType(_BaseFlowType):
|
|||||||
|
|
||||||
def parse(self, manager: _CommandBase, t: type, s: str) -> flow.Flow:
|
def parse(self, manager: _CommandBase, t: type, s: str) -> flow.Flow:
|
||||||
try:
|
try:
|
||||||
flows = manager.call_strings("view.flows.resolve", [s])
|
flows = manager.execute("view.flows.resolve %s" % (s))
|
||||||
except exceptions.CommandError as e:
|
except exceptions.CommandError as e:
|
||||||
raise exceptions.TypeError from e
|
raise exceptions.TypeError from e
|
||||||
if len(flows) != 1:
|
if len(flows) != 1:
|
||||||
@ -356,7 +356,7 @@ class _FlowsType(_BaseFlowType):
|
|||||||
|
|
||||||
def parse(self, manager: _CommandBase, t: type, s: str) -> typing.Sequence[flow.Flow]:
|
def parse(self, manager: _CommandBase, t: type, s: str) -> typing.Sequence[flow.Flow]:
|
||||||
try:
|
try:
|
||||||
return manager.call_strings("view.flows.resolve", [s])
|
return manager.execute("view.flows.resolve %s" % (s))
|
||||||
except exceptions.CommandError as e:
|
except exceptions.CommandError as e:
|
||||||
raise exceptions.TypeError from e
|
raise exceptions.TypeError from e
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ def test_save_command(tmpdir):
|
|||||||
v = view.View()
|
v = view.View()
|
||||||
tctx.master.addons.add(v)
|
tctx.master.addons.add(v)
|
||||||
tctx.master.addons.add(sa)
|
tctx.master.addons.add(sa)
|
||||||
tctx.master.commands.call_strings("save.file", ["@shown", p])
|
tctx.master.commands.execute("save.file @shown %s" % p)
|
||||||
|
|
||||||
|
|
||||||
def test_simple(tmpdir):
|
def test_simple(tmpdir):
|
||||||
|
@ -18,7 +18,7 @@ async def test_commands_exist():
|
|||||||
await m.load_flow(tflow())
|
await m.load_flow(tflow())
|
||||||
|
|
||||||
for binding in km.bindings:
|
for binding in km.bindings:
|
||||||
results = command_manager.parse_partial(binding.command)
|
results = command_manager.parse_partial(binding.command.strip())
|
||||||
|
|
||||||
cmd = results[0][0].value
|
cmd = results[0][0].value
|
||||||
args = [a.value for a in results[0][1:]]
|
args = [a.value for a in results[0][1:]]
|
||||||
|
Loading…
Reference in New Issue
Block a user