From edd730dc4f67ed1d1780c475701ad2b4dd40b234 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 6 Nov 2024 18:03:36 +0100 Subject: [PATCH] Finished temporary. --- lib/pp_admintools/common.py | 9 ++++++++- test/test_03_common.py | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/pp_admintools/common.py b/lib/pp_admintools/common.py index 1bb6985..0be4995 100644 --- a/lib/pp_admintools/common.py +++ b/lib/pp_admintools/common.py @@ -52,7 +52,7 @@ RE_DEFAULT_TZ = re.compile(PATTERN_DEFAULT_TZ) RE_TZ = re.compile(r'^\s*(?P[01]\d)(?::?(?P[0-5]\d))?') -RE_SPLIT_WS_BEGIN = re.compile(r'^\s+') +RE_SPLIT_WS_BEGIN = re.compile("^\\s+[^'\"]?") RE_SPLIT_SQ = re.compile(r"\\'") RE_SPLIT_SQ_CHUNK = re.compile(r"^'((?:\\'|[^'])*)'") RE_SPLIT_DQ = re.compile(r'\\"') @@ -186,6 +186,7 @@ def split_parts(text, keep_quotes=False, raise_on_unbalanced=True): last_chunk = '' # clean the line + # LOG.debug(f'Current: {txt!r}') txt = txt.strip() if txt == '': break @@ -193,6 +194,7 @@ def split_parts(text, keep_quotes=False, raise_on_unbalanced=True): # search for a single quoted string at the begin of the line match = RE_SPLIT_SQ_CHUNK.search(txt) if match: + # LOG.debug('Case single quoted.') chunk = match.group(1) chunk = RE_SPLIT_SQ.sub("'", chunk) if keep_quotes: @@ -204,6 +206,7 @@ def split_parts(text, keep_quotes=False, raise_on_unbalanced=True): # search for a double quoted string at the begin of the line match = RE_SPLIT_DQ_CHUNK.search(txt) if match: + # LOG.debug('Case double quoted.') chunk = match.group(1) chunk = RE_SPLIT_DQ.sub('"', chunk) if keep_quotes: @@ -215,6 +218,7 @@ def split_parts(text, keep_quotes=False, raise_on_unbalanced=True): # search for unquoted, whitespace delimited text at the begin of the line match = RE_SPLIT_UQ_CHUNK.search(txt) if match: + # LOG.debug('Case unquoted.') last_chunk += match.group(1) last_chunk = RE_SPLIT_SQ.sub("'", last_chunk) last_chunk = RE_SPLIT_DQ.sub('"', last_chunk) @@ -225,6 +229,9 @@ def split_parts(text, keep_quotes=False, raise_on_unbalanced=True): if RE_SPLIT_WS_ALL.search(txt): break + # if txt: + # LOG.debug(f'There is still something left: {txt!r}') + # Check for unbalanced quotes match = RE_SPLIT_UNBALANCED.search(txt) if match: diff --git a/test/test_03_common.py b/test/test_03_common.py index 8c49ee0..bae5ecd 100755 --- a/test/test_03_common.py +++ b/test/test_03_common.py @@ -24,7 +24,6 @@ libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'lib')) sys.path.insert(0, libdir) # from fb_tools.common import pp, to_str, is_sequence -from fb_tools.common import pp from general import PpAdminToolsTestcase, get_arg_verbose, init_root_logger @@ -63,7 +62,7 @@ class TestCommon(PpAdminToolsTestcase): """Test splitting strings in chunks by function split_parts().""" LOG.info(self.get_method_doc()) - from pp_admintools.errors import BrokenStringSplit + # from pp_admintools.errors import BrokenStringSplit from pp_admintools.common import split_parts test_data = ( @@ -100,11 +99,11 @@ class TestCommon(PpAdminToolsTestcase): 'quotes': False, 'tgt': ['Frank"s', 'Test'], }, { - 'src': 'Frank\\\'s Test', + 'src': "Frank\\'s Test", 'quotes': False, 'tgt': ["Frank's", 'Test'], }, { - 'src': 'Frank\\\'s Test Frank\\\'s', + 'src': "Frank\\'s Test Frank\\'s", 'quotes': False, 'tgt': ["Frank's", 'Test', "Frank's"], }, @@ -118,6 +117,16 @@ class TestCommon(PpAdminToolsTestcase): LOG.debug('Result: {!r}'.format(res)) self.assertEqual(data['tgt'], res) + # unbalanced = 'Franks "Test' + # if self.verbose >= 1: + # print() + # LOG.debug(f'Test raising an exception on unbalanced quotes in {unbalanced!r}.') + # with self.assertRaises(BrokenStringSplit) as cm: + # res = split_parts(unbalanced, raise_on_unbalanced=True) + # LOG.error('This result should never be visible: {!r}.'.format(res)) + # e = cm.exception + # LOG.debug('{c} raised: {e}'.format(c=e.__class__.__name__, e=e)) + # ============================================================================= if __name__ == '__main__': -- 2.39.5