safe decoding values going into --replicate (as we should have a "replicate" and sqlite3 supports all chars)

This commit is contained in:
Miroslav Stampar
2011-04-14 13:53:56 +00:00
parent 30bfefd638
commit eafab03d99
3 changed files with 35 additions and 1 deletions

View File

@@ -22,6 +22,7 @@ import urllib
from lib.core.data import conf
from lib.core.data import logger
from lib.core.settings import HEX_ENCODED_CHAR_REGEX
from lib.core.settings import UNICODE_ENCODING
from lib.core.settings import URLENCODE_CHAR_LIMIT
from lib.core.settings import URLENCODE_FAILSAFE_CHARS
@@ -145,11 +146,39 @@ def safecharencode(value):
"""
retVal = value
if isinstance(value, basestring):
retVal = reduce(lambda x, y: x + (y if (y in string.printable or ord(y) > 255) else '\%02x' % ord(y)), value, unicode())
for char in "\t\n\r\x0b\x0c":
retVal = retVal.replace(char, repr(char).strip('\''))
elif isinstance(value, list):
for i in xrange(len(value)):
retVal[i] = safecharencode(value[i])
return retVal
def safechardecode(value):
"""
Reverse function to safecharencode
"""
retVal = value
if isinstance(value, basestring):
for char in "\t\n\r\x0b\x0c":
retVal = retVal.replace(repr(char).strip('\''), char)
regex = re.compile(HEX_ENCODED_CHAR_REGEX)
while True:
match = regex.search(retVal)
if match:
retVal = retVal.replace(match.group("result"), unhexlify(value.lstrip('\\')))
else:
break
elif isinstance(value, list):
for i in xrange(len(value)):
retVal[i] = safechardecode(value[i])
return retVal