KLL Compiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python3
  2. '''
  3. KLL Compiler
  4. Keyboard Layout Langauge
  5. '''
  6. # Copyright (C) 2014-2016 by Jacob Alexander
  7. #
  8. # This file is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This file is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this file. If not, see <http://www.gnu.org/licenses/>.
  20. ### Imports ###
  21. import argparse
  22. import importlib
  23. import os
  24. import sys
  25. import common.stage as stage
  26. ### Decorators ###
  27. ## Print Decorator Variables
  28. ERROR = '\033[5;1;31mERROR\033[0m:'
  29. WARNING = '\033[5;1;33mWARNING\033[0m:'
  30. ## Python Text Formatting Fixer...
  31. ## Because the creators of Python are averse to proper capitalization.
  32. textFormatter_lookup = {
  33. "usage: " : "Usage: ",
  34. "optional arguments" : "\033[1mOptional Arguments\033[0m",
  35. }
  36. def textFormatter_gettext( s ):
  37. return textFormatter_lookup.get( s, s )
  38. argparse._ = textFormatter_gettext
  39. ### Misc Utility Functions ###
  40. def git_revision( kllPath ):
  41. import subprocess
  42. # Change the path to where kll.py is
  43. origPath = os.getcwd()
  44. os.chdir( kllPath )
  45. # Just in case git can't be found
  46. try:
  47. # Get hash of the latest git commit
  48. revision = subprocess.check_output( ['git', 'rev-parse', 'HEAD'] ).decode()[:-1]
  49. # Get list of files that have changed since the commit
  50. changed = subprocess.check_output( ['git', 'diff-index', '--name-only', 'HEAD', '--'] ).decode().splitlines()
  51. # Get commit date
  52. date = subprocess.check_output( ['git', 'show', '-s', '--format=%ci'] ).decode()[:-1]
  53. except:
  54. revision = "<no git>"
  55. changed = []
  56. date = "<no date>"
  57. # Change back to the old working directory
  58. os.chdir( origPath )
  59. return revision, changed, date
  60. ### Argument Parsing ###
  61. def checkFileExists( filename ):
  62. if not os.path.isfile( filename ):
  63. print ( "{0} {1} does not exist...".format( ERROR, filename ) )
  64. sys.exit( 1 )
  65. def command_line_args( control ):
  66. '''
  67. Initialize argparse and process all command line arguments
  68. @param control: ControlStage object which has access to all the group argument parsers
  69. '''
  70. # Setup argument processor
  71. parser = argparse.ArgumentParser(
  72. usage="%(prog)s [options..] [<generic>..]",
  73. description="KLL Compiler - Generates specified output from KLL .kll files.",
  74. epilog="Example: {0} scan_map.kll".format( os.path.basename( sys.argv[0] ) ),
  75. formatter_class=argparse.RawTextHelpFormatter,
  76. add_help=False,
  77. )
  78. # Get git information
  79. control.git_rev, control.git_changes, control.git_date = git_revision(
  80. os.path.dirname( os.path.realpath( __file__ ) )
  81. )
  82. control.version = "ALPHA 0.5c.{0} - {1}".format( control.git_rev, control.git_date )
  83. # Optional Arguments
  84. parser.add_argument(
  85. '-h', '--help',
  86. action="help",
  87. help="This message."
  88. )
  89. parser.add_argument(
  90. '-v', '--version',
  91. action="version",
  92. version="%(prog)s {0}".format( control.version ),
  93. help="Show program's version number and exit"
  94. )
  95. # Add stage arguments
  96. control.command_line_flags( parser )
  97. # Process Arguments
  98. args = parser.parse_args()
  99. # Utilize parsed arguments in each of the stages
  100. control.command_line_args( args )
  101. ### Main Entry Point ###
  102. if __name__ == '__main__':
  103. # Initialize Control Stages
  104. control = stage.ControlStage()
  105. # Process Command-Line Args
  106. command_line_args( control )
  107. # Process Control Stages
  108. control.process()
  109. # Successful Execution
  110. sys.exit( 0 )