2018-07-11 17:41:15 +02:00
|
|
|
import itertools
|
|
|
|
import json
|
2018-10-28 16:29:37 +01:00
|
|
|
import os.path
|
|
|
|
import re
|
2018-07-11 17:41:15 +02:00
|
|
|
|
2018-10-28 16:29:37 +01:00
|
|
|
KEYMAP_C = """/* {0}
|
|
|
|
{1}
|
|
|
|
*/
|
|
|
|
[{2}] = {3}(
|
|
|
|
{4})
|
|
|
|
"""
|
2018-07-11 17:41:15 +02:00
|
|
|
|
2018-10-28 16:29:37 +01:00
|
|
|
README_MD = """## {0}
|
|
|
|
```
|
|
|
|
{1}
|
|
|
|
```
|
|
|
|
"""
|
2018-07-11 17:41:15 +02:00
|
|
|
|
2018-10-28 16:29:37 +01:00
|
|
|
base = os.path.dirname(__file__)
|
2018-07-11 17:41:15 +02:00
|
|
|
|
2018-10-28 16:29:37 +01:00
|
|
|
layer_names = dict(enumerate(['_QWERTY', '_LOWER', '_RAISE', '_MOVEMENT', '_NUMPAD', '_FUNCTION']))
|
|
|
|
layer_name = {('MO(%d)' % i): layer_names.get(i).strip('_') for i in layer_names.keys()}
|
2018-07-11 17:41:15 +02:00
|
|
|
|
2018-10-28 16:29:37 +01:00
|
|
|
keys = json.load(open(os.path.join(base, 'keys.json')))
|
2018-07-11 17:41:15 +02:00
|
|
|
|
2018-10-28 16:29:37 +01:00
|
|
|
d = json.load(open(os.path.join(base, 'guidoism.json')))
|
2018-07-11 17:41:15 +02:00
|
|
|
|
|
|
|
def surround(s, a, b, c):
|
|
|
|
return a + b.join(s) + c
|
|
|
|
|
|
|
|
def pattern(cell, table):
|
|
|
|
return ['─'*cell for i in range(table)]
|
|
|
|
|
2018-10-28 16:29:37 +01:00
|
|
|
top = surround(pattern(5, 12), '┌', '┬', '┐')
|
|
|
|
mid = surround(pattern(5, 12), '├', '┼', '┤')
|
|
|
|
bottom = surround(pattern(5, 12), '└', '┴', '┘')
|
|
|
|
|
|
|
|
from more_itertools import chunked, intersperse, interleave_longest
|
|
|
|
|
|
|
|
def uni(k):
|
|
|
|
return keys.get(k, k).lower().center(5)
|
|
|
|
|
|
|
|
def c_layout(i, definition, template):
|
|
|
|
c_name = layer_names[i]
|
|
|
|
pretty_name = c_name.strip('_').capitalize()
|
|
|
|
layout = d['layout']
|
|
|
|
|
|
|
|
surround = lambda s: ''.join(interleave_longest(['│']*(len(s)+1), s))
|
|
|
|
layer = list(map(uni, definition))
|
|
|
|
layer[41] = layer[41].center(11)
|
|
|
|
layer = chunked(layer, 12)
|
|
|
|
rows = intersperse(mid, map(surround, layer))
|
|
|
|
pretty = '\n'.join(itertools.chain([top], rows, [bottom]))
|
|
|
|
|
|
|
|
surround = lambda s: ', '.join(s)
|
|
|
|
layer = list(map(lambda k: layer_name.get(k, k), definition))
|
|
|
|
layer = chunked(layer, 12)
|
|
|
|
rows = map(surround, layer)
|
|
|
|
c_layer = ',\n '.join(itertools.chain([], rows, []))
|
|
|
|
|
|
|
|
return template.format(pretty_name, pretty, c_name, layout, c_layer)
|
|
|
|
|
|
|
|
start = '// START_REPLACEMENT\n'
|
|
|
|
end = '// END_REPLACEMENT\n'
|
|
|
|
replacement = start + ',\n\n'.join(c_layout(i, l, KEYMAP_C) for i, l in enumerate(d['layers'])) + end
|
|
|
|
keymap = os.path.join(base, 'keymap.c')
|
|
|
|
existing = open(keymap).read()
|
|
|
|
r = re.compile(r'// START_REPLACEMENT.*// END_REPLACEMENT', re.DOTALL)
|
|
|
|
open(keymap, 'w').write(r.sub(replacement, existing))
|
|
|
|
|
|
|
|
replacement = '## Current Configuration\n\n' + '\n\n'.join(c_layout(i, l, README_MD) for i, l in enumerate(d['layers']))
|
|
|
|
keymap = os.path.join(base, 'readme.md')
|
|
|
|
existing = open(keymap).read()
|
|
|
|
r = re.compile(r'## Current Configuration.*', re.DOTALL)
|
|
|
|
open(keymap, 'w').write(r.sub(replacement, existing))
|