2 # KLL Compiler Containers
4 # Copyright (C) 2014 by Jacob Alexander
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.
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.
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/>.
25 ## Print Decorator Variables
26 ERROR = '\033[5;1;31mERROR\033[0m:'
34 # Container for capabilities dictionary and convenience functions
36 self.capabilities = dict()
38 def __getitem__( self, name ):
39 return self.capabilities[ name ]
41 def __setitem__( self, name, contents ):
42 self.capabilities[ name ] = contents
45 return "Capabilities => {0}\nIndexed Capabilities => {1}".format( self.capabilities, sorted( self.capabilities, key = self.capabilities.get ) )
48 # Total bytes needed to store arguments
49 def totalArgBytes( self, name ):
52 # Iterate over the arguments, summing the total bytes
53 for arg in self.capabilities[ name ][1]:
54 totalBytes += int( arg[1] )
58 # Name of the capability function
59 def funcName( self, name ):
60 return self.capabilities[ name ][0]
63 # Only valid while dictionary keys are not added/removed
64 def getIndex( self, name ):
65 return sorted( self.capabilities, key = self.capabilities.get ).index( name )
67 def getName( self, index ):
68 return sorted( self.capabilities, key = self.capabilities.get )[ index ]
71 return sorted( self.capabilities, key = self.capabilities.get )
75 # Container for Trigger Macro : Result Macro correlation
76 # Layer selection for generating TriggerLists
78 # Only convert USB Code list once all the ResultMacros have been accumulated (does a macro reduction; not reversible)
79 # Two staged list for ResultMacros:
80 # 1) USB Code/Non-converted (may contain capabilities)
87 self.macros = [ dict() ]
90 return "{0}".format( self.macros )
92 def setLayer( self, layer ):
95 # Use for ScanCode trigger macros
96 def appendScanCode( self, trigger, result ):
97 if not trigger in self.macros[ self.layer ]:
98 self.replaceScanCode( trigger, result )
100 self.macros[ self.layer ][ trigger ].append( result )
102 # Remove the given trigger/result pair
103 def removeScanCode( self, trigger, result ):
104 # Remove all instances of the given trigger/result pair
105 while result in self.macros[ self.layer ][ trigger ]:
106 self.macros[ self.layer ][ trigger ].remove( result )
108 # Replaces the given trigger with the given result
109 # If multiple results for a given trigger, clear, then add
110 def replaceScanCode( self, trigger, result ):
111 self.macros[ self.layer ][ trigger ] = [ result ]
113 # Return a list of ScanCode triggers with the given USB Code trigger
114 def lookupUSBCodes( self, usbCode ):
117 # Scan current layer for USB Codes
118 for macro in self.macros[ self.layer ].keys():
119 if usbCode == self.macros[ self.layer ][ macro ]:
120 scanCodeList.append( macro )