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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. # Default templates and output files
  31. templatePaths = []
  32. outputPaths = []
  33. # Initializes backend
  34. # Looks for template file and builds list of fill tags
  35. def __init__( self, templatePaths ):
  36. # Use defaults if no template is specified
  37. if templatePaths is not None:
  38. self.templatePaths = templatePaths
  39. self.fill_dict = dict()
  40. # Process each template and add to tagList
  41. self.tagList = []
  42. for templatePath in self.templatePaths:
  43. # Does template exist?
  44. if not os.path.isfile( templatePath ):
  45. print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) )
  46. sys.exit( 1 )
  47. # Generate list of fill tags
  48. with open( templatePath, 'r' ) as openFile:
  49. for line in openFile:
  50. match = re.findall( '<\|([^|>]+)\|>', line )
  51. for item in match:
  52. self.tagList.append( item )
  53. # USB Code Capability Name
  54. # XXX Make sure to override
  55. def usbCodeCapability( self ):
  56. return "my_capability";
  57. # Processes content for fill tags and does any needed dataset calculations
  58. # XXX Make sure to override
  59. def process( self, capabilities, macros, variables, gitRev, gitChanges ):
  60. print ( "{0} BackendBase 'process' function must be overridden".format( ERROR ) )
  61. sys.exit( 2 )
  62. # Generates the output keymap with fill tags filled
  63. def generate( self, outputPaths ):
  64. # Use default if not specified
  65. if outputPaths is None:
  66. outputPaths = self.outputPaths
  67. for templatePath, outputPath in zip( self.templatePaths, outputPaths ):
  68. # Process each line of the template, outputting to the target path
  69. with open( outputPath, 'w' ) as outputFile:
  70. with open( templatePath, 'r' ) as templateFile:
  71. for line in templateFile:
  72. # TODO Support multiple replacements per line
  73. # TODO Support replacement with other text inline
  74. match = re.findall( '<\|([^|>]+)\|>', line )
  75. # If match, replace with processed variable
  76. if match:
  77. outputFile.write( self.fill_dict[ match[ 0 ] ] )
  78. outputFile.write("\n")
  79. # Otherwise, just append template to output file
  80. else:
  81. outputFile.write( line )