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.

backends.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. ### Imports ###
  21. import os
  22. import sys
  23. import re
  24. ### Decorators ###
  25. ## Print Decorator Variables
  26. ERROR = '\033[5;1;31mERROR\033[0m:'
  27. WARNING = '\033[5;1;33mWARNING\033[0m:'
  28. ### Classes ###
  29. class BackendBase:
  30. # Initializes backend
  31. # Looks for template file and builds list of fill tags
  32. def __init__( self, templatePath, definesTemplatePath ):
  33. # Does template exist?
  34. if not os.path.isfile( templatePath ):
  35. print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) )
  36. sys.exit( 1 )
  37. self.definesTemplatePath = definesTemplatePath
  38. self.templatePath = templatePath
  39. self.fill_dict = dict()
  40. # Generate list of fill tags
  41. self.tagList = []
  42. with open( templatePath, 'r' ) as openFile:
  43. for line in openFile:
  44. match = re.findall( '<\|([^|>]+)\|>', line )
  45. for item in match:
  46. self.tagList.append( item )
  47. with open( definesTemplatePath, 'r' ) as openFile:
  48. for line in openFile:
  49. match = re.findall( '<\|([^|>]+)\|>', line )
  50. for item in match:
  51. self.tagList.append( item )
  52. # USB Code Capability Name
  53. # XXX Make sure to override
  54. def usbCodeCapability( self ):
  55. return "my_capability";
  56. # Processes content for fill tags and does any needed dataset calculations
  57. def process( self, capabilities, macros, variables, gitRev, gitChanges ):
  58. print ( "{0} BackendBase 'process' function must be overridden".format( ERROR ) )
  59. sys.exit( 2 )
  60. # Generates the output keymap with fill tags filled
  61. def generate( self, outputPath, definesOutputPath ):
  62. # Process each line of the template, outputting to the target path
  63. with open( outputPath, 'w' ) as outputFile:
  64. with open( self.templatePath, 'r' ) as templateFile:
  65. for line in templateFile:
  66. # TODO Support multiple replacements per line
  67. # TODO Support replacement with other text inline
  68. match = re.findall( '<\|([^|>]+)\|>', line )
  69. # If match, replace with processed variable
  70. if match:
  71. outputFile.write( self.fill_dict[ match[ 0 ] ] )
  72. outputFile.write("\n")
  73. # Otherwise, just append template to output file
  74. else:
  75. outputFile.write( line )
  76. # Process each line of the defines template, outputting to the target path
  77. with open( definesOutputPath, 'w' ) as outputFile:
  78. with open( self.definesTemplatePath, 'r' ) as templateFile:
  79. for line in templateFile:
  80. # TODO Support multiple replacements per line
  81. # TODO Support replacement with other text inline
  82. match = re.findall( '<\|([^|>]+)\|>', line )
  83. # If match, replace with processed variable
  84. if match:
  85. outputFile.write( self.fill_dict[ match[ 0 ] ] )
  86. outputFile.write("\n")
  87. # Otherwise, just append template to output file
  88. else:
  89. outputFile.write( line )