No point in re-testing for open-ness, due to: yield from ...
authorKarl O. Pinc <kop@karlpinc.com>
Sun, 17 Jan 2021 01:25:32 +0000 (19:25 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Mon, 18 Jan 2021 00:52:07 +0000 (18:52 -0600)
src/pgwui_bulk_upload/exceptions.py
src/pgwui_bulk_upload/views/bulk_upload.py

index 3d953bb58311b46022816b65bbd0288f93d93d9a..d7ee25608f18c6f6efe3f082046ba3fe7f7090aa 100644 (file)
 from pgwui_core import exceptions as core_ex
 
 
+# Line-by-line processing errors
+
+class CannotReReadError(core_ex.DataLineError):
+    def __init__(self, filename, lineno, exp):
+        super().__init__(
+            lineno,
+            f'Cannot re-open ({filename}) to read db data',
+            f'The error is: {exp}')
+
+
 # PGWUI setting related exceptions
 
 class Error(core_ex.Error):
index 55ec65a775039f1e2f4de2c42e03a028b07661c2..03a4e8b67f21133599c2550854e3574fd16388b3 100644 (file)
@@ -141,33 +141,28 @@ class UploadBulkData(UploadData):
         trim        (boolean) Trim leading and trailing whitespace?
 
         filepath    Path of file (zip_root relative)
-        reopened    The file has been re-opened after reading the header line
         '''
         super().__init__(fileo, file_fmt, null_data, null_rep, trim=True)
         self.path = path
         self.filepath = archive_path(path)
         self.relation = relation
-        self.reopened = False
 
     def _thunk(self):
         '''Get the thunk which returns the next udl
         '''
+        # Reopen the file, now that it is time to upload it
         try:
-            yield from super()._thunk()
-        except ValueError:
-            # The file isn't open
-            if self.reopened:
-                super()._thunk().close()
-                return            # skip this file
-            # Reopen the file, now that it is time to upload it
-            self.reopened = True
-            try:
-                self.open_fileo(self.path.open('rb'))
-            except OSError as exp:
-                # If the file does not open on the next iteration it is skipped
-                raise ex.CannotReadError(self.filepath, exp)
-            next(super()._thunk())             # skip header
-            yield from super()._thunk()
+            self.open_fileo(self.path.open('rb'))
+        except OSError as exp:
+            saved = exp
+
+            def thunk():
+                raise ex.CannotReReadError(self.filepath, self.lineno, saved)
+            yield thunk
+            self.lineno -= 1
+            return   # shut down generator
+        next(super()._thunk())             # skip header
+        yield from super()._thunk()
 
 
 @attr.s