ldcheck.py

changeset 150
fcc07f6907a8
parent 149
7c01f9876b69
equal deleted inserted replaced
149:7c01f9876b69 150:fcc07f6907a8
15 from geometry import * 15 from geometry import *
16 from pathlib import Path 16 from pathlib import Path
17 import linetypes 17 import linetypes
18 import header 18 import header
19 import parse 19 import parse
20
21 from os.path import realpath
22 script_directory = Path(realpath(__file__)).parent
20 23
21 def check_library_paths(library_paths): 24 def check_library_paths(library_paths):
22 for library_path in library_paths: 25 for library_path in library_paths:
23 if not library_path.exists(): 26 if not library_path.exists():
24 raise RuntimeError(str.format( 27 raise RuntimeError(str.format(
50 class LDrawContext: 53 class LDrawContext:
51 ''' 54 '''
52 Contains context-dependant LDraw information, like library directory 55 Contains context-dependant LDraw information, like library directory
53 paths and the colour table. 56 paths and the colour table.
54 ''' 57 '''
55 def __init__(self, libraries = None): 58 def __init__(self, libraries):
56 self._libraries = libraries 59 self._libraries = libraries
57 if not self._libraries:
58 self._libraries = ldraw_dirs_from_config()
59 self.ldconfig_colour_data = self.load_ldconfig_ldr() 60 self.ldconfig_colour_data = self.load_ldconfig_ldr()
60 self.check_library_paths() 61 self.check_library_paths()
61 @property 62 @property
62 def libraries(self): 63 def libraries(self):
63 return self._libraries 64 return self._libraries
111 else: 112 else:
112 return '#000000' 113 return '#000000'
113 def is_valid_colour(self, colour): 114 def is_valid_colour(self, colour):
114 return self.is_ldconfig_colour(colour) or colour.is_direct_colour 115 return self.is_ldconfig_colour(colour) or colour.is_direct_colour
115 116
116 def parse_commandline_arguments(): 117 def load_rcfile():
117 import os 118 import os
118 rcpath = Path(os.path.expanduser('~/.config/ldcheckrc')) 119 rcpath = Path(os.path.expanduser('~/.config/ldcheckrc'))
119 if rcpath.exists(): 120 if rcpath.exists():
120 with rcpath.open() as file: 121 with rcpath.open() as file:
121 rcargs = ['--' + line.strip() for line in file] 122 return ['--' + line.strip() for line in file]
122 else: 123 else:
123 rcargs = [] 124 return []
125
126 def parse_commandline_arguments():
124 import argparse 127 import argparse
125 class ListProblemTypesAction(argparse.Action): 128 class ListProblemTypesAction(argparse.Action):
126 def __init__(self, option_strings, dest, nargs = None, **kwargs): 129 def __init__(self, option_strings, dest, nargs = None, **kwargs):
127 super().__init__(option_strings, dest, nargs = 0, **kwargs) 130 super().__init__(option_strings, dest, nargs = 0, **kwargs)
128 def __call__(self, *args, **kwargs): 131 def __call__(self, *args, **kwargs):
168 version = str.format('{appname} {version}', 171 version = str.format('{appname} {version}',
169 appname = appname, 172 appname = appname,
170 version = version_string, 173 version = version_string,
171 ), 174 ),
172 ) 175 )
173 arglist = rcargs + sys.argv[1:] 176 return parser.parse_args(load_rcfile() + sys.argv[1:])
174 return parser.parse_args(arglist)
175 177
176 def format_report(report, model, test_suite, *, use_colors = True): 178 def format_report(report, model, test_suite, *, use_colors = True):
177 from testsuite import problem_text 179 from testsuite import problem_text
178 messages = [] 180 messages = []
179 for problem in report['problems']: 181 for problem in report['problems']:
196 ldraw_code = ldraw_code, 198 ldraw_code = ldraw_code,
197 ) 199 )
198 messages.append(message) 200 messages.append(message)
199 return '\n'.join(messages) 201 return '\n'.join(messages)
200 202
203 def postprocess_library_paths(libraries_strings):
204 import os
205 return [Path(os.path.expanduser(library)) for library in libraries_strings]
206
201 def main(): 207 def main():
202 args = parse_commandline_arguments() 208 args = parse_commandline_arguments()
203 # Make sure that we have at least one library path specified. 209 # Make sure that we have at least one library path specified.
204 if not args.library: 210 if not args.library:
205 raise RuntimeError( 211 raise RuntimeError(
207 'For example: -l ~/ldraw or --library=~/ldraw\n' 213 'For example: -l ~/ldraw or --library=~/ldraw\n'
208 'Multiple --library switches may be used.') 214 'Multiple --library switches may be used.')
209 # Prepare the list of libraries. This also expands the ~ for the home 215 # Prepare the list of libraries. This also expands the ~ for the home
210 # directory 216 # directory
211 import os 217 import os
212 libraries = [Path(os.path.expanduser(library)) for library in args.library] 218 libraries = postprocess_library_paths(args.library)
213 check_library_paths(libraries) 219 check_library_paths(libraries)
214 load_ldconfig(libraries) 220 load_ldconfig(libraries)
215 try: 221 try:
216 context = LDrawContext(libraries) 222 context = LDrawContext(libraries)
217 except RuntimeError as error: 223 except RuntimeError as error:

mercurial