Thu, 26 Aug 2021 19:43:03 +0300
Fix merge issues
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
1 | #!/usr/bin/env python3 |
146 | 2 | import sys |
3 | if sys.version_info < (3, 4): | |
8 | 4 | raise RuntimeError('Python 3.4 or newer required') |
5 | from colours import load_colours | |
149 | 6 | |
7 | try: | |
8 | import colorama | |
9 | except ImportError: | |
10 | colorama = None | |
8 | 11 | |
100
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
98
diff
changeset
|
12 | appname = 'ldcheck' |
114
9ea2549e6171
changed version to 1.0.9999
Teemu Piippo <teemu@hecknology.net>
parents:
113
diff
changeset
|
13 | version = (1, 0, 9999) |
100
62759e5c4554
add some basic versioning
Teemu Piippo <teemu@hecknology.net>
parents:
98
diff
changeset
|
14 | version_string = str.join('.', map(str, version)) |
7 | 15 | from geometry import * |
8 | 16 | from pathlib import Path |
13 | 17 | import linetypes |
47
4da025d0b283
added work on header check
Teemu Piippo <teemu@hecknology.net>
parents:
38
diff
changeset
|
18 | import header |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
19 | import parse |
3
1dc58f44d556
Can now write dat files, added direct color handling
Santeri Piippo
parents:
diff
changeset
|
20 | |
146 | 21 | def check_library_paths(library_paths): |
22 | for library_path in library_paths: | |
34
7ed2e831acd4
the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
23 | if not library_path.exists(): |
146 | 24 | raise RuntimeError(str.format( |
25 | 'error: library path {} does not exist', | |
34
7ed2e831acd4
the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
26 | library_path, |
7ed2e831acd4
the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
27 | )) |
7ed2e831acd4
the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
28 | elif not library_path.exists(): |
146 | 29 | raise RuntimeError(str.format( |
30 | 'error: library path {} is not a directory', | |
34
7ed2e831acd4
the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
31 | library_path, |
7ed2e831acd4
the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
32 | )) |
7ed2e831acd4
the program now checks that all ldraw paths are reachable
Teemu Piippo <teemu@hecknology.net>
parents:
32
diff
changeset
|
33 | |
146 | 34 | def find_ldconfig_ldr_paths(libraries): |
35 | for library_path in libraries: | |
17 | 36 | yield from [ |
37 | library_path / path | |
38 | for path in ['LDConfig.ldr', 'ldconfig.ldr'] | |
39 | if (library_path / path).is_file() | |
8 | 40 | ] |
41 | ||
146 | 42 | def load_ldconfig(libraries): |
43 | ldconfig_ldr_paths = list(find_ldconfig_ldr_paths(libraries)) | |
44 | if not ldconfig_ldr_paths: | |
45 | raise RuntimeError('could not find any LDConfig.ldr') | |
46 | for ldconfig_ldr_path in ldconfig_ldr_paths: | |
47 | with ldconfig_ldr_path.open() as ldconfig_ldr: | |
48 | load_colours(ldconfig_ldr) | |
9
fea8e9ae6f29
Added matrix code, moved library paths to ldcheck.cfg.
Santeri Piippo
parents:
8
diff
changeset
|
49 | |
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:
122
diff
changeset
|
50 | class LDrawContext: |
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:
122
diff
changeset
|
51 | ''' |
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:
122
diff
changeset
|
52 | Contains context-dependant LDraw information, like library directory |
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:
122
diff
changeset
|
53 | paths and the colour table. |
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:
122
diff
changeset
|
54 | ''' |
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:
122
diff
changeset
|
55 | def __init__(self, libraries = None): |
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:
122
diff
changeset
|
56 | self._libraries = libraries |
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:
122
diff
changeset
|
57 | if not self._libraries: |
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:
122
diff
changeset
|
58 | self._libraries = ldraw_dirs_from_config() |
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:
122
diff
changeset
|
59 | self.ldconfig_colour_data = self.load_ldconfig_ldr() |
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:
122
diff
changeset
|
60 | self.check_library_paths() |
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:
122
diff
changeset
|
61 | @property |
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:
122
diff
changeset
|
62 | def libraries(self): |
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:
122
diff
changeset
|
63 | return self._libraries |
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:
122
diff
changeset
|
64 | @property |
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:
122
diff
changeset
|
65 | def colours(self): |
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:
122
diff
changeset
|
66 | return self.ldconfig_colour_data |
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:
122
diff
changeset
|
67 | def ldconfig_ldr_discovery(self): |
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:
122
diff
changeset
|
68 | for library_path in self.libraries: |
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:
122
diff
changeset
|
69 | yield from [ |
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:
122
diff
changeset
|
70 | library_path / path |
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:
122
diff
changeset
|
71 | for path in ['LDConfig.ldr', 'ldconfig.ldr'] |
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:
122
diff
changeset
|
72 | if (library_path / path).is_file() |
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:
122
diff
changeset
|
73 | ] |
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:
122
diff
changeset
|
74 | def load_ldconfig_ldr(self): |
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:
122
diff
changeset
|
75 | from colours import load_colours |
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:
122
diff
changeset
|
76 | for ldconfig_ldr_path in self.ldconfig_ldr_discovery(): |
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:
122
diff
changeset
|
77 | with open(ldconfig_ldr_path) as ldconfig_ldr: |
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:
122
diff
changeset
|
78 | return load_colours(ldconfig_ldr) |
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:
122
diff
changeset
|
79 | def check_library_paths(self): |
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:
122
diff
changeset
|
80 | from sys import stderr |
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:
122
diff
changeset
|
81 | problems = False |
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:
122
diff
changeset
|
82 | have_paths = False |
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:
122
diff
changeset
|
83 | for library_path in self.libraries: |
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:
122
diff
changeset
|
84 | have_paths = True |
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:
122
diff
changeset
|
85 | if not library_path.exists(): |
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:
122
diff
changeset
|
86 | problems = True |
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:
122
diff
changeset
|
87 | print(str.format( |
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:
122
diff
changeset
|
88 | 'Library path {} does not exist', |
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:
122
diff
changeset
|
89 | library_path, |
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:
122
diff
changeset
|
90 | ), file = stderr) |
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:
122
diff
changeset
|
91 | elif not library_path.exists(): |
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:
122
diff
changeset
|
92 | problems = True |
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:
122
diff
changeset
|
93 | print(str.format( |
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:
122
diff
changeset
|
94 | 'Library path {} is not a directory', |
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:
122
diff
changeset
|
95 | library_path, |
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:
122
diff
changeset
|
96 | ), file = stderr) |
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:
122
diff
changeset
|
97 | if not have_paths: |
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:
122
diff
changeset
|
98 | raise RuntimeError('no LDraw library paths specified') |
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:
122
diff
changeset
|
99 | def is_ldconfig_colour(self, colour): |
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:
122
diff
changeset
|
100 | return colour.index in self.colours |
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:
122
diff
changeset
|
101 | def colour_name(self, colour): |
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:
122
diff
changeset
|
102 | if self.is_ldconfig_colour(colour): |
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:
122
diff
changeset
|
103 | return self.colours[self.index]['name'] |
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:
122
diff
changeset
|
104 | else: |
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:
122
diff
changeset
|
105 | return str(colour) |
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:
122
diff
changeset
|
106 | def colour_face(self, colour): |
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:
122
diff
changeset
|
107 | if self.is_ldconfig_colour(colour): |
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:
122
diff
changeset
|
108 | return self.colours[self.index]['value'] |
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:
122
diff
changeset
|
109 | elif colour.is_direct_colour: |
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:
122
diff
changeset
|
110 | return '#%06X' % (self.index & 0xffffff) |
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:
122
diff
changeset
|
111 | else: |
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:
122
diff
changeset
|
112 | return '#000000' |
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:
122
diff
changeset
|
113 | def is_valid_colour(self, colour): |
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:
122
diff
changeset
|
114 | return self.is_ldconfig_colour(colour) or colour.is_direct_colour |
62
f0a6bf48b05e
Problem reporting revamp, program is now aware of its problem types
Teemu Piippo <teemu@hecknology.net>
parents:
54
diff
changeset
|
115 | |
146 | 116 | def parse_commandline_arguments(): |
117 | import os | |
118 | rcpath = Path(os.path.expanduser('~/.config/ldcheckrc')) | |
119 | if rcpath.exists(): | |
120 | with rcpath.open() as file: | |
121 | rcargs = ['--' + line.strip() for line in file] | |
122 | else: | |
123 | rcargs = [] | |
124 | import argparse | |
125 | class ListProblemTypesAction(argparse.Action): | |
126 | def __init__(self, option_strings, dest, nargs = None, **kwargs): | |
127 | super().__init__(option_strings, dest, nargs = 0, **kwargs) | |
128 | def __call__(self, *args, **kwargs): | |
129 | import testsuite | |
130 | test_suite = testsuite.load_tests() | |
131 | for warning_type in testsuite.all_problem_types(test_suite): | |
132 | print(str.format('{name}: {severity}: "{message}"', | |
133 | name = warning_type.name, | |
134 | severity = warning_type.severity, | |
135 | message = warning_type.placeholder_message(), | |
136 | )) | |
137 | sys.exit(0) | |
149 | 138 | parser = argparse.ArgumentParser() |
139 | parser.add_argument('filename') | |
140 | parser.add_argument('-l', '--library', action = 'append') | |
141 | parser.add_argument('--list', | |
142 | action = ListProblemTypesAction, | |
143 | help = 'lists all possible problem types and exit', | |
144 | ) | |
145 | parser.add_argument('--dump', | |
146 | action = 'store_true', | |
147 | help = 'dumps the internal parsed structure of the part file', | |
148 | ) | |
149 | parser.add_argument('--rebuild', | |
150 | action = 'store_true', | |
151 | help = 'parses the part file and prints it back out, used for ' | |
152 | 'testing whether the program interprets part files correctly', | |
153 | ) | |
154 | parser.add_argument('--subfile', | |
155 | action = 'store_true', | |
156 | help = 'finds a subfile by name and prints out information about it' | |
157 | ) | |
158 | parser.add_argument('--color', | |
159 | action = 'store_true', | |
160 | help = 'use colors' | |
161 | ) | |
162 | parser.add_argument('-d', '--ldraw-dir', | |
163 | nargs = '+', | |
164 | help = 'specify LDraw directory path(s)', | |
165 | ) | |
166 | parser.add_argument('-v', '--version', | |
167 | action = 'version', | |
168 | version = str.format('{appname} {version}', | |
169 | appname = appname, | |
170 | version = version_string, | |
171 | ), | |
172 | ) | |
173 | arglist = rcargs + sys.argv[1:] | |
174 | return parser.parse_args(arglist) | |
26
7c263b864371
Added command line option to list all checks.
Santeri Piippo
parents:
21
diff
changeset
|
175 | |
98 | 176 | def format_report(report, model, test_suite, *, use_colors = True): |
177 | from testsuite import problem_text | |
178 | messages = [] | |
179 | for problem in report['problems']: | |
180 | text_colour = '' | |
181 | if use_colors: | |
182 | if problem.severity == 'hold': | |
183 | text_colour = colorama.Fore.LIGHTRED_EX | |
184 | elif problem.severity == 'warning': | |
185 | text_colour = colorama.Fore.LIGHTBLUE_EX | |
186 | ldraw_code = model.body[problem.body_index].textual_representation() | |
187 | message = str.format( | |
188 | '{text_colour}{model_name}:{line_number}: {problem_type}: {message}' | |
189 | '{colour_reset}\n\t{ldraw_code}', | |
190 | text_colour = text_colour, | |
191 | model_name = model.name, | |
192 | line_number = problem.line_number, | |
193 | problem_type = problem.severity, | |
194 | message = problem_text(problem, test_suite), | |
195 | colour_reset = use_colors and colorama.Fore.RESET or '', | |
196 | ldraw_code = ldraw_code, | |
197 | ) | |
198 | messages.append(message) | |
199 | return '\n'.join(messages) | |
200 | ||
146 | 201 | def main(): |
202 | args = parse_commandline_arguments() | |
203 | # Make sure that we have at least one library path specified. | |
204 | if not args.library: | |
205 | raise RuntimeError( | |
206 | 'Please specify libraries using the -l / --library switch.\n' | |
207 | 'For example: -l ~/ldraw or --library=~/ldraw\n' | |
208 | 'Multiple --library switches may be used.') | |
209 | # Prepare the list of libraries. This also expands the ~ for the home | |
210 | # directory | |
211 | import os | |
212 | libraries = [Path(os.path.expanduser(library)) for library in args.library] | |
213 | check_library_paths(libraries) | |
214 | load_ldconfig(libraries) | |
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:
122
diff
changeset
|
215 | try: |
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:
122
diff
changeset
|
216 | context = LDrawContext(libraries) |
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:
122
diff
changeset
|
217 | except RuntimeError as error: |
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:
122
diff
changeset
|
218 | print('error:', str(error), file = stderr) |
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:
122
diff
changeset
|
219 | exit(1) |
98 | 220 | if args.color: |
221 | try: | |
222 | import colorama | |
223 | colorama.init() | |
224 | except ImportError: | |
225 | print('Use of --color requires the colorama module, disabling colours', file = stderr) | |
226 | args.color = False | |
92
b8d72909d593
improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents:
85
diff
changeset
|
227 | if args.subfile: |
146 | 228 | # Subfile debug mode: searches for the specified subfile from the LDraw |
229 | # libraries, opens it as if it was referenced by something and prints | |
230 | # out all information that is calculated from this subfile. | |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
231 | import filecache |
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:
122
diff
changeset
|
232 | cache = filecache.SubfileCache(context = context) |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
233 | subfile = cache.prepare_file(args.filename) |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
234 | if not subfile.valid: |
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
235 | print(subfile.problem) |
92
b8d72909d593
improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents:
85
diff
changeset
|
236 | else: |
b8d72909d593
improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents:
85
diff
changeset
|
237 | print('Flat dimensions:', repr(subfile.flatness)) |
85
4438502fd3e0
fixed the use of moved-to-files check not working if there were non-alphanumerics in the filename
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
238 | print('Description:', repr(subfile.description)) |
92
b8d72909d593
improved the mirrored stud check to catch cases where a subfile that contains studs is mirrored
Teemu Piippo <teemu@hecknology.net>
parents:
85
diff
changeset
|
239 | print('Contains studs:', repr(subfile.has_studs)) |
54
0c686d10eb49
added tests for moved-to files and scaling in flat dimensions
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
240 | else: |
94
109fb7cf658f
added a check for non-DOS line endings
Teemu Piippo <teemu@hecknology.net>
parents:
92
diff
changeset
|
241 | with open(args.filename, 'rb') as file: |
149 | 242 | try: |
243 | from os.path import basename | |
244 | model = parse.read_ldraw( | |
245 | file, | |
246 | name = basename(args.filename), | |
247 | context = context) | |
248 | if args.dump: | |
249 | # Dump mode: prints out the structure of the processed LDraw file | |
250 | print('header: ' + type(model.header).__name__) | |
251 | for key in sorted(dir(model.header)): | |
252 | if not key.startswith('__'): | |
253 | print('\t' + key + ': ' + repr(getattr(model.header, key))) | |
254 | for i, entry in enumerate(model.body): | |
255 | if model.header.valid and i == model.header_size: | |
256 | # Mark where the header is considered to end | |
257 | print('--------- End of header') | |
258 | print(entry) | |
259 | elif args.rebuild: | |
260 | # Debug rebuild mode: open the file, parse it and turn it back | |
261 | # into LDraw code and write it into stdout. This is used to ensure | |
262 | # that LDCheck does not miss any information while parsing files. | |
263 | for entry in model.body: | |
264 | print(entry.textual_representation(), end = '\r\n') | |
265 | else: | |
266 | # Regular mode | |
267 | from testsuite import load_tests, check_model | |
268 | # load the test suite | |
269 | # TODO: maybe add some command line argument to filter tests | |
270 | # in case the user wants to run some specific tests only or | |
271 | # possibly leave some test out | |
272 | test_suite = load_tests() | |
273 | # use the test suite to check the model | |
274 | report = check_model(model, test_suite) | |
275 | # print out the report | |
276 | print(format_report( | |
277 | report, | |
278 | model, | |
279 | test_suite, | |
280 | use_colors = args.color | |
281 | )) | |
282 | except FileNotFoundError: | |
283 | print(str.format( | |
284 | 'no such file: {filename!r}', | |
285 | filename = args.filename | |
286 | ), file = stderr) | |
287 | exit(1) | |
146 | 288 | |
289 | if __name__ == '__main__': | |
290 | try: | |
291 | main() | |
292 | except RuntimeError as e: | |
293 | import sys | |
294 | print('error:', str(e), file = sys.stderr) | |
295 | sys.exit(1) |