colours.py

Wed, 29 May 2019 16:36:23 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 29 May 2019 16:36:23 +0300
changeset 53
0cc196c634f1
parent 11
b42788f5d0a9
child 145
fde18c4d6784
child 146
3555679d276b
permissions
-rw-r--r--

fixed the formatting of the license

#!/usr/bin/env python3
class Colour:
    '''
        Colour interface. Supports LDConfig colours and direct colours.
        For LDConfig colours to work, load_colours must be called first.
    '''
    def __init__(self, index):
        if not isinstance(index, int):
            index = int(index, 0)
        self.index = index
    def __str__(self):
        if self.is_direct_colour:
            # write direct colours with hex codes
            return '0x%07X' % self.index
        else:
            return str(self.index)
    def __repr__(self):
        try:
            return 'colours.' + colours_inverse_dict[self.index]
        except KeyError:
            return str.format('Colour({!r})', self.index)
    @property
    def is_direct_colour(self):
        return self.index >= 0x2000000
    @property
    def is_ldconfig_colour(self):
        return self.index in ldconfig_colour_data
    @property
    def name(self):
        if self.is_ldconfig_colour:
            return ldconfig_colour_data[self.index]['name']
        else:
            return str(self)
    @property
    def face_colour(self):
        if self.is_ldconfig_colour:
            return ldconfig_colour_data[self.index]['value']
        elif self.is_direct_colour:
            return '#%06X' % (self.index & 0xffffff)
        else:
            return '#000000'
    @property
    def is_valid(self):
        return self.is_ldconfig_colour or self.is_direct_colour
    def __eq__(self, other):
        return self.index == other.index
    def __lt__(self, other):
        return self.index < other.index
    def __le__(self, other):
        return self.index <= other.index
    def __gt__(self, other):
        return self.index > other.index
    def __ge__(self, other):
        return self.index >= other.index

def parse_ldconfig_ldr_line(line):
    '''
        Parses a single LDConfig.ldr line.

        Returns:
            · a dict for a successful parsed colour.
            · None for empty lines and comments.

        Raises an error for invalid lines.
    '''
    line = line.strip()
    import re
    def parse_tag(tag):
        match = re.search(tag + r'\s+([^ ]+)', line)
        if match:
            return match.group(1)
        else:
            raise KeyError(str.format('Tag {} not found', tag))
    # parse 0 !COLOUR and get the name
    match = re.search(r'^0\s+!COLOUR\s([^ ]+)', line)
    if not match:
        raise ValueError('Bad LDConfig.ldr line: ' + line)
    # replace underscores for readability
    name = match.group(1).replace('_', ' ')
    # parse tags
    code = int(parse_tag('CODE'))
    value = parse_tag('VALUE')
    edge = parse_tag('EDGE')
    return {
        'name': name,
        'code': code,
        'value': value,
        'edge': edge,
    }

def parse_ldconfig_ldr(ldconfig_ldr):
    '''
        Parses LDConfig.ldr and returns pairs
    '''
    for line in ldconfig_ldr:
        line = line.strip()
        if line.startswith('0 !COLOUR'):
            colour = parse_ldconfig_ldr_line(line)
            yield (colour['code'], colour)

class colours:
    '''
        LDConfig colour namespace, exists for interactive mode and for
        Colour.__repr__ to return something pretty.
    '''
    pass

# LDConfig lookup tables
colours_inverse_dict = {}
ldconfig_colour_data = {}

def load_colours(ldconfig_ldr):
    '''
        Loads colours. Expects a file pointer to LDConfig.ldr as the parameter.
    '''
    global ldconfig_colour_data
    ldconfig_colour_data = dict(parse_ldconfig_ldr(ldconfig_ldr))
    for index, colour in ldconfig_colour_data.items():
        identifier = colour['name'].replace(' ', '_').lower()
        setattr(colours, identifier, Colour(index))
        colours_inverse_dict[index] = identifier

# Interactive mode support (pass LDConfig.ldr path as a command-line argument)
if __name__ == '__main__':
    from sys import argv
    with open(argv[1]) as ldconfig_ldr:
        load_colours(ldconfig_ldr)

mercurial