Thu, 26 Aug 2021 19:37:00 +0300
Update license year
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): |
145
fde18c4d6784
refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents:
11
diff
changeset
|
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 | def __eq__(self, other): |
23 | return self.index == other.index | |
24 | def __lt__(self, other): | |
25 | return self.index < other.index | |
26 | def __le__(self, other): | |
27 | return self.index <= other.index | |
28 | def __gt__(self, other): | |
29 | return self.index > other.index | |
30 | def __ge__(self, other): | |
31 | return self.index >= other.index | |
32 | ||
33 | def parse_ldconfig_ldr_line(line): | |
34 | ''' | |
35 | Parses a single LDConfig.ldr line. | |
36 | ||
37 | Returns: | |
38 | · a dict for a successful parsed colour. | |
39 | · None for empty lines and comments. | |
40 | ||
41 | Raises an error for invalid lines. | |
42 | ''' | |
43 | line = line.strip() | |
44 | import re | |
45 | def parse_tag(tag): | |
46 | match = re.search(tag + r'\s+([^ ]+)', line) | |
47 | if match: | |
48 | return match.group(1) | |
49 | else: | |
50 | raise KeyError(str.format('Tag {} not found', tag)) | |
51 | # parse 0 !COLOUR and get the name | |
52 | match = re.search(r'^0\s+!COLOUR\s([^ ]+)', line) | |
53 | if not match: | |
11 | 54 | raise ValueError('Bad LDConfig.ldr line: ' + line) |
8 | 55 | # replace underscores for readability |
56 | name = match.group(1).replace('_', ' ') | |
57 | # parse tags | |
58 | code = int(parse_tag('CODE')) | |
59 | value = parse_tag('VALUE') | |
60 | edge = parse_tag('EDGE') | |
61 | return { | |
62 | 'name': name, | |
63 | 'code': code, | |
64 | 'value': value, | |
65 | 'edge': edge, | |
66 | } | |
67 | ||
68 | def parse_ldconfig_ldr(ldconfig_ldr): | |
69 | ''' | |
70 | Parses LDConfig.ldr and returns pairs | |
71 | ''' | |
72 | for line in ldconfig_ldr: | |
11 | 73 | line = line.strip() |
74 | if line.startswith('0 !COLOUR'): | |
75 | colour = parse_ldconfig_ldr_line(line) | |
8 | 76 | yield (colour['code'], colour) |
77 | ||
78 | def load_colours(ldconfig_ldr): | |
79 | ''' | |
80 | Loads colours. Expects a file pointer to LDConfig.ldr as the parameter. | |
145
fde18c4d6784
refactoring: moved context-dependant data to new class LDrawContext. ldcheck no longer writes the config file, and looks for it in sensible locations like ~/.config and /etc. LDraw libraries can now be specified on the command line.
Teemu Piippo <teemu@hecknology.net>
parents:
11
diff
changeset
|
81 | Returns a lookup table |
8 | 82 | ''' |
147 | 83 | return dict(parse_ldconfig_ldr(ldconfig_ldr)) |