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 2.7KB

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