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