]> git.donarmstrong.com Git - kiibohd-kll.git/blob - kll_lib/backends.py
Adding backend specific template and output defaults.
[kiibohd-kll.git] / kll_lib / backends.py
1 #!/usr/bin/env python3
2 # KLL Compiler - Kiibohd Backend
3 #
4 # Backend code generator classes
5 #
6 # Copyright (C) 2015 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
21 ### Imports ###
22
23 import os
24 import sys
25 import re
26
27
28 ### Decorators ###
29
30  ## Print Decorator Variables
31 ERROR = '\033[5;1;31mERROR\033[0m:'
32 WARNING = '\033[5;1;33mWARNING\033[0m:'
33
34
35
36 ### Classes ###
37
38 class BackendBase:
39         # Default templates and output files
40         templatePaths = []
41         outputPaths = []
42
43         # Initializes backend
44         # Looks for template file and builds list of fill tags
45         def __init__( self, templatePaths ):
46                 # Use defaults if no template is specified
47                 if templatePaths is not None:
48                         self.templatePaths = templatePaths
49
50                 self.fill_dict = dict()
51
52                 # Process each template and add to tagList
53                 self.tagList = []
54                 for templatePath in self.templatePaths:
55                         # Does template exist?
56                         if not os.path.isfile( templatePath ):
57                                 print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) )
58                                 sys.exit( 1 )
59
60                         # Generate list of fill tags
61                         with open( templatePath, 'r' ) as openFile:
62                                 for line in openFile:
63                                         match = re.findall( '<\|([^|>]+)\|>', line )
64                                         for item in match:
65                                                 self.tagList.append( item )
66
67
68         # USB Code Capability Name
69         # XXX Make sure to override
70         def usbCodeCapability( self ):
71                 return "my_capability";
72
73
74         # Processes content for fill tags and does any needed dataset calculations
75         # XXX Make sure to override
76         def process( self, capabilities, macros, variables, gitRev, gitChanges ):
77                 print ( "{0} BackendBase 'process' function must be overridden".format( ERROR ) )
78                 sys.exit( 2 )
79
80
81         # Generates the output keymap with fill tags filled
82         def generate( self, outputPaths ):
83                 # Use default if not specified
84                 if outputPaths is None:
85                         outputPaths = self.outputPaths
86
87                 for templatePath, outputPath in zip( self.templatePaths, outputPaths ):
88                         # Process each line of the template, outputting to the target path
89                         with open( outputPath, 'w' ) as outputFile:
90                                 with open( templatePath, 'r' ) as templateFile:
91                                         for line in templateFile:
92                                                 # TODO Support multiple replacements per line
93                                                 # TODO Support replacement with other text inline
94                                                 match = re.findall( '<\|([^|>]+)\|>', line )
95
96                                                 # If match, replace with processed variable
97                                                 if match:
98                                                         outputFile.write( self.fill_dict[ match[ 0 ] ] )
99                                                         outputFile.write("\n")
100
101                                                 # Otherwise, just append template to output file
102                                                 else:
103                                                         outputFile.write( line )
104