RE_TZ = re.compile(r'^\s*(?P<tz_hours>[01]\d)(?::?(?P<tz_mins>[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'\\"')
last_chunk = ''
# clean the line
+ # LOG.debug(f'Current: {txt!r}')
txt = txt.strip()
if txt == '':
break
# 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:
# 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:
# 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)
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:
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
"""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 = (
'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"],
},
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__':