ldcheck.py

changeset 98
f9d4e59392f7
parent 94
109fb7cf658f
child 100
62759e5c4554
equal deleted inserted replaced
97:7b24ff111cb6 98:f9d4e59392f7
78 severity = warning_type.severity, 78 severity = warning_type.severity,
79 message = warning_type.placeholder_message(), 79 message = warning_type.placeholder_message(),
80 )) 80 ))
81 exit(0) 81 exit(0)
82 82
83 def format_report(report, model, test_suite, *, use_colors = True):
84 from testsuite import problem_text
85 messages = []
86 for problem in report['problems']:
87 text_colour = ''
88 if use_colors:
89 if problem.severity == 'hold':
90 text_colour = colorama.Fore.LIGHTRED_EX
91 elif problem.severity == 'warning':
92 text_colour = colorama.Fore.LIGHTBLUE_EX
93 ldraw_code = model.body[problem.body_index].textual_representation()
94 message = str.format(
95 '{text_colour}{model_name}:{line_number}: {problem_type}: {message}'
96 '{colour_reset}\n\t{ldraw_code}',
97 text_colour = text_colour,
98 model_name = model.name,
99 line_number = problem.line_number,
100 problem_type = problem.severity,
101 message = problem_text(problem, test_suite),
102 colour_reset = use_colors and colorama.Fore.RESET or '',
103 ldraw_code = ldraw_code,
104 )
105 messages.append(message)
106 return '\n'.join(messages)
107
83 if __name__ == '__main__': 108 if __name__ == '__main__':
84 from sys import argv 109 from sys import argv
85 parser = argparse.ArgumentParser() 110 parser = argparse.ArgumentParser()
86 parser.add_argument('filename') 111 parser.add_argument('filename')
87 parser.add_argument('--list', 112 parser.add_argument('--list',
99 ) 124 )
100 parser.add_argument('--subfile', 125 parser.add_argument('--subfile',
101 action = 'store_true', 126 action = 'store_true',
102 help = 'finds a subfile by name and prints out information about it' 127 help = 'finds a subfile by name and prints out information about it'
103 ) 128 )
129 parser.add_argument('--color',
130 action = 'store_true',
131 help = 'use colors'
132 )
104 args = parser.parse_args() 133 args = parser.parse_args()
105 config = load_config('ldcheck.cfg') 134 config = load_config('ldcheck.cfg')
106 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config): 135 for ldconfig_ldr_path in find_ldconfig_ldr_paths(config):
107 with ldconfig_ldr_path.open() as ldconfig_ldr: 136 with ldconfig_ldr_path.open() as ldconfig_ldr:
108 load_colours(ldconfig_ldr) 137 load_colours(ldconfig_ldr)
138 if args.color:
139 try:
140 import colorama
141 colorama.init()
142 except ImportError:
143 from sys import stderr
144 print('Use of --color requires the colorama module, disabling colours', file = stderr)
145 args.color = False
109 if args.subfile: 146 if args.subfile:
110 import filecache 147 import filecache
111 cache = filecache.SubfileCache( 148 cache = filecache.SubfileCache(
112 ldraw_directories = config['libraries'], 149 ldraw_directories = config['libraries'],
113 ) 150 )
136 print(entry) 173 print(entry)
137 elif args.rebuild: 174 elif args.rebuild:
138 for entry in model.body: 175 for entry in model.body:
139 print(entry.textual_representation(), end = '\r\n') 176 print(entry.textual_representation(), end = '\r\n')
140 else: 177 else:
141 from testsuite import load_tests, check_model, format_report 178 from testsuite import load_tests, check_model
142 test_suite = load_tests() 179 test_suite = load_tests()
143 report = check_model(model, test_suite) 180 report = check_model(model, test_suite)
144 print(format_report(report, model, test_suite)) 181 print(format_report(report, model, test_suite, use_colors = args.color))

mercurial