2 # KLL Compiler - Kiibohd Backend
4 # Backend code generator classes
6 # Copyright (C) 2015 by Jacob Alexander
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.
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.
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/>.
30 ## Print Decorator Variables
31 ERROR = '\033[5;1;31mERROR\033[0m:'
32 WARNING = '\033[5;1;33mWARNING\033[0m:'
40 # Looks for template file and builds list of fill tags
41 def __init__( self, templatePaths=[] ):
42 self.templatePaths = templatePaths
43 self.fill_dict = dict()
45 # Process each template and add to tagList
47 for templatePath in templatePaths:
48 # Does template exist?
49 if not os.path.isfile( templatePath ):
50 print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) )
53 # Generate list of fill tags
54 with open( templatePath, 'r' ) as openFile:
56 match = re.findall( '<\|([^|>]+)\|>', line )
58 self.tagList.append( item )
61 # USB Code Capability Name
62 # XXX Make sure to override
63 def usbCodeCapability( self ):
64 return "my_capability";
67 # Processes content for fill tags and does any needed dataset calculations
68 # XXX Make sure to override
69 def process( self, capabilities, macros, variables, gitRev, gitChanges ):
70 print ( "{0} BackendBase 'process' function must be overridden".format( ERROR ) )
74 # Generates the output keymap with fill tags filled
75 def generate( self, outputPaths ):
76 for templatePath, outputPath in zip( self.templatePaths, outputPaths ):
77 # Process each line of the template, outputting to the target path
78 with open( outputPath, 'w' ) as outputFile:
79 with open( templatePath, 'r' ) as templateFile:
80 for line in templateFile:
81 # TODO Support multiple replacements per line
82 # TODO Support replacement with other text inline
83 match = re.findall( '<\|([^|>]+)\|>', line )
85 # If match, replace with processed variable
87 outputFile.write( self.fill_dict[ match[ 0 ] ] )
88 outputFile.write("\n")
90 # Otherwise, just append template to output file
92 outputFile.write( line )