colours.py

Thu, 26 Aug 2021 19:16:25 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 26 Aug 2021 19:16:25 +0300
changeset 146
3555679d276b
parent 11
b42788f5d0a9
child 147
bec55b021ae7
permissions
-rw-r--r--

Cleanup ldcheck.py
Remove dependency on configobj. Configuration file replaced with command line arguments and rcfile

8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
1 #!/usr/bin/env python3
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
2 class Colour:
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
3 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
4 Colour interface. Supports LDConfig colours and direct colours.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
5 For LDConfig colours to work, load_colours must be called first.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
6 '''
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
7 def __init__(self, index):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
8 if not isinstance(index, int):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
9 index = int(index, 0)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
10 self.index = index
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
11 def __str__(self):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
12 if self.is_direct_colour:
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
13 # write direct colours with hex codes
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
14 return '0x%07X' % self.index
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
15 else:
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
16 return str(self.index)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
17 def __repr__(self):
146
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
18 return str.format('Colour({!r})', self.index)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
19 @property
4
8eb83f200486 color -> colour
Santeri Piippo
parents: 3
diff changeset
20 def is_direct_colour(self):
3
1dc58f44d556 Can now write dat files, added direct color handling
Santeri Piippo
parents: 2
diff changeset
21 return self.index >= 0x2000000
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
22 @property
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
23 def is_ldconfig_colour(self):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
24 return self.index in ldconfig_colour_data
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
25 @property
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
26 def name(self):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
27 if self.is_ldconfig_colour:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
28 return ldconfig_colour_data[self.index]['name']
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
29 else:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
30 return str(self)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
31 @property
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
32 def face_colour(self):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
33 if self.is_ldconfig_colour:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
34 return ldconfig_colour_data[self.index]['value']
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
35 elif self.is_direct_colour:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
36 return '#%06X' % (self.index & 0xffffff)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
37 else:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
38 return '#000000'
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
39 @property
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
40 def is_valid(self):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
41 return self.is_ldconfig_colour or self.is_direct_colour
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
42 def __eq__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
43 return self.index == other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
44 def __lt__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
45 return self.index < other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
46 def __le__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
47 return self.index <= other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
48 def __gt__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
49 return self.index > other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
50 def __ge__(self, other):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
51 return self.index >= other.index
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
52
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
53 def parse_ldconfig_ldr_line(line):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
54 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
55 Parses a single LDConfig.ldr line.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
56
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
57 Returns:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
58 · a dict for a successful parsed colour.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
59 · None for empty lines and comments.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
60
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
61 Raises an error for invalid lines.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
62 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
63 line = line.strip()
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
64 import re
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
65 def parse_tag(tag):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
66 match = re.search(tag + r'\s+([^ ]+)', line)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
67 if match:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
68 return match.group(1)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
69 else:
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
70 raise KeyError(str.format('Tag {} not found', tag))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
71 # parse 0 !COLOUR and get the name
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
72 match = re.search(r'^0\s+!COLOUR\s([^ ]+)', line)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
73 if not match:
11
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
74 raise ValueError('Bad LDConfig.ldr line: ' + line)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
75 # replace underscores for readability
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
76 name = match.group(1).replace('_', ' ')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
77 # parse tags
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
78 code = int(parse_tag('CODE'))
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
79 value = parse_tag('VALUE')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
80 edge = parse_tag('EDGE')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
81 return {
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
82 'name': name,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
83 'code': code,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
84 'value': value,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
85 'edge': edge,
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
86 }
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
87
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
88 def parse_ldconfig_ldr(ldconfig_ldr):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
89 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
90 Parses LDConfig.ldr and returns pairs
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
91 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
92 for line in ldconfig_ldr:
11
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
93 line = line.strip()
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
94 if line.startswith('0 !COLOUR'):
b42788f5d0a9 fix ldconfig.ldr parsing
Santeri Piippo
parents: 8
diff changeset
95 colour = parse_ldconfig_ldr_line(line)
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
96 yield (colour['code'], colour)
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
97
146
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 11
diff changeset
98 # LDConfig lookup table
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
99 ldconfig_colour_data = {}
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
100
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
101 def load_colours(ldconfig_ldr):
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
102 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
103 Loads colours. Expects a file pointer to LDConfig.ldr as the parameter.
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
104 '''
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
105 global ldconfig_colour_data
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 4
diff changeset
106 ldconfig_colour_data = dict(parse_ldconfig_ldr(ldconfig_ldr))

mercurial