ldcheck.py

Thu, 26 Aug 2021 19:43:03 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 26 Aug 2021 19:43:03 +0300
changeset 149
7c01f9876b69
parent 147
bec55b021ae7
child 150
fcc07f6907a8
permissions
-rwxr-xr-x

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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
2 import sys
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
3 if sys.version_info < (3, 4):
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
4 raise RuntimeError('Python 3.4 or newer required')
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
5 from colours import load_colours
149
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
6
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
7 try:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
8 import colorama
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
9 except ImportError:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
10 colorama = None
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
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
0ab0d61ccee8 Smallest angles
Santeri Piippo
parents: 6
diff changeset
15 from geometry import *
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
16 from pathlib import Path
13
12d4ddc4bfd8 Got the skew test working
Santeri Piippo
parents: 9
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
21 def check_library_paths(library_paths):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
24 raise RuntimeError(str.format(
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
29 raise RuntimeError(str.format(
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
34 def find_ldconfig_ldr_paths(libraries):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
35 for library_path in libraries:
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
36 yield from [
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
37 library_path / path
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
38 for path in ['LDConfig.ldr', 'ldconfig.ldr']
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
39 if (library_path / path).is_file()
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
40 ]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
41
146
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
42 def load_ldconfig(libraries):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
43 ldconfig_ldr_paths = list(find_ldconfig_ldr_paths(libraries))
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
44 if not ldconfig_ldr_paths:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
45 raise RuntimeError('could not find any LDConfig.ldr')
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
46 for ldconfig_ldr_path in ldconfig_ldr_paths:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
47 with ldconfig_ldr_path.open() as ldconfig_ldr:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
116 def parse_commandline_arguments():
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
117 import os
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
118 rcpath = Path(os.path.expanduser('~/.config/ldcheckrc'))
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
119 if rcpath.exists():
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
120 with rcpath.open() as file:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
121 rcargs = ['--' + line.strip() for line in file]
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
122 else:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
123 rcargs = []
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
124 import argparse
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
125 class ListProblemTypesAction(argparse.Action):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
126 def __init__(self, option_strings, dest, nargs = None, **kwargs):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
127 super().__init__(option_strings, dest, nargs = 0, **kwargs)
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
128 def __call__(self, *args, **kwargs):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
129 import testsuite
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
130 test_suite = testsuite.load_tests()
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
131 for warning_type in testsuite.all_problem_types(test_suite):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
132 print(str.format('{name}: {severity}: "{message}"',
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
133 name = warning_type.name,
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
134 severity = warning_type.severity,
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
135 message = warning_type.placeholder_message(),
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
136 ))
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
137 sys.exit(0)
149
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
138 parser = argparse.ArgumentParser()
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
139 parser.add_argument('filename')
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
140 parser.add_argument('-l', '--library', action = 'append')
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
141 parser.add_argument('--list',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
142 action = ListProblemTypesAction,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
143 help = 'lists all possible problem types and exit',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
144 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
145 parser.add_argument('--dump',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
146 action = 'store_true',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
147 help = 'dumps the internal parsed structure of the part file',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
148 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
149 parser.add_argument('--rebuild',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
150 action = 'store_true',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
151 help = 'parses the part file and prints it back out, used for '
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
152 'testing whether the program interprets part files correctly',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
153 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
154 parser.add_argument('--subfile',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
155 action = 'store_true',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
156 help = 'finds a subfile by name and prints out information about it'
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
157 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
158 parser.add_argument('--color',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
159 action = 'store_true',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
160 help = 'use colors'
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
161 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
162 parser.add_argument('-d', '--ldraw-dir',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
163 nargs = '+',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
164 help = 'specify LDraw directory path(s)',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
165 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
166 parser.add_argument('-v', '--version',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
167 action = 'version',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
168 version = str.format('{appname} {version}',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
169 appname = appname,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
170 version = version_string,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
171 ),
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
172 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
173 arglist = rcargs + sys.argv[1:]
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
174 return parser.parse_args(arglist)
26
7c263b864371 Added command line option to list all checks.
Santeri Piippo
parents: 21
diff changeset
175
98
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
176 def format_report(report, model, test_suite, *, use_colors = True):
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
177 from testsuite import problem_text
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
178 messages = []
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
179 for problem in report['problems']:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
180 text_colour = ''
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
181 if use_colors:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
182 if problem.severity == 'hold':
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
183 text_colour = colorama.Fore.LIGHTRED_EX
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
184 elif problem.severity == 'warning':
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
185 text_colour = colorama.Fore.LIGHTBLUE_EX
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
186 ldraw_code = model.body[problem.body_index].textual_representation()
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
187 message = str.format(
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
188 '{text_colour}{model_name}:{line_number}: {problem_type}: {message}'
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
189 '{colour_reset}\n\t{ldraw_code}',
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
190 text_colour = text_colour,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
191 model_name = model.name,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
192 line_number = problem.line_number,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
193 problem_type = problem.severity,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
194 message = problem_text(problem, test_suite),
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
195 colour_reset = use_colors and colorama.Fore.RESET or '',
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
196 ldraw_code = ldraw_code,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
197 )
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
198 messages.append(message)
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
199 return '\n'.join(messages)
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
200
146
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
201 def main():
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
202 args = parse_commandline_arguments()
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
203 # Make sure that we have at least one library path specified.
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
204 if not args.library:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
205 raise RuntimeError(
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
206 'Please specify libraries using the -l / --library switch.\n'
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
207 'For example: -l ~/ldraw or --library=~/ldraw\n'
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
208 'Multiple --library switches may be used.')
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
209 # Prepare the list of libraries. This also expands the ~ for the home
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
210 # directory
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
211 import os
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
212 libraries = [Path(os.path.expanduser(library)) for library in args.library]
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
213 check_library_paths(libraries)
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
220 if args.color:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
221 try:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
222 import colorama
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
223 colorama.init()
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
224 except ImportError:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
225 print('Use of --color requires the colorama module, disabling colours', file = stderr)
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
228 # Subfile debug mode: searches for the specified subfile from the LDraw
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
229 # libraries, opens it as if it was referenced by something and prints
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
242 try:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
243 from os.path import basename
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
244 model = parse.read_ldraw(
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
245 file,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
246 name = basename(args.filename),
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
247 context = context)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
248 if args.dump:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
249 # Dump mode: prints out the structure of the processed LDraw file
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
250 print('header: ' + type(model.header).__name__)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
251 for key in sorted(dir(model.header)):
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
252 if not key.startswith('__'):
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
253 print('\t' + key + ': ' + repr(getattr(model.header, key)))
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
254 for i, entry in enumerate(model.body):
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
255 if model.header.valid and i == model.header_size:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
256 # Mark where the header is considered to end
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
257 print('--------- End of header')
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
258 print(entry)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
259 elif args.rebuild:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
260 # Debug rebuild mode: open the file, parse it and turn it back
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
261 # into LDraw code and write it into stdout. This is used to ensure
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
262 # that LDCheck does not miss any information while parsing files.
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
263 for entry in model.body:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
264 print(entry.textual_representation(), end = '\r\n')
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
265 else:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
266 # Regular mode
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
267 from testsuite import load_tests, check_model
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
268 # load the test suite
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
269 # TODO: maybe add some command line argument to filter tests
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
270 # in case the user wants to run some specific tests only or
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
271 # possibly leave some test out
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
272 test_suite = load_tests()
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
273 # use the test suite to check the model
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
274 report = check_model(model, test_suite)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
275 # print out the report
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
276 print(format_report(
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
277 report,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
278 model,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
279 test_suite,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
280 use_colors = args.color
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
281 ))
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
282 except FileNotFoundError:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
283 print(str.format(
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
284 'no such file: {filename!r}',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
285 filename = args.filename
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
286 ), file = stderr)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
287 exit(1)
146
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
288
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
289 if __name__ == '__main__':
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
290 try:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
291 main()
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
292 except RuntimeError as e:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
293 import sys
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
294 print('error:', str(e), file = sys.stderr)
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
295 sys.exit(1)

mercurial