keymaps.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """List the keymaps for a specific keyboard
  2. """
  3. import os
  4. import re
  5. import glob
  6. from bs4 import UnicodeDammit
  7. from milc import cli
  8. def unicode_text(filename):
  9. """Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding.
  10. """
  11. with open(filename, "rb") as fd:
  12. text = UnicodeDammit(fd.read())
  13. if text.contains_replacement_characters:
  14. log_warning("%s: Could not determine file encoding, some characters were replaced." % (filename,))
  15. return text.unicode_markup or ""
  16. def unicode_lines(filename):
  17. """Returns the contents of filename as a UTF-8 string. Tries to DTRT when it comes to encoding.
  18. """
  19. return unicode_text(filename).split("\n")
  20. def parse_rules_mk(keyboard, revision = ""):
  21. base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep
  22. rules_mk_path_wildcard = os.path.join(base_path, "**", "rules.mk")
  23. rules_mk_regex = re.compile(r"^" + base_path + "(?:" + revision + os.path.sep + ")?rules.mk")
  24. paths = [path for path in glob.iglob(rules_mk_path_wildcard, recursive = True) if rules_mk_regex.search(path)]
  25. rules_mk = dict()
  26. config_regex = re.compile(r"^\s*(\S+)\s*=\s*((?:\s*\S+)+)")
  27. for file_path in paths:
  28. rules_mk_content = unicode_lines(file_path)
  29. parsed_file = dict()
  30. for line in rules_mk_content:
  31. found = config_regex.search(line)
  32. if found:
  33. parsed_file[found.group(1)] = found.group(2)
  34. version = file_path.replace(base_path, "").replace(os.path.sep, "").replace("rules.mk", "")
  35. rules_mk[version if version else "base"] = parsed_file
  36. return rules_mk
  37. def find_keymaps(base_path, revision = "", community = False):
  38. path_wildcard = os.path.join(base_path, "**", "keymap.c")
  39. if community:
  40. path_regex = re.compile(r"^" + re.escape(base_path) + "(\S+)" + os.path.sep + "keymap\.c")
  41. names = [revision + os.path.sep + path_regex.sub(lambda name: name.group(1), path) for path in glob.iglob(path_wildcard, recursive = True)]
  42. else:
  43. 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")
  44. 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)]
  45. return names
  46. @cli.argument("-kb", "--keyboard", help="Specify keyboard name. Example: 1upkeyboards/1up60hse")
  47. @cli.argument("-rv", "--revision", help="Specify the revison name. All revision if not specified. Example: rev6")
  48. @cli.subcommand("List the keymaps for a specific keyboard")
  49. def list_keymaps(cli):
  50. """List the keymaps for a specific keyboard
  51. """
  52. # ask for user input if keyboard was not provided in the command line
  53. keyboard = cli.config.list_keymaps.keyboard if cli.config.list_keymaps.keyboard else input("Keyboard Name: ")
  54. revision = cli.config.list_keymaps.revision if cli.config.list_keymaps.revision else ""
  55. # get all the rules.mk files for the keyboard
  56. rules_mk = parse_rules_mk(keyboard, revision)
  57. if "base" in rules_mk or revision:
  58. keyboard_name = keyboard + os.path.sep + revision
  59. kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep
  60. names = find_keymaps(kb_base_path, revision)
  61. else:
  62. names = list()
  63. for rev, data in rules_mk.items():
  64. if "LAYOUTS" in data:
  65. for layout in data["LAYOUTS"].split():
  66. cl_base_path = os.path.join(os.getcwd(), "layouts", "community", layout) + os.path.sep
  67. names = names + find_keymaps(cl_base_path, rev, community = True)
  68. names.sort()
  69. for name in names:
  70. # We echo instead of cli.log.info to allow easier piping of this output
  71. cli.echo(keyboard + os.path.sep + name)