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, templatePath, definesTemplatePath ):
42 # Does template exist?
43 if not os.path.isfile( templatePath ):
44 print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) )
47 self.definesTemplatePath = definesTemplatePath
48 self.templatePath = templatePath
49 self.fill_dict = dict()
51 # Generate list of fill tags
53 with open( templatePath, 'r' ) as openFile:
55 match = re.findall( '<\|([^|>]+)\|>', line )
57 self.tagList.append( item )
58 with open( definesTemplatePath, 'r' ) as openFile:
60 match = re.findall( '<\|([^|>]+)\|>', line )
62 self.tagList.append( item )
65 # USB Code Capability Name
66 # XXX Make sure to override
67 def usbCodeCapability( self ):
68 return "my_capability";
71 # Processes content for fill tags and does any needed dataset calculations
72 def process( self, capabilities, macros, variables, gitRev, gitChanges ):
73 print ( "{0} BackendBase 'process' function must be overridden".format( ERROR ) )
77 # Generates the output keymap with fill tags filled
78 def generate( self, outputPath, definesOutputPath ):
79 # Process each line of the template, outputting to the target path
80 with open( outputPath, 'w' ) as outputFile:
81 with open( self.templatePath, 'r' ) as templateFile:
82 for line in templateFile:
83 # TODO Support multiple replacements per line
84 # TODO Support replacement with other text inline
85 match = re.findall( '<\|([^|>]+)\|>', line )
87 # If match, replace with processed variable
89 outputFile.write( self.fill_dict[ match[ 0 ] ] )
90 outputFile.write("\n")
92 # Otherwise, just append template to output file
94 outputFile.write( line )
96 # Process each line of the defines template, outputting to the target path
97 with open( definesOutputPath, 'w' ) as outputFile:
98 with open( self.definesTemplatePath, 'r' ) as templateFile:
99 for line in templateFile:
100 # TODO Support multiple replacements per line
101 # TODO Support replacement with other text inline
102 match = re.findall( '<\|([^|>]+)\|>', line )
104 # If match, replace with processed variable
106 outputFile.write( self.fill_dict[ match[ 0 ] ] )
107 outputFile.write("\n")
109 # Otherwise, just append template to output file
111 outputFile.write( line )