shared conftree.py: improved stringToStrings

This commit is contained in:
Jean-Francois Dockes 2019-05-02 10:31:19 +02:00
parent 88c9ff1935
commit c0b014b955

View File

@ -22,6 +22,7 @@ import os
import sys import sys
import base64 import base64
import platform import platform
import shlex
def _debug(s): def _debug(s):
print("%s"%s, file=sys.stderr) print("%s"%s, file=sys.stderr)
@ -251,16 +252,25 @@ class ConfStack(object):
v = v.decode('utf-8') v = v.decode('utf-8')
return v return v
def stringToStrings(s): # Split string of strings, with possible quoting and escaping.
'''Parse a string made of space-separated words and C-Style strings # The default is do do Recoll stringToStrings emulation: whitespace
(double-quoted with backslash escape). E.g.: # separated, and doublequotes only (C-style). E.G.:
word1 word2 "compound \\"quoted\\" string" -> # word1 word2 "compound \\"quoted\\" string" ->
['word1', 'word2', 'compound "quoted string']''' # ['word1', 'word2', 'compound "quoted string']
import shlex #
# This is not the shlex default and can be changed by setting the
# parameters
def stringToStrings(s, quotes = '"', escape = '\\', escapedquotes = '"',
whitespace = None):
lex = shlex.shlex(s, posix=True) lex = shlex.shlex(s, posix=True)
lex.quotes = '"' if quotes is not None:
lex.escape = '\\' lex.quotes = quotes
lex.escapedquotes = '"' if escape is not None:
lex.escape = escape
if escapedquotes is not None:
lex.escapedquotes = escapedquotes
if whitespace is not None:
lex.whitespace = whitespace
l = [] l = []
while True: while True:
tok = lex.get_token() tok = lex.get_token()
@ -268,3 +278,14 @@ def stringToStrings(s):
break break
l.append(tok) l.append(tok)
return l return l
def stringsToString(vs):
out = []
for s in vs:
if s.find(" ") != -1 or s.find("\t") != -1 or s.find("\\") != -1 or \
s.find('"') != -1:
out.append('"' + s.replace('\\', '\\\\').replace('"', '\\"') + '"')
else:
out.append(s)
return " ".join(out)