13 # write direct colours with hex codes |
13 # write direct colours with hex codes |
14 return '0x%07X' % self.index |
14 return '0x%07X' % self.index |
15 else: |
15 else: |
16 return str(self.index) |
16 return str(self.index) |
17 def __repr__(self): |
17 def __repr__(self): |
18 try: |
18 return str.format('Colour({!r})', self.index) |
19 return 'colours.' + colours_inverse_dict[self.index] |
|
20 except KeyError: |
|
21 return str.format('Colour({!r})', self.index) |
|
22 @property |
19 @property |
23 def is_direct_colour(self): |
20 def is_direct_colour(self): |
24 return self.index >= 0x2000000 |
21 return self.index >= 0x2000000 |
25 @property |
22 @property |
26 def is_ldconfig_colour(self): |
23 def is_ldconfig_colour(self): |
96 line = line.strip() |
93 line = line.strip() |
97 if line.startswith('0 !COLOUR'): |
94 if line.startswith('0 !COLOUR'): |
98 colour = parse_ldconfig_ldr_line(line) |
95 colour = parse_ldconfig_ldr_line(line) |
99 yield (colour['code'], colour) |
96 yield (colour['code'], colour) |
100 |
97 |
101 class colours: |
98 # LDConfig lookup table |
102 ''' |
|
103 LDConfig colour namespace, exists for interactive mode and for |
|
104 Colour.__repr__ to return something pretty. |
|
105 ''' |
|
106 pass |
|
107 |
|
108 # LDConfig lookup tables |
|
109 colours_inverse_dict = {} |
|
110 ldconfig_colour_data = {} |
99 ldconfig_colour_data = {} |
111 |
100 |
112 def load_colours(ldconfig_ldr): |
101 def load_colours(ldconfig_ldr): |
113 ''' |
102 ''' |
114 Loads colours. Expects a file pointer to LDConfig.ldr as the parameter. |
103 Loads colours. Expects a file pointer to LDConfig.ldr as the parameter. |
115 ''' |
104 ''' |
116 global ldconfig_colour_data |
105 global ldconfig_colour_data |
117 ldconfig_colour_data = dict(parse_ldconfig_ldr(ldconfig_ldr)) |
106 ldconfig_colour_data = dict(parse_ldconfig_ldr(ldconfig_ldr)) |
118 for index, colour in ldconfig_colour_data.items(): |
|
119 identifier = colour['name'].replace(' ', '_').lower() |
|
120 setattr(colours, identifier, Colour(index)) |
|
121 colours_inverse_dict[index] = identifier |
|
122 |
|
123 # Interactive mode support (pass LDConfig.ldr path as a command-line argument) |
|
124 if __name__ == '__main__': |
|
125 from sys import argv |
|
126 with open(argv[1]) as ldconfig_ldr: |
|
127 load_colours(ldconfig_ldr) |
|