Normalize newlines of sql from textarea and for sql output
authorKarl O. Pinc <kop@karlpinc.com>
Sun, 29 Sep 2024 17:09:31 +0000 (12:09 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Sun, 29 Sep 2024 17:09:31 +0000 (12:09 -0500)
src/pgwui_sql/lib.py [new file with mode: 0644]
src/pgwui_sql/templates/sql.mak
src/pgwui_sql/views/base.py
tests/test_lib.py [new file with mode: 0644]

diff --git a/src/pgwui_sql/lib.py b/src/pgwui_sql/lib.py
new file mode 100644 (file)
index 0000000..3d2972d
--- /dev/null
@@ -0,0 +1,26 @@
+# Copyright (C) 2024 The Meme Factory, Inc. http://www.karlpinc.com/
+
+# This file is part of PGWUI_SQL.
+#
+# 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/>.
+#
+
+import io
+
+
+def normalize_newlines(sval):
+    '''Apply the Python io module's univeral newline transalation to a string
+    '''
+    return io.StringIO(sval, newline=None).getvalue()
index c916419ea869192f85084da04bb76d151bda3df6..c3726d59395c077c5f21635f152674e9ce71193b 100644 (file)
@@ -34,6 +34,7 @@
 
 
 <%!
+    import pgwui_sql.lib
     from pgwui_common.path import asset_abspath
 
     sql_base_mak = asset_abspath('pgwui_sql:templates/sql_base.mak')
@@ -83,6 +84,9 @@
       }<em class="success">!</em>
   </p>
   % if sql:
+       <% if upload_sql:
+              sql = pgwui_sql.lib.normalize_newlines(sql)
+       %>
        <ol>
          % for sql_line in sql.rstrip().split('\n'):
            <li>${sql_line}</li>
index 97197bc32299a468895758b009e4fe7b3ff9dd4a..8aa163b1189c909b9cf2d2ad774d6b43e93887f6 100644 (file)
@@ -20,6 +20,7 @@
 import attrs
 import pgwui_core.core
 import pgwui_core.forms
+import pgwui_sql.lib
 import wtforms.fields
 
 from pgwui_core.constants import (
@@ -123,7 +124,7 @@ class SQLBaseForm(pgwui_core.forms.UploadFormBaseMixin,
         elif self._form.sql.data is None:
             self['sql'] = ''
         else:
-            self['sql'] = self._form.sql.data
+            self['sql'] = pgwui_sql.lib.normalize_newlines(self._form.sql.data)
 
         self['null_rep'] = self._form.null_rep.data
         self['download'] = self._form.download.data
diff --git a/tests/test_lib.py b/tests/test_lib.py
new file mode 100644 (file)
index 0000000..955cdfb
--- /dev/null
@@ -0,0 +1,43 @@
+# Copyright (C) 2024 The Meme Factory, Inc.  http://www.karlpinc.com/
+
+# This file is part of PGWUI_SQL.
+#
+# 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>
+
+import pgwui_sql.lib as lib
+import pytest
+
+
+# Activiate the PGWUI pytest plugin
+pytest_plugins = ("pgwui",)
+
+
+# normalize_newlines()
+@pytest.mark.unittest
+@pytest.mark.parametrize(
+    'val,expected',
+    [('this has no newlines', 'this has no newlines'),
+     ('this\nhas\n newlines\n', 'this\nhas\n newlines\n'),
+     ('this\r\nhas\r\n newlines\r\n', 'this\nhas\n newlines\n'),
+     ('this\rhas\r newlines\r', 'this\nhas\n newlines\n')],
+    ids=('no_newlines', 'newlines', 'crlf', 'cr'))
+def test_normalize_newlines(val, expected):
+    '''The expected normalization happens
+    '''
+    result = lib.normalize_newlines(val)
+    assert result == expected