#!/usr/bin/env python3
'''
KLL Compiler
Keyboard Layout Langauge
'''
# Copyright (C) 2014-2016 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
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this file. If not, see .
### Imports ###
import argparse
import importlib
import os
import sys
import common.stage as stage
### Decorators ###
## Print Decorator Variables
ERROR = '\033[5;1;31mERROR\033[0m:'
WARNING = '\033[5;1;33mWARNING\033[0m:'
## Python Text Formatting Fixer...
## Because the creators of Python are averse to proper capitalization.
textFormatter_lookup = {
"usage: " : "Usage: ",
"optional arguments" : "\033[1mOptional Arguments\033[0m",
}
def textFormatter_gettext( s ):
return textFormatter_lookup.get( s, s )
argparse._ = textFormatter_gettext
### Misc Utility Functions ###
def git_revision( 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()
# Get commit date
date = subprocess.check_output( ['git', 'show', '-s', '--format=%ci'] ).decode()[:-1]
except:
revision = ""
changed = []
date = ""
# Change back to the old working directory
os.chdir( origPath )
return revision, changed, date
### Argument Parsing ###
def checkFileExists( filename ):
if not os.path.isfile( filename ):
print ( "{0} {1} does not exist...".format( ERROR, filename ) )
sys.exit( 1 )
def command_line_args( control ):
'''
Initialize argparse and process all command line arguments
@param control: ControlStage object which has access to all the group argument parsers
'''
# Setup argument processor
parser = argparse.ArgumentParser(
usage="%(prog)s [options..] [..]",
description="KLL Compiler - Generates specified output from KLL .kll files.",
epilog="Example: {0} scan_map.kll".format( os.path.basename( sys.argv[0] ) ),
formatter_class=argparse.RawTextHelpFormatter,
add_help=False,
)
# Get git information
control.git_rev, control.git_changes, control.git_date = git_revision(
os.path.dirname( os.path.realpath( __file__ ) )
)
control.version = "ALPHA 0.5c.{0} - {1}".format( control.git_rev, control.git_date )
# Optional Arguments
parser.add_argument(
'-h', '--help',
action="help",
help="This message."
)
parser.add_argument(
'-v', '--version',
action="version",
version="%(prog)s {0}".format( control.version ),
help="Show program's version number and exit"
)
# Add stage arguments
control.command_line_flags( parser )
# Process Arguments
args = parser.parse_args()
# Utilize parsed arguments in each of the stages
control.command_line_args( args )
### Main Entry Point ###
if __name__ == '__main__':
# Initialize Control Stages
control = stage.ControlStage()
# Process Command-Line Args
command_line_args( control )
# Process Control Stages
control.process()
# Successful Execution
sys.exit( 0 )