Skip to content

Commit

Permalink
Refactor visualizer entrypoint.
Browse files Browse the repository at this point in the history
A few changes:
 * Moves directory logic out of the script, into the visualizer.
 * Adds an `all` target (primarily useful for svg)
 * Puts SVGs into the root
  • Loading branch information
Michael Nye committed Jul 19, 2024
1 parent 29cf395 commit cacec6f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
27 changes: 14 additions & 13 deletions qmk
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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:
Expand Down
21 changes: 16 additions & 5 deletions visualizer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 4 additions & 7 deletions visualizer/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit cacec6f

Please sign in to comment.