Add flake8 to our test suite and fix all errors (#7379)
[jackhill/qmk/firmware.git] / lib / python / kle2xy.py
1 """ Original code from https://github.com/skullydazed/kle2xy
2 """
3
4 import hjson
5 from decimal import Decimal
6
7
8 class KLE2xy(list):
9 """Abstract interface for interacting with a KLE layout.
10 """
11 def __init__(self, layout=None, name='', invert_y=True):
12 super(KLE2xy, self).__init__()
13
14 self.name = name
15 self.invert_y = invert_y
16 self.key_width = Decimal('19.05')
17 self.key_skel = {'decal': False, 'border_color': 'none', 'keycap_profile': '', 'keycap_color': 'grey', 'label_color': 'black', 'label_size': 3, 'label_style': 4, 'width': Decimal('1'), 'height': Decimal('1'), 'x': Decimal('0'), 'y': Decimal('0')}
18 self.rows = Decimal(0)
19 self.columns = Decimal(0)
20
21 if layout:
22 self.parse_layout(layout)
23
24 @property
25 def width(self):
26 """Returns the width of the keyboard plate.
27 """
28 return (Decimal(self.columns) * self.key_width) + self.key_width / 2
29
30 @property
31 def height(self):
32 """Returns the height of the keyboard plate.
33 """
34 return (self.rows * self.key_width) + self.key_width / 2
35
36 @property
37 def size(self):
38 """Returns the size of the keyboard plate.
39 """
40 return (self.width, self.height)
41
42 def attrs(self, properties):
43 """Parse the keyboard properties dictionary.
44 """
45 # FIXME: Store more than just the keyboard name.
46 if 'name' in properties:
47 self.name = properties['name']
48
49 def parse_layout(self, layout): # noqa FIXME(skullydazed): flake8 says this has a complexity of 25, it should be refactored.
50 # Wrap this in a dictionary so hjson will parse KLE raw data
51 layout = '{"layout": [' + layout + ']}'
52 layout = hjson.loads(layout)['layout']
53
54 # Initialize our state machine
55 current_key = self.key_skel.copy()
56 current_row = Decimal(0)
57 current_col = Decimal(0)
58 current_x = 0
59 current_y = self.key_width / 2
60
61 if isinstance(layout[0], dict):
62 self.attrs(layout[0])
63 layout = layout[1:]
64
65 for row_num, row in enumerate(layout):
66 self.append([])
67
68 # Process the current row
69 for key in row:
70 if isinstance(key, dict):
71 if 'w' in key and key['w'] != Decimal(1):
72 current_key['width'] = Decimal(key['w'])
73 if 'w2' in key and 'h2' in key and key['w2'] == 1.5 and key['h2'] == 1:
74 # FIXME: ISO Key uses these params: {x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25}
75 current_key['isoenter'] = True
76 if 'h' in key and key['h'] != Decimal(1):
77 current_key['height'] = Decimal(key['h'])
78 if 'a' in key:
79 current_key['label_style'] = self.key_skel['label_style'] = int(key['a'])
80 if current_key['label_style'] < 0:
81 current_key['label_style'] = 0
82 elif current_key['label_style'] > 9:
83 current_key['label_style'] = 9
84 if 'f' in key:
85 font_size = int(key['f'])
86 if font_size > 9:
87 font_size = 9
88 elif font_size < 1:
89 font_size = 1
90 current_key['label_size'] = self.key_skel['label_size'] = font_size
91 if 'p' in key:
92 current_key['keycap_profile'] = self.key_skel['keycap_profile'] = key['p']
93 if 'c' in key:
94 current_key['keycap_color'] = self.key_skel['keycap_color'] = key['c']
95 if 't' in key:
96 # FIXME: Need to do better validation, plus figure out how to support multiple colors
97 if '\n' in key['t']:
98 key['t'] = key['t'].split('\n')[0]
99 if key['t'] == "0":
100 key['t'] = "#000000"
101 current_key['label_color'] = self.key_skel['label_color'] = key['t']
102 if 'x' in key:
103 current_col += Decimal(key['x'])
104 current_x += Decimal(key['x']) * self.key_width
105 if 'y' in key:
106 current_row += Decimal(key['y'])
107 current_y += Decimal(key['y']) * self.key_width
108 if 'd' in key:
109 current_key['decal'] = True
110
111 else:
112 current_key['name'] = key
113 current_key['row'] = current_row
114 current_key['column'] = current_col
115
116 # Determine the X center
117 x_center = (current_key['width'] * self.key_width) / 2
118 current_x += x_center
119 current_key['x'] = current_x
120 current_x += x_center
121
122 # Determine the Y center
123 y_center = (current_key['height'] * self.key_width) / 2
124 y_offset = y_center - (self.key_width / 2)
125 current_key['y'] = (current_y + y_offset)
126
127 # Tend to our row/col count
128 current_col += current_key['width']
129 if current_col > self.columns:
130 self.columns = current_col
131
132 # Invert the y-axis if neccesary
133 if self.invert_y:
134 current_key['y'] = -current_key['y']
135
136 # Store this key
137 self[-1].append(current_key)
138 current_key = self.key_skel.copy()
139
140 # Move to the next row
141 current_x = 0
142 current_y += self.key_width
143 current_col = Decimal(0)
144 current_row += Decimal(1)
145 if current_row > self.rows:
146 self.rows = Decimal(current_row)