X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=kll.py;h=eab30981aa6d4abdeeb318dd80074f26bf669482;hb=5af3e3a0b549e00eb84ac8bb038dc2b9a870f3aa;hp=76dc3b50da92e8564a2e9275d4c89bf47a17fe02;hpb=6454917b11f89eb345d57daf9976ecfeab6509dc;p=kiibohd-kll.git diff --git a/kll.py b/kll.py index 76dc3b5..eab3098 100755 --- a/kll.py +++ b/kll.py @@ -2,7 +2,7 @@ # KLL Compiler # Keyboard Layout Langauge # -# Copyright (C) 2014 by Jacob Alexander +# Copyright (C) 2014-2015 by Jacob Alexander # # This file is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -90,18 +90,14 @@ def processCommandLineArgs(): help="Specify .kll files to generate partial map, multiple files per flag.\n" "Each -p defines another partial map.\n" "Base .kll files (that define the scan code maps) must be defined for each partial map." ) - pArgs.add_argument( '-t', '--template', type=str, default="templates/kiibohdKeymap.h", + pArgs.add_argument( '-t', '--templates', type=str, nargs='+', + default=["templates/kiibohdKeymap.h", "templates/kiibohdDefs.h"], help="Specify template used to generate the keymap.\n" - "Default: templates/kiibohdKeymap.h" ) - pArgs.add_argument( '--defines-template', type=str, default="templates/kiibohdDefs.h", - help="Specify template used to generate kll_defs.h.\n" - "Default: templates/kiibohdDefs.h" ) - pArgs.add_argument( '-o', '--output', type=str, default="generatedKeymap.h", + "Default: templates/kiibohdKeymap.h templates/kiibohdDefs.h" ) + pArgs.add_argument( '-o', '--outputs', type=str, nargs='+', + default=["generatedKeymap.h", "kll_defs.h"], help="Specify output file. Writes to current working directory by default.\n" - "Default: generatedKeymap.h" ) - pArgs.add_argument( '--defines-output', type=str, default="kll_defs.h", - help="Specify output path for kll_defs.h. Writes to current working directory by default.\n" - "Default: kll_defs.h" ) + "Default: generatedKeymap.h kll_defs.h" ) pArgs.add_argument( '-h', '--help', action="help", help="This message." ) @@ -128,7 +124,7 @@ def processCommandLineArgs(): for filename in partial: checkFileExists( filename ) - return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.defines_template, args.output, args.defines_output) + return (baseFiles, defaultFiles, partialFileSets, args.backend, args.templates, args.outputs) @@ -387,7 +383,6 @@ def optionExpansion( sequences ): curLeafList[ leaf ] = 0 if leaf + 1 < len( curLeafList ): curLeafList[ leaf + 1 ] += 1 - break return expandedSequences @@ -402,7 +397,7 @@ def usbCodeToCapability( items ): # Only convert if an integer, otherwise USB Code doesn't need converting if isinstance( items[ variant ][ sequence ][ combo ], int ): # Use backend capability name and a single argument - items[ variant ][ sequence ][ combo ] = tuple( [ backend.usbCodeCapability(), tuple( [ items[ variant ][ sequence ][ combo ] ] ) ] ) + items[ variant ][ sequence ][ combo ] = tuple( [ backend.usbCodeCapability(), tuple( [ hid_lookup_dictionary[ items[ variant ][ sequence ][ combo ] ] ] ) ] ) return items @@ -553,19 +548,51 @@ def processKLLFile( filename ): data = file.read() tokenSequence = tokenize( data ) #print ( pformat( tokenSequence ) ) # Display tokenization - tree = parse( tokenSequence ) + try: + tree = parse( tokenSequence ) + except NoParseError as e: + print("Error parsing %s. %s" % (filename, e.msg), file=sys.stderr) + sys.exit(1) + + +### Misc Utility Functions ### + +def gitRevision( kllPath ): + import subprocess + + # Change the path to where kll.py is + origPath = os.getcwd() + os.chdir( kllPath ) + # Just in case git can't be found + try: + # Get hash of the latest git commit + revision = subprocess.check_output( ['git', 'rev-parse', 'HEAD'] ).decode()[:-1] + + # Get list of files that have changed since the commit + changed = subprocess.check_output( ['git', 'diff-index', '--name-only', 'HEAD', '--'] ).decode().splitlines() + except: + revision = "" + changed = [] + + # Change back to the old working directory + os.chdir( origPath ) + + return revision, changed ### Main Entry Point ### if __name__ == '__main__': - (baseFiles, defaultFiles, partialFileSets, backend_name, template, defines_template, output, defines_output) = processCommandLineArgs() + (baseFiles, defaultFiles, partialFileSets, backend_name, templates, outputs) = processCommandLineArgs() + + # Look up git information on the compiler + gitRev, gitChanges = gitRevision( os.path.dirname( os.path.realpath( __file__ ) ) ) # Load backend module global backend backend_import = importlib.import_module( "backends.{0}".format( backend_name ) ) - backend = backend_import.Backend( template, defines_template ) + backend = backend_import.Backend( templates ) # Process base layout files for filename in baseFiles: @@ -601,10 +628,16 @@ if __name__ == '__main__': macros_map.generate() # Process needed templating variables using backend - backend.process( capabilities_dict, macros_map, variables_dict ) + backend.process( + capabilities_dict, + macros_map, + variables_dict, + gitRev, + gitChanges + ) # Generate output file using template and backend - backend.generate( output, defines_output ) + backend.generate( outputs ) # Successful Execution sys.exit( 0 )