diff --git a/qmk b/qmk index 5c5cf44..bc44888 100755 --- a/qmk +++ b/qmk @@ -22,17 +22,14 @@ _KEYBOARDS = { } -def abs_path(*paths): - dirname = os.path.abspath(os.path.dirname(__file__)) - return os.path.join(dirname, *paths) +def qmk(cmd, targets): + for target in targets: + subprocess.check_call(["qmk", cmd, "-kb", target]) -def qmk(cmd, keyboard): - subprocess.check_call(["qmk", cmd, "-kb", keyboard]) - - -def show(keyboard, render): - render(abs_path("users/mnye"), abs_path("keyboards", keyboard, "keymaps/mnye")) +def show(render, targets): + for target in targets: + render(target) def main(): @@ -42,15 +39,19 @@ def main(): args = parser.parse_args() command = _COMMANDS.get(args.command, args.command) - keyboard = _KEYBOARDS.get(args.keyboard, args.keyboard) + + if args.keyboard == "all": + targets = _KEYBOARDS.values() + else: + targets = [_KEYBOARDS.get(args.keyboard, args.keyboard)] try: if command == "show": - show(keyboard, visualizer.render_ascii) + show(visualizer.render_ascii, targets) elif command == "svg": - show(keyboard, visualizer.render_svg) + show(visualizer.render_svg, targets) else: - qmk(command, keyboard) + qmk(command, targets) except subprocess.CalledProcessError: return 1 except KeyboardInterrupt: diff --git a/visualizer/__init__.py b/visualizer/__init__.py index ab95a08..c11b880 100644 --- a/visualizer/__init__.py +++ b/visualizer/__init__.py @@ -7,18 +7,29 @@ from .svg import SvgLayout -def render_ascii(user_dir, keyboard_dir): +def abs_path(*paths): + dirname = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) + return os.path.join(dirname, *paths) + + +def render_ascii(keyboard): + user_dir = abs_path("users/mnye") + keyboard_dir = abs_path("keyboards", keyboard, "keymaps/mnye") keymap = KeyMap.load(user_dir, keyboard_dir) AsciiTemplate.load(keyboard_dir).render(keymap) -def render_svg(user_dir, keyboard_dir): +def render_svg(keyboard): + user_dir = abs_path("users/mnye") + keyboard_dir = abs_path("keyboards", keyboard, "keymaps/mnye") keymap = KeyMap.load(user_dir, keyboard_dir) - output_file = SvgLayout.load(keyboard_dir).render(keymap) + + output_file = abs_path(keyboard.replace("/", "_") + "_mnye.svg") + SvgLayout.load(keyboard_dir).render(keymap, output_file) print("Wrote svg:", os.path.relpath(output_file)) - _open_file(output_file) + _maybe_open_file(output_file) -def _open_file(f): +def _maybe_open_file(f): if sys.platform == "darwin": subprocess.run(["open", f], check=True) diff --git a/visualizer/svg.py b/visualizer/svg.py index bc51e99..3fe2a8e 100644 --- a/visualizer/svg.py +++ b/visualizer/svg.py @@ -50,18 +50,16 @@ class SvgLayout(object): @classmethod def load(cls, keyboard_dir): - output_file = os.path.join(keyboard_dir, "keymap.svg") with open(os.path.join(keyboard_dir, "svg_layout.json")) as f: keys = [] for key in json.load(f): keys.append(_Key(**key)) - return cls(keys, output_file) + return cls(keys) - def __init__(self, keys, output_file): + def __init__(self, keys): self.keys = keys - self.output_file = output_file - def render(self, keymap): + def render(self, keymap, output_file): # Attach keymap to keys. for layer in keymap.layers: for key, code in zip(self.keys, layer.key_codes()): @@ -101,11 +99,10 @@ def render(self, keymap): _Combo(self.keys[i1], self.keys[i2], combo.code).render(svg) # Write out rendered svg. - with open(self.output_file, "wb") as f: + with open(output_file, "wb") as f: tree = ElementTree.ElementTree(svg) ElementTree.indent(tree) tree.write(f) - return self.output_file class _Key(object):