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.

containers.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python3
  2. # KLL Compiler Containers
  3. #
  4. # Copyright (C) 2014 by Jacob Alexander
  5. #
  6. # This file is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This file is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this file. If not, see <http://www.gnu.org/licenses/>.
  18. ### Imports ###
  19. ### Decorators ###
  20. ## Print Decorator Variables
  21. ERROR = '\033[5;1;31mERROR\033[0m:'
  22. ### Parsing ###
  23. ## Containers
  24. class Capabilities:
  25. # Container for capabilities dictionary and convenience functions
  26. def __init__( self ):
  27. self.capabilities = dict()
  28. def __getitem__( self, name ):
  29. return self.capabilities[ name ]
  30. def __setitem__( self, name, contents ):
  31. self.capabilities[ name ] = contents
  32. def __repr__( self ):
  33. return "Capabilities => {0}\nIndexed Capabilities => {1}".format( self.capabilities, sorted( self.capabilities, key = self.capabilities.get ) )
  34. # Total bytes needed to store arguments
  35. def totalArgBytes( self, name ):
  36. totalBytes = 0
  37. # Iterate over the arguments, summing the total bytes
  38. for arg in self.capabilities[ name ][1]:
  39. totalBytes += int( arg[1] )
  40. return totalBytes
  41. # Name of the capability function
  42. def funcName( self, name ):
  43. return self.capabilities[ name ][0]
  44. # Only valid while dictionary keys are not added/removed
  45. def getIndex( self, name ):
  46. return sorted( self.capabilities, key = self.capabilities.get ).index( name )
  47. def getName( self, index ):
  48. return sorted( self.capabilities, key = self.capabilities.get )[ index ]
  49. def keys( self ):
  50. return sorted( self.capabilities, key = self.capabilities.get )
  51. class Macros:
  52. # Container for Trigger Macro : Result Macro correlation
  53. # Layer selection for generating TriggerLists
  54. #
  55. # Only convert USB Code list once all the ResultMacros have been accumulated (does a macro reduction; not reversible)
  56. # Two staged list for ResultMacros:
  57. # 1) USB Code/Non-converted (may contain capabilities)
  58. # 2) Capabilities
  59. def __init__( self ):
  60. # Default layer (0)
  61. self.layer = 0
  62. # Macro Storage
  63. self.macros = [ [] ]
  64. def setLayer( self, layer ):
  65. self.layer = layer
  66. # Use for ScanCode trigger macros
  67. def appendScanCode( self, trigger, result ):
  68. self.macros[ self.layer ][ trigger ] = result
  69. # Use for USBCode trigger macros
  70. # An extra lookup is required
  71. def appendUSBCode( self, trigger, result ):
  72. noSuccess = True
  73. for macro in self.macros[ self.layer ].keys():
  74. # USB Code Found
  75. if trigger == self.macros[ self.layer ][ macro ]:
  76. print ( "USBCode - Replacing '{0}' with '{1}' -> '{2}'".format( trigger, macro, result ) )
  77. self.macros[ self.layer ][ macro ] = result
  78. noSuccess = False
  79. # Only show warning if no replacements were done
  80. if noSuccess:
  81. print ( "Warning: '{1}' USB Code not found in layer {1}".format( trigger, self.layer ) )
  82. return False
  83. return True