From 1570eb9740b0d5a5d6f637ef5ace0eac204bd2ef Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Tue, 16 Jul 2024 17:08:36 -0500 Subject: [PATCH] Move core utility functions into a separate module --- src/pgwui_core/core.py | 44 +------------------------- src/pgwui_core/utils.py | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 43 deletions(-) create mode 100644 src/pgwui_core/utils.py diff --git a/src/pgwui_core/core.py b/src/pgwui_core/core.py index 8d18916..b2f720a 100644 --- a/src/pgwui_core/core.py +++ b/src/pgwui_core/core.py @@ -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 index 0000000..50405d9 --- /dev/null +++ b/src/pgwui_core/utils.py @@ -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 +# . +# + +# Karl O. Pinc + +'''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 -- 2.34.1