ソースを参照

Give a neater error message for FileNotFoundError.
Rerarrange read/write to open the image file before switching on drive motor.

Keir Fraser 4 年 前
コミット
65941efc32

+ 13 - 10
scripts/greaseweazle/tools/read.py

@@ -10,20 +10,22 @@
 import sys, argparse
 
 from greaseweazle.tools import util
+from greaseweazle import error
 from greaseweazle import usb as USB
 
-# read_to_image:
-# Reads a floppy disk and dumps it into a new image file.
-def read_to_image(usb, args):
 
+def open_image(args):
     image_class = util.get_image_class(args.file)
-    if not image_class:
-        return
-    if not hasattr(image_class, 'to_file'):
-        print("%s: Cannot create %s image files"
-              % (args.file, image_class.__name__))
-        return
+    error.check(hasattr(image_class, 'to_file'),
+                "%s: Cannot create %s image files"
+                % (args.file, image_class.__name__))
     image = image_class.to_file(args.scyl, args.nr_sides)
+    return image
+
+
+# read_to_image:
+# Reads a floppy disk and dumps it into a new image file.
+def read_to_image(usb, args, image):
 
     for cyl in range(args.scyl, args.ecyl+1):
         for side in range(0, args.nr_sides):
@@ -63,7 +65,8 @@ def main(argv):
 
     try:
         usb = util.usb_open(args.device)
-        util.with_drive_selected(read_to_image, usb, args)
+        image = open_image(args)
+        util.with_drive_selected(read_to_image, usb, args, image)
     except USB.CmdError as error:
         print("Command Failed: %s" % error)
 

+ 5 - 5
scripts/greaseweazle/tools/util.py

@@ -11,6 +11,7 @@ import argparse, os, sys, serial, struct, time
 import serial.tools.list_ports
 
 from greaseweazle import version
+from greaseweazle import error
 from greaseweazle import usb as USB
 from greaseweazle.image.scp import SCP
 from greaseweazle.image.hfe import HFE
@@ -33,18 +34,17 @@ def drive_letter(letter):
 def get_image_class(name):
     image_types = { '.scp': SCP, '.hfe': HFE, '.ipf': IPF }
     _, ext = os.path.splitext(name)
-    if not ext.lower() in image_types:
-        print("%s: Unrecognised file suffix '%s'" % (name, ext))
-        return None
+    error.check(ext.lower() in image_types,
+                "%s: Unrecognised file suffix '%s'" % (name, ext))
     return image_types[ext.lower()]
 
 
-def with_drive_selected(fn, usb, args):
+def with_drive_selected(fn, usb, args, *_args, **_kwargs):
     usb.set_bus_type(args.drive[0])
     try:
         usb.drive_select(args.drive[1])
         usb.drive_motor(args.drive[1], True)
-        fn(usb, args)
+        fn(usb, args, *_args, **_kwargs)
     except KeyboardInterrupt:
         print()
         usb.reset()

+ 15 - 12
scripts/greaseweazle/tools/write.py

@@ -12,9 +12,21 @@ import sys, argparse
 from greaseweazle.tools import util
 from greaseweazle import usb as USB
 
+
+# Read and parse the image file.
+def open_image(args):
+    image_class = util.get_image_class(args.file)
+    if hasattr(image_class, 'from_filename'):
+        image = image_class.from_filename(args.file)
+    else:
+        with open(args.file, "rb") as f:
+            image = image_class.from_file(f.read())
+    return image
+
+
 # write_from_image:
 # Writes the specified image file to floppy disk.
-def write_from_image(usb, args):
+def write_from_image(usb, args, image):
 
     # @drive_ticks is the time in Greaseweazle ticks between index pulses.
     # We will adjust the flux intervals per track to allow for this.
@@ -22,16 +34,6 @@ def write_from_image(usb, args):
     drive_ticks = (flux.index_list[0] + flux.index_list[1]) / 2
     del flux
 
-    # Read and parse the image file.
-    image_class = util.get_image_class(args.file)
-    if not image_class:
-        return
-    if hasattr(image_class, 'from_filename'):
-        image = image_class.from_filename(args.file)
-    else:
-        with open(args.file, "rb") as f:
-            image = image_class.from_file(f.read())
-
     for cyl in range(args.scyl, args.ecyl+1):
         for side in range(0, args.nr_sides):
 
@@ -83,7 +85,8 @@ def main(argv):
 
     try:
         usb = util.usb_open(args.device)
-        util.with_drive_selected(write_from_image, usb, args)
+        image = open_image(args)
+        util.with_drive_selected(write_from_image, usb, args, image)
     except USB.CmdError as error:
         print("Command Failed: %s" % error)
 

+ 1 - 1
scripts/gw.py

@@ -61,7 +61,7 @@ try:
     res = main(argv)
     if res is None:
         res = 0
-except error.Fatal as err:
+except (error.Fatal, FileNotFoundError) as err:
     print("** FATAL ERROR:")
     print(err)
     res = 1