Parent directory

kle2xy.py

5198 bytes
  1""" Original code from https://github.com/skullydazed/kle2xy
  2"""
  3
  4import hjson
  5from decimal import Decimal
  6
  7
  8class 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')}
 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
 59        if isinstance(layout[0], dict):
 60            self.attrs(layout[0])
 61            layout = layout[1:]
 62
 63        for row_num, row in enumerate(layout):
 64            self.append([])
 65
 66            # Process the current row
 67            for key in row:
 68                if isinstance(key, dict):
 69                    if 'w' in key and key['w'] != Decimal(1):
 70                        current_key['width'] = Decimal(key['w'])
 71                    if 'w2' in key and 'h2' in key and key['w2'] == 1.5 and key['h2'] == 1:
 72                        # FIXME: ISO Key uses these params: {x:0.25,w:1.25,h:2,w2:1.5,h2:1,x2:-0.25}
 73                        current_key['isoenter'] = True
 74                    if 'h' in key and key['h'] != Decimal(1):
 75                        current_key['height'] = Decimal(key['h'])
 76                    if 'a' in key:
 77                        current_key['label_style'] = self.key_skel['label_style'] = max(min(int(key['a']), 9), 0)
 78                    if 'f' in key:
 79                        current_key['label_size'] = self.key_skel['label_size'] = max(min(int(key['f']), 9), 1)
 80                    if 'p' in key:
 81                        current_key['keycap_profile'] = self.key_skel['keycap_profile'] = key['p']
 82                    if 'c' in key:
 83                        current_key['keycap_color'] = self.key_skel['keycap_color'] = key['c']
 84                    if 't' in key:
 85                        # FIXME: Need to do better validation, plus figure out how to support multiple colors
 86                        if '\n' in key['t']:
 87                            key['t'] = key['t'].split('\n')[0]
 88                        if key['t'] == "0":
 89                            key['t'] = "#000000"
 90                        current_key['label_color'] = self.key_skel['label_color'] = key['t']
 91                    if 'x' in key:
 92                        current_col += Decimal(key['x'])
 93                    if 'y' in key:
 94                        current_row += Decimal(key['y'])
 95                    if 'd' in key:
 96                        current_key['decal'] = True
 97
 98                else:
 99                    current_key['name'] = key
100                    current_key['row'] = round(current_row, 2)
101                    current_key['column'] = round(current_col, 2)
102
103                    # x,y (units mm) is the center of the key
104                    x_center = current_col + current_key['width'] / 2
105                    y_center = current_row + current_key['height'] / 2
106                    current_key['x'] = x_center * self.key_width
107                    current_key['y'] = y_center * self.key_width
108
109                    # Tend to our row/col count
110                    current_col += current_key['width']
111                    if current_col > self.columns:
112                        self.columns = current_col
113
114                    # Invert the y-axis if neccesary
115                    if self.invert_y:
116                        current_key['y'] = -current_key['y']
117
118                    # Store this key
119                    self[-1].append(current_key)
120                    current_key = self.key_skel.copy()
121
122            # Move to the next row
123            current_col = Decimal(0)
124            current_row += Decimal(1)
125            if current_row > self.rows:
126                self.rows = Decimal(current_row)