keymaps.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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):
  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. paths = [path for path in glob.iglob(rules_mk_path_wildcard, recursive=True) if "keymaps" not in path]
  24. rules_mk = dict()
  25. for file in paths:
  26. rules_mk_content = unicode_lines(file)
  27. parsed_file = dict()
  28. for line in rules_mk_content:
  29. found = re.search(r'^\s*(\w+)\s*=\s*((?:\s*\w+)+)', line)
  30. if found:
  31. parsed_file[found.group(1)] = found.group(2)
  32. version = file.replace(base_path, "").replace(os.path.sep, "").replace("rules.mk", "")
  33. rules_mk[version if version else "base"] = parsed_file
  34. return rules_mk
  35. def find_keymaps(base_path, name_finder, community = False):
  36. path_wildcard = os.path.join(base_path, "**", "keymap.c")
  37. if community:
  38. paths = [path for path in glob.iglob(path_wildcard, recursive=True)]
  39. else:
  40. paths = [path for path in glob.iglob(path_wildcard, recursive=True) if 'keymaps' in path]
  41. return list(map(name_finder, paths))
  42. @cli.argument('-kb', '--keyboard', help='Specify keyboard name. Example: 1upkeyboards/1up60hse')
  43. @cli.subcommand("List the keymaps for a specific keyboard")
  44. def list_keymaps(cli):
  45. """List the keymaps for a specific keyboard
  46. """
  47. # ask for user input if keyboard was not provided in the command line
  48. keyboard = cli.config.list_keymaps.keyboard if cli.config.list_keymaps.keyboard else input("Keyboard Name: ")
  49. kb_base_path = os.path.join(os.getcwd(), "keyboards", keyboard) + os.path.sep
  50. km_find_name = lambda path: path.replace(kb_base_path, keyboard + os.path.sep).replace("/keymaps/", ":").replace(os.path.sep + "keymap.c", "")
  51. names = find_keymaps(kb_base_path, km_find_name)
  52. # get all the rules.mk files for the keyboard
  53. rules_mk = parse_rules_mk(keyboard)
  54. for version, data in rules_mk.items():
  55. if "LAYOUTS" in data:
  56. if version == "base":
  57. cl_find_name = lambda path: path.replace(cl_base_path, keyboard + ":").replace(os.path.sep + "keymap.c", "")
  58. else:
  59. cl_find_name = lambda path: path.replace(cl_base_path, keyboard + os.path.sep + version + ":").replace(os.path.sep + "keymap.c", "")
  60. for layout in data["LAYOUTS"].split():
  61. cl_base_path = os.path.join(os.getcwd(), "layouts", "community", layout) + os.path.sep
  62. names = names + find_keymaps(cl_base_path, cl_find_name, community = True)
  63. names.sort()
  64. for name in names:
  65. # We echo instead of cli.log.info to allow easier piping of this output
  66. cli.echo(name)