keymap: read keys from CONFDIR/keys.yaml by default

This commit is contained in:
Aldo Cortesi 2018-05-10 15:29:29 +12:00
parent 9830e5b597
commit 8c63a8818d
4 changed files with 16 additions and 5 deletions

View File

@ -163,16 +163,25 @@ requiredKeyAttrs = set(["key", "cmd"])
class KeymapConfig:
defaultFile = "keys.yaml"
@command.command("console.keymap.load")
def keymap_load_path(self, path: mitmproxy.types.Path) -> None:
try:
master = ctx.master # type: mitmproxy.tools.console.master.ConsoleMaster
self.load_path(master.keymap, path)
self.load_path(ctx.master.keymap, path) # type: ignore
except (OSError, KeyBindingError) as e:
raise exceptions.CommandError(
"Could not load key bindings - %s" % 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):
if os.path.exists(p) and os.path.isfile(p):
with open(p, "rt", encoding="utf8") as f:
@ -193,7 +202,7 @@ class KeymapConfig:
km.add(
key = v["key"],
command = v["cmd"],
contexts = v.get("ctx", None),
contexts = v.get("ctx", ["global"]),
help = v.get("help", None),
)
except ValueError as e:

View File

@ -56,6 +56,7 @@ class ConsoleMaster(master.Master):
consoleaddons.UnsupportedLog(),
readfile.ReadFile(),
consoleaddons.ConsoleAddon(self),
keymap.KeymapConfig(),
)
def sigint_handler(*args, **kwargs):

View File

@ -78,6 +78,7 @@ def test_load_path(tmpdir):
kmc = keymap.KeymapConfig()
with taddons.context(kmc) as tctx:
km = keymap.Keymap(tctx.master)
tctx.master.keymap = km
with open(dst, 'wb') as f:
f.write(b"\xff\xff\xff")

View File

@ -11,8 +11,8 @@ from mitmproxy.utils import arg_check
(["-T"], "-T is deprecated, please use --mode transparent instead"),
(["-U"], "-U is deprecated, please use --mode upstream:SPEC instead"),
(["--confdir"], "--confdir is deprecated.\n"
"Please use `--set confdir=value` instead.\n"
"To show all options and their default values use --options"),
"Please use `--set confdir=value` instead.\n"
"To show all options and their default values use --options"),
(["--palette"], "--palette is deprecated.\n"
"Please use `--set console_palette=value` instead.\n"
"To show all options and their default values use --options"),