summaryrefslogtreecommitdiff
path: root/lib/python/qmk/keycodes.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/keycodes.py')
-rw-r--r--lib/python/qmk/keycodes.py99
1 files changed, 79 insertions, 20 deletions
diff --git a/lib/python/qmk/keycodes.py b/lib/python/qmk/keycodes.py
index cf1ee0767a..d2f2492829 100644
--- a/lib/python/qmk/keycodes.py
+++ b/lib/python/qmk/keycodes.py
@@ -1,8 +1,64 @@
from pathlib import Path
-from qmk.json_schema import deep_update, json_load, validate
+from qmk.json_schema import merge_ordered_dicts, deep_update, json_load, validate
-CONSTANTS_PATH = Path('data/constants/keycodes/')
+CONSTANTS_PATH = Path('data/constants/')
+KEYCODES_PATH = CONSTANTS_PATH / 'keycodes'
+EXTRAS_PATH = KEYCODES_PATH / 'extras'
+
+
+def _find_versions(path, prefix):
+ ret = []
+ for file in path.glob(f'{prefix}_[0-9].[0-9].[0-9].hjson'):
+ ret.append(file.stem.split('_')[-1])
+
+ ret.sort(reverse=True)
+ return ret
+
+
+def _potential_search_versions(version, lang=None):
+ versions = list_versions(lang)
+ versions.reverse()
+
+ loc = versions.index(version) + 1
+
+ return versions[:loc]
+
+
+def _search_path(lang=None):
+ return EXTRAS_PATH if lang else KEYCODES_PATH
+
+
+def _search_prefix(lang=None):
+ return f'keycodes_{lang}' if lang else 'keycodes'
+
+
+def _locate_files(path, prefix, versions):
+ # collate files by fragment "type"
+ files = {'_': []}
+ for version in versions:
+ files['_'].append(path / f'{prefix}_{version}.hjson')
+
+ for file in path.glob(f'{prefix}_{version}_*.hjson'):
+ fragment = file.stem.replace(f'{prefix}_{version}_', '')
+ if fragment not in files:
+ files[fragment] = []
+ files[fragment].append(file)
+
+ return files
+
+
+def _process_files(files):
+ # allow override within types of fragments - but not globally
+ spec = {}
+ for category in files.values():
+ specs = []
+ for file in category:
+ specs.append(json_load(file))
+
+ deep_update(spec, merge_ordered_dicts(specs))
+
+ return spec
def _validate(spec):
@@ -19,26 +75,21 @@ def _validate(spec):
raise ValueError(f'Keycode spec contains duplicate keycodes! ({",".join(duplicates)})')
-def load_spec(version):
+def load_spec(version, lang=None):
"""Build keycode data from the requested spec file
"""
if version == 'latest':
- version = list_versions()[0]
+ version = list_versions(lang)[0]
- file = CONSTANTS_PATH / f'keycodes_{version}.hjson'
- if not file.exists():
- raise ValueError(f'Requested keycode spec ({version}) is invalid!')
+ path = _search_path(lang)
+ prefix = _search_prefix(lang)
+ versions = _potential_search_versions(version, lang)
- # Load base
- spec = json_load(file)
-
- # Merge in fragments
- fragments = CONSTANTS_PATH.glob(f'keycodes_{version}_*.hjson')
- for file in fragments:
- deep_update(spec, json_load(file))
+ # Load bases + any fragments
+ spec = _process_files(_locate_files(path, prefix, versions))
# Sort?
- spec['keycodes'] = dict(sorted(spec['keycodes'].items()))
+ spec['keycodes'] = dict(sorted(spec.get('keycodes', {}).items()))
# Validate?
_validate(spec)
@@ -46,12 +97,20 @@ def load_spec(version):
return spec
-def list_versions():
+def list_versions(lang=None):
"""Return available versions - sorted newest first
"""
- ret = []
- for file in CONSTANTS_PATH.glob('keycodes_[0-9].[0-9].[0-9].hjson'):
- ret.append(file.stem.split('_')[1])
+ path = _search_path(lang)
+ prefix = _search_prefix(lang)
+
+ return _find_versions(path, prefix)
+
+
+def list_languages():
+ """Return available languages
+ """
+ ret = set()
+ for file in EXTRAS_PATH.glob('keycodes_*_[0-9].[0-9].[0-9].hjson'):
+ ret.add(file.stem.split('_')[1])
- ret.sort(reverse=True)
return ret