1313from lib .core .settings import UNICODE_ENCODING
1414
1515def base64decode (value ):
16+ """
17+ Decodes string value from Base64 to plain format
18+
19+ >>> base64decode('Zm9vYmFy')
20+ 'foobar'
21+ """
22+
1623 return value .decode ("base64" )
1724
1825def base64encode (value ):
26+ """
27+ Encodes string value from plain to Base64 format
28+
29+ >>> base64encode('foobar')
30+ 'Zm9vYmFy'
31+ """
32+
1933 return value .encode ("base64" )[:- 1 ].replace ("\n " , "" )
2034
2135def base64pickle (value ):
36+ """
37+ Serializes (with pickle) and encodes to Base64 format supplied (binary) value
38+
39+ >>> base64pickle('foobar')
40+ 'gAJVBmZvb2JhcnEALg=='
41+ """
42+
2243 retVal = None
2344 try :
2445 retVal = base64encode (pickle .dumps (value , pickle .HIGHEST_PROTOCOL ))
@@ -31,21 +52,42 @@ def base64pickle(value):
3152 return retVal
3253
3354def base64unpickle (value ):
55+ """
56+ Decodes value from Base64 to plain format and deserializes (with pickle) it's content
57+
58+ >>> base64unpickle('gAJVBmZvb2JhcnEALg==')
59+ 'foobar'
60+ """
61+
3462 return pickle .loads (base64decode (value ))
3563
3664def hexdecode (value ):
65+ """
66+ Decodes string value from hex to plain format
67+
68+ >>> hexdecode('666f6f626172')
69+ 'foobar'
70+ """
71+
3772 value = value .lower ()
3873 return (value [2 :] if value .startswith ("0x" ) else value ).decode ("hex" )
3974
4075def hexencode (value ):
76+ """
77+ Encodes string value from plain to hex format
78+
79+ >>> hexencode('foobar')
80+ '666f6f626172'
81+ """
82+
4183 return utf8encode (value ).encode ("hex" )
4284
4385def unicodeencode (value , encoding = None ):
4486 """
45- Return 8-bit string representation of the supplied unicode value:
87+ Returns 8-bit string representation of the supplied unicode value
4688
47- >>> unicodeencode(u'test ')
48- 'test '
89+ >>> unicodeencode(u'foobar ')
90+ 'foobar '
4991 """
5092
5193 retVal = value
@@ -57,16 +99,33 @@ def unicodeencode(value, encoding=None):
5799 return retVal
58100
59101def utf8encode (value ):
102+ """
103+ Returns 8-bit string representation of the supplied UTF-8 value
104+
105+ >>> utf8encode(u'foobar')
106+ 'foobar'
107+ """
108+
60109 return unicodeencode (value , "utf-8" )
61110
62111def utf8decode (value ):
63- return value .decode ("utf-8" )
112+ """
113+ Returns UTF-8 representation of the supplied 8-bit string representation
64114
65- def htmlescape (value ):
66- codes = (('&' , '&' ), ('<' , '<' ), ('>' , '>' ), ('"' , '"' ), ("'" , ''' ), (' ' , ' ' ))
67- return reduce (lambda x , y : x .replace (y [0 ], y [1 ]), codes , value )
115+ >>> utf8decode('foobar')
116+ u'foobar'
117+ """
118+
119+ return value .decode ("utf-8" )
68120
69121def htmlunescape (value ):
122+ """
123+ Returns (basic conversion) HTML unescaped value
124+
125+ >>> htmlunescape('a<b')
126+ 'a<b'
127+ """
128+
70129 retVal = value
71130 if value and isinstance (value , basestring ):
72131 codes = (('<' , '<' ), ('>' , '>' ), ('"' , '"' ), (' ' , ' ' ), ('&' , '&' ))
@@ -103,7 +162,21 @@ def stdoutencode(data):
103162 return retVal
104163
105164def jsonize (data ):
165+ """
166+ Returns JSON serialized data
167+
168+ >>> jsonize({'foo':'bar'})
169+ '{\\ n "foo": "bar"\\ n}'
170+ """
171+
106172 return json .dumps (data , sort_keys = False , indent = 4 )
107173
108174def dejsonize (data ):
175+ """
176+ Returns JSON deserialized data
177+
178+ >>> dejsonize('{\\ n "foo": "bar"\\ n}')
179+ {u'foo': u'bar'}
180+ """
181+
109182 return json .loads (data )
0 commit comments