Move core utility functions into a separate module
authorKarl O. Pinc <kop@karlpinc.com>
Tue, 16 Jul 2024 22:08:36 +0000 (17:08 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Tue, 16 Jul 2024 22:08:36 +0000 (17:08 -0500)
src/pgwui_core/core.py
src/pgwui_core/utils.py [new file with mode: 0644]

index 8d189166d27938b4cd648700d57d9138753a80b3..b2f720a37802a0fa8049d33d0009cedd16171c9d 100644 (file)
@@ -25,6 +25,7 @@
 More general description of the module.
 '''
 
+
 # There are main objects, and their subclasses, here:
 #   LoadedForm
 #   DBHandler  (generally referred to a an "upload handler", at present)
@@ -652,49 +653,6 @@ class UploadDoubleTableForm(UploadDoubleFileFormMixin, UploadTableForm):
         return super().write_response(response)
 
 
-# Utility functions
-
-def textualize(st):
-    '''
-    Return pg representation of NULL for None when string st is None.
-    '''
-    return 'NULL' if st is None else st
-
-
-def is_checked(val):
-    '''Is the value something a html input entity recognizes as checked?'''
-    return val == CHECKED
-
-
-# Some functions for logging
-
-def escape_eol(string):
-    '''Change all the newlines to \n.'''
-    return string.replace('\n', r'\n')
-
-
-def format_exception(ex):
-    '''Return an exception formatted as suffix text for a log message.'''
-    if isinstance(ex, psycopg.errors.DatabaseError):
-        diag = ex.diag
-        msg = diag.message_primary
-        if hasattr(diag, 'message_detail'):
-            msg += ', detail={0}'.format(escape_eol(diag.message_detail))
-        if hasattr(diag, 'message_hint'):
-            msg += ', hint={0}'.format(escape_eol(diag.message_hint))
-    elif isinstance(ex, core_ex.UploadError):
-        msg = ex.e
-        if ex.descr != '':
-            msg += ' {0}'.format(escape_eol(ex.descr))
-        if ex.detail != '':
-            msg += ' {0}'.format(escape_eol(ex.detail))
-    else:
-        msg = ''
-    if msg != '':
-        msg = ': Error is ({0})'.format(msg)
-    return msg
-
-
 # Upload processing
 
 @attrs.define(slots=False)
diff --git a/src/pgwui_core/utils.py b/src/pgwui_core/utils.py
new file mode 100644 (file)
index 0000000..50405d9
--- /dev/null
@@ -0,0 +1,70 @@
+# Copyright (C) 2013, 2014, 2015, 2018, 2020, 2021, 2024 The Meme Factory, Inc.
+#               http://www.karlpinc.com/
+
+# This file is part of PGWUI_Core.
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU Affero General Public License
+# as published by the Free Software Foundation, either version 3 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program.  If not, see
+# <http://www.gnu.org/licenses/>.
+#
+
+# Karl O. Pinc <kop@karlpinc.com>
+
+'''Utility functions
+'''
+from . import exceptions as core_ex
+import pgwui_core.constants
+import psycopg.errors
+
+
+# Utility functions
+
+def textualize(st):
+    '''
+    Return pg representation of NULL for None when string st is None.
+    '''
+    return 'NULL' if st is None else st
+
+
+def is_checked(val):
+    '''Is the value something a html input entity recognizes as checked?'''
+    return val == pgwui_core.constants.CHECKED
+
+
+# Some functions for logging
+
+def escape_eol(string):
+    '''Change all the newlines to \n.'''
+    return string.replace('\n', r'\n')
+
+
+def format_exception(ex):
+    '''Return an exception formatted as suffix text for a log message.'''
+    if isinstance(ex, psycopg.errors.DatabaseError):
+        diag = ex.diag
+        msg = diag.message_primary
+        if hasattr(diag, 'message_detail'):
+            msg += ', detail={0}'.format(escape_eol(diag.message_detail))
+        if hasattr(diag, 'message_hint'):
+            msg += ', hint={0}'.format(escape_eol(diag.message_hint))
+    elif isinstance(ex, core_ex.UploadError):
+        msg = ex.e
+        if ex.descr != '':
+            msg += ' {0}'.format(escape_eol(ex.descr))
+        if ex.detail != '':
+            msg += ' {0}'.format(escape_eol(ex.detail))
+    else:
+        msg = ''
+    if msg != '':
+        msg = ': Error is ({0})'.format(msg)
+    return msg