2019-07-15 12:14:27 -07:00
""" Generate a keymap.c from a configurator export.
"""
import json
import os
from milc import cli
import qmk . keymap
2019-09-22 13:25:33 -07:00
@cli.argument ( ' -o ' , ' --output ' , arg_only = True , help = ' File to write to ' )
2019-11-12 17:08:55 -08:00
@cli.argument ( ' -q ' , ' --quiet ' , arg_only = True , action = ' store_true ' , help = " Quiet mode, only output error messages " )
2019-09-22 13:25:33 -07:00
@cli.argument ( ' filename ' , arg_only = True , help = ' Configurator JSON file ' )
@cli.subcommand ( ' Create a keymap.c from a QMK Configurator export. ' )
def json_keymap ( cli ) :
2019-07-15 12:14:27 -07:00
""" Generate a keymap.c from a configurator export.
This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided.
"""
# Error checking
if cli . args . filename == ( ' - ' ) :
cli . log . error ( ' Reading from STDIN is not (yet) supported. ' )
cli . print_usage ( )
exit ( 1 )
if not os . path . exists ( qmk . path . normpath ( cli . args . filename ) ) :
cli . log . error ( ' JSON file does not exist! ' )
cli . print_usage ( )
exit ( 1 )
# Environment processing
2019-09-22 13:25:33 -07:00
if cli . args . output == ( ' - ' ) :
cli . args . output = None
2019-07-15 12:14:27 -07:00
# Parse the configurator json
with open ( qmk . path . normpath ( cli . args . filename ) , ' r ' ) as fd :
user_keymap = json . load ( fd )
# Generate the keymap
keymap_c = qmk . keymap . generate ( user_keymap [ ' keyboard ' ] , user_keymap [ ' layout ' ] , user_keymap [ ' layers ' ] )
2019-09-22 13:25:33 -07:00
if cli . args . output :
output_dir = os . path . dirname ( cli . args . output )
2019-07-15 12:14:27 -07:00
if not os . path . exists ( output_dir ) :
os . makedirs ( output_dir )
2019-09-22 13:25:33 -07:00
output_file = qmk . path . normpath ( cli . args . output )
2019-07-15 12:14:27 -07:00
with open ( output_file , ' w ' ) as keymap_fd :
keymap_fd . write ( keymap_c )
2019-11-12 17:08:55 -08:00
if not cli . args . quiet :
cli . log . info ( ' Wrote keymap to %s . ' , cli . args . output )
2019-07-15 12:14:27 -07:00
else :
print ( keymap_c )