Commit b39d0d02 authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

update shared module

parent 78ddb207
......@@ -22,6 +22,7 @@ import os
import sys
import base64
import platform
import shlex
def _debug(s):
print("%s"%s, file=sys.stderr)
......@@ -151,7 +152,15 @@ class ConfSimple(object):
for nm,value in mp.items():
f.write(nm + b'=' + value + b'\n')
f.close()
os.rename(tname, self.confname)
try:
# os.replace works on Windows even if dst exists, but py3 only
os.replace(tname, self.confname)
except:
try:
os.rename(tname, self.confname)
except:
import shutil
shutil.move(tname, self.confname)
def setbin(self, nm, value, sk = b''):
if self.readonly:
......@@ -251,16 +260,26 @@ class ConfStack(object):
v = v.decode('utf-8')
return v
def stringToStrings(s):
'''Parse a string made of space-separated words and C-Style strings
(double-quoted with backslash escape). E.g.:
word1 word2 "compound \\"quoted\\" string" ->
['word1', 'word2', 'compound "quoted string']'''
import shlex
# Split string of strings, with possible quoting and escaping.
# The default is do do Recoll stringToStrings emulation: whitespace
# separated, and doublequotes only (C-style). E.G.:
# word1 word2 "compound \\"quoted\\" string" ->
# ['word1', 'word2', 'compound "quoted string']
#
# 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.quotes = '"'
lex.escape = '\\'
lex.escapedquotes = '"'
lex.whitespace_split = True
if quotes is not None:
lex.quotes = quotes
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 = []
while True:
tok = lex.get_token()
......@@ -268,3 +287,14 @@ def stringToStrings(s):
break
l.append(tok)
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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment