X-Git-Url: http://git.vrable.net/?a=blobdiff_plain;f=python%2Fcumulus%2F__init__.py;h=e1dac844e56677d594e9b58dc02329e66de30196;hb=5f2e50c2ad75043142dc1812fe19dbd7ad86488c;hp=51d3ee82f49ec0e072714d343f1e9b38cd552ac3;hpb=2ee97034047db53780a52d803b1c577b4c23c303;p=cumulus.git diff --git a/python/cumulus/__init__.py b/python/cumulus/__init__.py index 51d3ee8..e1dac84 100644 --- a/python/cumulus/__init__.py +++ b/python/cumulus/__init__.py @@ -23,6 +23,19 @@ MAX_RECURSION_DEPTH = 3 # All segments which have been accessed this session. accessed_segments = set() +def uri_decode(s): + """Decode a URI-encoded (%xx escapes) string.""" + def hex_decode(m): return chr(int(m.group(1), 16)) + return re.sub(r"%([0-9a-f]{2})", hex_decode, s) +def uri_encode(s): + """Encode a string to URI-encoded (%xx escapes) form.""" + def hex_encode(c): + if c > '+' and c < '\x7f' and c != '@': + return c + else: + return "%%%02x" % (ord(c),) + return ''.join(hex_encode(c) for c in s) + class Struct: """A class which merely acts as a data container. @@ -347,8 +360,7 @@ class MetadataItem: @staticmethod def decode_str(s): """Decode a URI-encoded (%xx escapes) string.""" - def hex_decode(m): return chr(int(m.group(1), 16)) - return re.sub(r"%([0-9a-f]{2})", hex_decode, s) + return uri_decode(s) @staticmethod def raw_str(s):