mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2025-02-02 00:05:27 +00:00
keymap: read keys from CONFDIR/keys.yaml by default
This commit is contained in:
parent
9830e5b597
commit
8c63a8818d
@ -163,16 +163,25 @@ requiredKeyAttrs = set(["key", "cmd"])
|
|||||||
|
|
||||||
|
|
||||||
class KeymapConfig:
|
class KeymapConfig:
|
||||||
|
defaultFile = "keys.yaml"
|
||||||
|
|
||||||
@command.command("console.keymap.load")
|
@command.command("console.keymap.load")
|
||||||
def keymap_load_path(self, path: mitmproxy.types.Path) -> None:
|
def keymap_load_path(self, path: mitmproxy.types.Path) -> None:
|
||||||
try:
|
try:
|
||||||
master = ctx.master # type: mitmproxy.tools.console.master.ConsoleMaster
|
self.load_path(ctx.master.keymap, path) # type: ignore
|
||||||
self.load_path(master.keymap, path)
|
|
||||||
except (OSError, KeyBindingError) as e:
|
except (OSError, KeyBindingError) as e:
|
||||||
raise exceptions.CommandError(
|
raise exceptions.CommandError(
|
||||||
"Could not load key bindings - %s" % e
|
"Could not load key bindings - %s" % e
|
||||||
) from e
|
) from e
|
||||||
|
|
||||||
|
def running(self):
|
||||||
|
p = os.path.join(os.path.expanduser(ctx.options.confdir), self.defaultFile)
|
||||||
|
if os.path.exists(p):
|
||||||
|
try:
|
||||||
|
self.load_path(ctx.master.keymap, p)
|
||||||
|
except KeyBindingError as e:
|
||||||
|
ctx.log.error(e)
|
||||||
|
|
||||||
def load_path(self, km, p):
|
def load_path(self, km, p):
|
||||||
if os.path.exists(p) and os.path.isfile(p):
|
if os.path.exists(p) and os.path.isfile(p):
|
||||||
with open(p, "rt", encoding="utf8") as f:
|
with open(p, "rt", encoding="utf8") as f:
|
||||||
@ -193,7 +202,7 @@ class KeymapConfig:
|
|||||||
km.add(
|
km.add(
|
||||||
key = v["key"],
|
key = v["key"],
|
||||||
command = v["cmd"],
|
command = v["cmd"],
|
||||||
contexts = v.get("ctx", None),
|
contexts = v.get("ctx", ["global"]),
|
||||||
help = v.get("help", None),
|
help = v.get("help", None),
|
||||||
)
|
)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
|
@ -56,6 +56,7 @@ class ConsoleMaster(master.Master):
|
|||||||
consoleaddons.UnsupportedLog(),
|
consoleaddons.UnsupportedLog(),
|
||||||
readfile.ReadFile(),
|
readfile.ReadFile(),
|
||||||
consoleaddons.ConsoleAddon(self),
|
consoleaddons.ConsoleAddon(self),
|
||||||
|
keymap.KeymapConfig(),
|
||||||
)
|
)
|
||||||
|
|
||||||
def sigint_handler(*args, **kwargs):
|
def sigint_handler(*args, **kwargs):
|
||||||
|
@ -78,6 +78,7 @@ def test_load_path(tmpdir):
|
|||||||
kmc = keymap.KeymapConfig()
|
kmc = keymap.KeymapConfig()
|
||||||
with taddons.context(kmc) as tctx:
|
with taddons.context(kmc) as tctx:
|
||||||
km = keymap.Keymap(tctx.master)
|
km = keymap.Keymap(tctx.master)
|
||||||
|
tctx.master.keymap = km
|
||||||
|
|
||||||
with open(dst, 'wb') as f:
|
with open(dst, 'wb') as f:
|
||||||
f.write(b"\xff\xff\xff")
|
f.write(b"\xff\xff\xff")
|
||||||
|
@ -11,8 +11,8 @@ from mitmproxy.utils import arg_check
|
|||||||
(["-T"], "-T is deprecated, please use --mode transparent instead"),
|
(["-T"], "-T is deprecated, please use --mode transparent instead"),
|
||||||
(["-U"], "-U is deprecated, please use --mode upstream:SPEC instead"),
|
(["-U"], "-U is deprecated, please use --mode upstream:SPEC instead"),
|
||||||
(["--confdir"], "--confdir is deprecated.\n"
|
(["--confdir"], "--confdir is deprecated.\n"
|
||||||
"Please use `--set confdir=value` instead.\n"
|
"Please use `--set confdir=value` instead.\n"
|
||||||
"To show all options and their default values use --options"),
|
"To show all options and their default values use --options"),
|
||||||
(["--palette"], "--palette is deprecated.\n"
|
(["--palette"], "--palette is deprecated.\n"
|
||||||
"Please use `--set console_palette=value` instead.\n"
|
"Please use `--set console_palette=value` instead.\n"
|
||||||
"To show all options and their default values use --options"),
|
"To show all options and their default values use --options"),
|
||||||
|
Loading…
Reference in New Issue
Block a user