Use the new attrs API on SQLCommand classes
authorKarl O. Pinc <kop@karlpinc.com>
Sat, 2 Mar 2024 22:53:37 +0000 (16:53 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Sat, 2 Mar 2024 22:53:37 +0000 (16:53 -0600)
src/pgwui_core/core.py

index 3f6c91aece0e42d3181144822c7dd5b559ff4abe..206ecc232959d2e8b16b2cca9fef216bbe83d860 100644 (file)
@@ -42,6 +42,7 @@ from csv import reader as csv_reader
 import collections.abc
 import ast
 import attr
+import attrs
 import markupsafe
 import hashlib
 
@@ -652,26 +653,21 @@ def format_exception(ex):
 
 # Upload processing
 
-class SQLCommand(object):
+@attrs.define(slots=False)
+class SQLCommand():
     '''
     An SQL command that returns nothing
 
     Attributes:
       stmt  The statement, formatted for psycopg3 substitution
       args  Tuple of arguments used to substitute when executed.
+      ec(ex) Produces the exception to raise an instance of on failure
+              Input:
+                ex  The exception raised by psycopg3
     '''
-    def __init__(self, stmt, args, ec=None):
-        '''
-        stmt   The statement, formatted for psycopg3 substitution
-        args   Tuple of arguments used to substitute when executed.
-        ec(ex) Produces the exception to raise an instance of on failure
-                Input:
-                  ex  The exception raised by psycopg3
-        '''
-        super(SQLCommand, self).__init__()
-        self.stmt = stmt
-        self.args = args
-        self.ec = ec
+    stmt = attrs.field()
+    args = attrs.field()
+    ec = attrs.field(default=None)
 
     def execute(self, cur):
         '''
@@ -693,20 +689,25 @@ class SQLCommand(object):
                 raise self.ec(ex)
 
 
+@attrs.define(slots=False)
 class LogSQLCommand(SQLCommand):
-    '''An SQL command that logs success or failure.'''
-    def __init__(self, stmt, args, ec=None,
-                 log_success=None, log_failure=None):
-        '''
-        stmt  The statement, formatted for psycopg3 substitution
-        args  Tuple of arguments used to substitute when executed.
-        ec(ex) Produces the exception to raise an instance of on failure
-                Input:
-                  ex  The exception raised by psycopg3
-        '''
-        super(LogSQLCommand, self).__init__(stmt, args, ec)
-        self.log_success = log_success
-        self.log_failure = log_failure
+    '''An SQL command that logs success or failure.
+
+    Attributes:
+      stmt  The statement, formatted for psycopg3 substitution
+      args  Tuple of arguments used to substitute when executed.
+      ec(ex) Produces the exception to raise an instance of on failure
+              Input:
+                ex  The exception raised by psycopg3
+      log_success
+             Logs success
+      log_failure(ex)
+             Logs failure
+              Input:
+                ex  The exception to log
+    '''
+    log_success = attrs.field(default=None)
+    log_failure = attrs.field(default=None)
 
     def execute(self, cur):
         '''
@@ -720,7 +721,7 @@ class LogSQLCommand(SQLCommand):
           Can raise a psycopg3 error
         '''
         try:
-            super(LogSQLCommand, self).execute(cur)
+            super().execute(cur)
         except (core_ex.UploadError, psycopg.DatabaseError) as ex:
             if self.log_failure:
                 self.log_failure(ex)