]> git.donarmstrong.com Git - kiibohd-kll.git/blob - kll_lib/containers.py
Adding list to tuple conversion and USBCode to Capabiltiy conversion.
[kiibohd-kll.git] / kll_lib / containers.py
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
19 ### Imports ###
20
21
22
23 ### Decorators ###
24
25  ## Print Decorator Variables
26 ERROR = '\033[5;1;31mERROR\033[0m:'
27
28
29
30 ### Parsing ###
31
32  ## Containers
33 class Capabilities:
34         # Container for capabilities dictionary and convenience functions
35         def __init__( self ):
36                 self.capabilities = dict()
37
38         def __getitem__( self, name ):
39                 return self.capabilities[ name ]
40
41         def __setitem__( self, name, contents ):
42                 self.capabilities[ name ] = contents
43
44         def __repr__( self ):
45                 return "Capabilities => {0}\nIndexed Capabilities => {1}".format( self.capabilities, sorted( self.capabilities, key = self.capabilities.get ) )
46
47
48         # Total bytes needed to store arguments
49         def totalArgBytes( self, name ):
50                 totalBytes = 0
51
52                 # Iterate over the arguments, summing the total bytes
53                 for arg in self.capabilities[ name ][1]:
54                         totalBytes += int( arg[1] )
55
56                 return totalBytes
57
58         # Name of the capability function
59         def funcName( self, name ):
60                 return self.capabilities[ name ][0]
61
62
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 )
66
67         def getName( self, index ):
68                 return sorted( self.capabilities, key = self.capabilities.get )[ index ]
69
70         def keys( self ):
71                 return sorted( self.capabilities, key = self.capabilities.get )
72
73
74 class Macros:
75         # Container for Trigger Macro : Result Macro correlation
76         # Layer selection for generating TriggerLists
77         #
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)
81         #  2) Capabilities
82         def __init__( self ):
83                 # Default layer (0)
84                 self.layer = 0
85
86                 # Macro Storage
87                 self.macros = [ dict() ]
88
89         def __repr__( self ):
90                 return "{0}".format( self.macros )
91
92         def setLayer( self, layer ):
93                 self.layer = layer
94
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 )
99                 else:
100                         self.macros[ self.layer ][ trigger ].append( result )
101
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 )
107
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 ]
112
113         # Return a list of ScanCode triggers with the given USB Code trigger
114         def lookupUSBCodes( self, usbCode ):
115                 scanCodeList = []
116
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 )
121
122                 return scanCodeList
123