handle file not found more cleanly

Tue, 25 Aug 2020 23:37:53 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 25 Aug 2020 23:37:53 +0300
changeset 109
b627f8963a84
parent 108
09ffafb154a8
child 110
ea148f6fbd01

handle file not found more cleanly

ldcheck.py file | annotate | diff | comparison | revisions
--- a/ldcheck.py	Tue Aug 25 23:35:48 2020 +0300
+++ b/ldcheck.py	Tue Aug 25 23:37:53 2020 +0300
@@ -110,7 +110,7 @@
     return '\n'.join(messages)
 
 if __name__ == '__main__':
-    from sys import argv
+    from sys import argv, stderr, exit
     parser = argparse.ArgumentParser()
     parser.add_argument('filename')
     parser.add_argument('--list',
@@ -151,7 +151,6 @@
             import colorama
             colorama.init()
         except ImportError:
-            from sys import stderr
             print('Use of --color requires the colorama module, disabling colours', file = stderr)
             args.color = False
     if args.subfile:
@@ -167,26 +166,38 @@
             print('Description:', repr(subfile.description))
             print('Contains studs:', repr(subfile.has_studs))
     else:
-        with open(args.filename, 'rb') as file:
-            from os.path import basename
-            model = parse.read_ldraw(
-                file,
-                name = basename(args.filename),
-                ldraw_directories = config['libraries'])
-            if args.dump:
-                print('header: ' + type(model.header).__name__)
-                for key in sorted(dir(model.header)):
-                    if not key.startswith('__'):
-                        print('\t' + key + ': ' + repr(getattr(model.header, key)))
-                for i, entry in enumerate(model.body):
-                    if model.header.valid and i == model.header_size:
-                        print('--------- End of header')
-                    print(entry)
-            elif args.rebuild:
-                for entry in model.body:
-                    print(entry.textual_representation(), end = '\r\n')
-            else:
-                from testsuite import load_tests, check_model
-                test_suite = load_tests()
-                report = check_model(model, test_suite)
-                print(format_report(report, model, test_suite, use_colors = args.color))
+        try:
+            with open(args.filename, 'rb') as file:
+                from os.path import basename
+                model = parse.read_ldraw(
+                    file,
+                    name = basename(args.filename),
+                    ldraw_directories = config['libraries'])
+                if args.dump:
+                    print('header: ' + type(model.header).__name__)
+                    for key in sorted(dir(model.header)):
+                        if not key.startswith('__'):
+                            print('\t' + key + ': ' + repr(getattr(model.header, key)))
+                    for i, entry in enumerate(model.body):
+                        if model.header.valid and i == model.header_size:
+                            print('--------- End of header')
+                        print(entry)
+                elif args.rebuild:
+                    for entry in model.body:
+                        print(entry.textual_representation(), end = '\r\n')
+                else:
+                    from testsuite import load_tests, check_model
+                    test_suite = load_tests()
+                    report = check_model(model, test_suite)
+                    print(format_report(
+                        report,
+                        model,
+                        test_suite,
+                        use_colors = args.color
+                    ))
+        except FileNotFoundError:
+            print(str.format(
+                'no such file: {filename!r}',
+                filename = args.filename
+            ), file = stderr)
+            exit(1)

mercurial