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