2019-10-08 21:50:21 +02:00
|
|
|
"""List the keymaps for a specific keyboard
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import glob
|
|
|
|
from bs4 import UnicodeDammit
|
|
|
|
|
|
|
|
from milc import cli
|
|
|
|
|
|
|
|
def unicode_text(filename):
|
|
|
|
"""Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding.
|
|
|
|
"""
|
2019-10-09 20:53:18 +02:00
|
|
|
with open(filename, "rb") as fd:
|
2019-10-08 21:50:21 +02:00
|
|
|
text = UnicodeDammit(fd.read())
|
|
|
|
|
|
|
|
if text.contains_replacement_characters:
|
2019-10-09 20:53:18 +02:00
|
|
|
log_warning("%s: Could not determine file encoding, some characters were replaced." % (filename,))
|
2019-10-08 21:50:21 +02:00
|
|
|
|
2019-10-09 20:53:18 +02:00
|
|
|
return text.unicode_markup or ""
|
2019-10-08 21:50:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def unicode_lines(filename):
|
|
|
|
"""Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding.
|
|
|
|
"""
|
2019-10-09 20:53:18 +02:00
|
|
|
return unicode_text(filename).split("\n")
|
2019-10-08 21:50:21 +02:00
|
|
|
|
2019-10-09 20:53:18 +02:00
|
|
|
def parse_rules_mk(keyboard, revision = ""):
|
2019-10-08 21:50:21 +02:00
|
|
|
base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep
|
|
|
|
rules_mk_path_wildcard = os.path.join(base_path, "**", "rules.mk")
|
2019-10-09 20:53:18 +02:00
|
|
|
rules_mk_regex = re.compile(r"^" + base_path + "(?:" + revision + os.path.sep + ")?rules.mk")
|
|
|
|
paths = [path for path in glob.iglob(rules_mk_path_wildcard, recursive = True) if rules_mk_regex.search(path)]
|
2019-10-08 21:50:21 +02:00
|
|
|
|
2019-10-09 10:14:53 +02:00
|
|
|
rules_mk = dict()
|
2019-10-09 20:53:18 +02:00
|
|
|
config_regex = re.compile(r"^\s*(\S+)\s*=\s*((?:\s*\S+)+)")
|
|
|
|
for file_path in paths:
|
|
|
|
rules_mk_content = unicode_lines(file_path)
|
2019-10-08 21:50:21 +02:00
|
|
|
parsed_file = dict()
|
|
|
|
for line in rules_mk_content:
|
2019-10-09 20:53:18 +02:00
|
|
|
found = config_regex.search(line)
|
2019-10-08 21:50:21 +02:00
|
|
|
if found:
|
|
|
|
parsed_file[found.group(1)] = found.group(2)
|
2019-10-09 20:53:18 +02:00
|
|
|
version = file_path.replace(base_path, "").replace(os.path.sep, "").replace("rules.mk", "")
|
2019-10-09 10:14:53 +02:00
|
|
|
rules_mk[version if version else "base"] = parsed_file
|
2019-10-08 21:50:21 +02:00
|
|
|
return rules_mk
|
|
|
|
|
2019-10-09 20:53:18 +02:00
|
|
|
def find_keymaps(base_path, revision = "", community = False):
|
2019-10-09 10:14:53 +02:00
|
|
|
path_wildcard = os.path.join(base_path, "**", "keymap.c")
|
|
|
|
if community:
|
2019-10-09 20:53:18 +02:00
|
|
|
path_regex = re.compile(r"^" + re.escape(base_path) + "(\S+)" + os.path.sep + "keymap\.c")
|
|
|
|
names = [revision + os.path.sep + path_regex.sub(lambda name: name.group(1), path) for path in glob.iglob(path_wildcard, recursive = True)]
|
2019-10-09 10:14:53 +02:00
|
|
|
else:
|
2019-10-09 20:53:18 +02:00
|
|
|
path_regex = re.compile(r"^" + re.escape(base_path) + "(?:" + re.escape(revision) + os.path.sep + ")?keymaps" + os.path.sep + "(\S+)" + os.path.sep + "keymap\.c")
|
|
|
|
names = [revision + os.path.sep + path_regex.sub(lambda name: name.group(1), path) if revision else path_regex.sub(lambda name: name.group(1), path) for path in glob.iglob(path_wildcard, recursive = True) if path_regex.search(path)]
|
|
|
|
return names
|
2019-10-09 10:14:53 +02:00
|
|
|
|
2019-10-09 20:53:18 +02:00
|
|
|
@cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse")
|
2019-10-10 18:54:12 +02:00
|
|
|
@cli.argument("-rv", "--revision", help="Specify the revison name. Example: rev6")
|
2019-10-08 21:50:21 +02:00
|
|
|
@cli.subcommand("List the keymaps for a specific keyboard")
|
|
|
|
def list_keymaps(cli):
|
|
|
|
"""List the keymaps for a specific keyboard
|
|
|
|
"""
|
|
|
|
# ask for user input if keyboard was not provided in the command line
|
|
|
|
keyboard = cli.config.list_keymaps.keyboard if cli.config.list_keymaps.keyboard else input("Keyboard Name: ")
|
2019-10-09 20:53:18 +02:00
|
|
|
revision = cli.config.list_keymaps.revision if cli.config.list_keymaps.revision else ""
|
2019-10-08 21:50:21 +02:00
|
|
|
|
|
|
|
# get all the rules.mk files for the keyboard
|
2019-10-09 20:53:18 +02:00
|
|
|
rules_mk = parse_rules_mk(keyboard, revision)
|
|
|
|
|
|
|
|
if "base" in rules_mk or revision:
|
|
|
|
keyboard_name = keyboard + os.path.sep + revision
|
|
|
|
kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep
|
|
|
|
names = find_keymaps(kb_base_path, revision)
|
|
|
|
else:
|
|
|
|
names = list()
|
2019-10-08 21:50:21 +02:00
|
|
|
|
2019-10-09 20:53:18 +02:00
|
|
|
for rev, data in rules_mk.items():
|
2019-10-09 10:14:53 +02:00
|
|
|
if "LAYOUTS" in data:
|
|
|
|
for layout in data["LAYOUTS"].split():
|
|
|
|
cl_base_path = os.path.join(os.getcwd(), "layouts", "community", layout) + os.path.sep
|
2019-10-09 20:53:18 +02:00
|
|
|
names = names + find_keymaps(cl_base_path, rev, community = True)
|
2019-10-08 21:50:21 +02:00
|
|
|
|
|
|
|
names.sort()
|
|
|
|
|
|
|
|
for name in names:
|
|
|
|
# We echo instead of cli.log.info to allow easier piping of this output
|
2019-10-09 20:53:18 +02:00
|
|
|
cli.echo(keyboard + os.path.sep + name)
|