ldcheck.py

Thu, 26 Aug 2021 22:04:33 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 26 Aug 2021 22:04:33 +0300
changeset 152
5e347a96869a
parent 150
fcc07f6907a8
permissions
-rwxr-xr-x

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
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
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
24 def check_library_paths(library_paths):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
27 raise RuntimeError(str.format(
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
32 raise RuntimeError(str.format(
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
37 def find_ldconfig_ldr_paths(libraries):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
38 for library_path in libraries:
17
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
39 yield from [
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
40 library_path / path
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
41 for path in ['LDConfig.ldr', 'ldconfig.ldr']
327da5d00360 Added code to run the test suite.
Santeri Piippo
parents: 13
diff changeset
42 if (library_path / path).is_file()
8
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
43 ]
303c51137cb2 Added ldconfig.ldr support
Santeri Piippo
parents: 7
diff changeset
44
146
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
45 def load_ldconfig(libraries):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
46 ldconfig_ldr_paths = list(find_ldconfig_ldr_paths(libraries))
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
47 if not ldconfig_ldr_paths:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
48 raise RuntimeError('could not find any LDConfig.ldr')
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
49 for ldconfig_ldr_path in ldconfig_ldr_paths:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
50 with ldconfig_ldr_path.open() as ldconfig_ldr:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
118 import os
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
119 rcpath = Path(os.path.expanduser('~/.config/ldcheckrc'))
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
120 if rcpath.exists():
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
127 import argparse
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
128 class ListProblemTypesAction(argparse.Action):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
129 def __init__(self, option_strings, dest, nargs = None, **kwargs):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
130 super().__init__(option_strings, dest, nargs = 0, **kwargs)
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
131 def __call__(self, *args, **kwargs):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
132 import testsuite
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
133 test_suite = testsuite.load_tests()
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
134 for warning_type in testsuite.all_problem_types(test_suite):
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
135 print(str.format('{name}: {severity}: "{message}"',
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
136 name = warning_type.name,
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
137 severity = warning_type.severity,
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
138 message = warning_type.placeholder_message(),
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
139 ))
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
140 sys.exit(0)
149
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
141 parser = argparse.ArgumentParser()
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
142 parser.add_argument('filename')
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
143 parser.add_argument('-l', '--library', action = 'append')
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
144 parser.add_argument('--list',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
145 action = ListProblemTypesAction,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
146 help = 'lists all possible problem types and exit',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
147 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
148 parser.add_argument('--dump',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
149 action = 'store_true',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
150 help = 'dumps the internal parsed structure of the part file',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
151 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
152 parser.add_argument('--rebuild',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
153 action = 'store_true',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
154 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
155 'testing whether the program interprets part files correctly',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
156 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
157 parser.add_argument('--subfile',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
158 action = 'store_true',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
159 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
160 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
161 parser.add_argument('--color',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
162 action = 'store_true',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
163 help = 'use colors'
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
164 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
165 parser.add_argument('-d', '--ldraw-dir',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
166 nargs = '+',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
167 help = 'specify LDraw directory path(s)',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
168 )
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
169 parser.add_argument('-v', '--version',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
170 action = 'version',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
171 version = str.format('{appname} {version}',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
172 appname = appname,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
173 version = version_string,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
174 ),
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
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
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
178 def format_report(report, model, test_suite, *, use_colors = True):
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
179 from testsuite import problem_text
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
180 messages = []
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
181 for problem in report['problems']:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
182 text_colour = ''
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
183 if use_colors:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
184 if problem.severity == 'hold':
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
185 text_colour = colorama.Fore.LIGHTRED_EX
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
186 elif problem.severity == 'warning':
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
187 text_colour = colorama.Fore.LIGHTBLUE_EX
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
188 ldraw_code = model.body[problem.body_index].textual_representation()
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
189 message = str.format(
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
190 '{text_colour}{model_name}:{line_number}: {problem_type}: {message}'
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
191 '{colour_reset}\n\t{ldraw_code}',
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
192 text_colour = text_colour,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
193 model_name = model.name,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
194 line_number = problem.line_number,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
195 problem_type = problem.severity,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
196 message = problem_text(problem, test_suite),
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
197 colour_reset = use_colors and colorama.Fore.RESET or '',
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
198 ldraw_code = ldraw_code,
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
199 )
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
200 messages.append(message)
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
201 return '\n'.join(messages)
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
207 def main():
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
208 args = parse_commandline_arguments()
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
209 # Make sure that we have at least one library path specified.
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
210 if not args.library:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
211 raise RuntimeError(
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
212 'Please specify libraries using the -l / --library switch.\n'
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
213 'For example: -l ~/ldraw or --library=~/ldraw\n'
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
214 'Multiple --library switches may be used.')
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
215 # 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
216 # directory
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
219 check_library_paths(libraries)
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
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
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
226 if args.color:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
227 try:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
228 import colorama
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
229 colorama.init()
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
230 except ImportError:
f9d4e59392f7 made colors toggleable
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
231 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
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
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
234 # Subfile debug mode: searches for the specified subfile from the LDraw
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
235 # 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
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
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
248 try:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
249 from os.path import basename
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
250 model = parse.read_ldraw(
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
251 file,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
252 name = basename(args.filename),
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
253 context = context)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
254 if args.dump:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
255 # Dump mode: prints out the structure of the processed LDraw file
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
256 print('header: ' + type(model.header).__name__)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
257 for key in sorted(dir(model.header)):
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
258 if not key.startswith('__'):
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
259 print('\t' + key + ': ' + repr(getattr(model.header, key)))
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
260 for i, entry in enumerate(model.body):
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
261 if model.header.valid and i == model.header_size:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
262 # Mark where the header is considered to end
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
263 print('--------- End of header')
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
264 print(entry)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
265 elif args.rebuild:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
266 # 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
267 # 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
268 # that LDCheck does not miss any information while parsing files.
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
269 for entry in model.body:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
270 print(entry.textual_representation(), end = '\r\n')
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
271 else:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
272 # Regular mode
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
273 from testsuite import load_tests, check_model
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
274 # load the test suite
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
275 # TODO: maybe add some command line argument to filter tests
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
276 # 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
277 # possibly leave some test out
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
278 test_suite = load_tests()
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
279 # use the test suite to check the model
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
280 report = check_model(model, test_suite)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
281 # print out the report
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
282 print(format_report(
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
283 report,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
284 model,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
285 test_suite,
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
286 use_colors = args.color
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
287 ))
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
288 except FileNotFoundError:
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
289 print(str.format(
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
290 'no such file: {filename!r}',
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
291 filename = args.filename
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
292 ), file = stderr)
7c01f9876b69 Fix merge issues
Teemu Piippo <teemu@hecknology.net>
parents: 147
diff changeset
293 exit(1)
146
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
294
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
295 if __name__ == '__main__':
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
296 try:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
297 main()
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
298 except RuntimeError as e:
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
299 import sys
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
300 print('error:', str(e), file = sys.stderr)
3555679d276b Cleanup ldcheck.py
Teemu Piippo <teemu@hecknology.net>
parents: 94
diff changeset
301 sys.exit(1)

mercurial