-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescaping_codec.py
88 lines (74 loc) · 2.63 KB
/
escaping_codec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#s.dobrev '2k4
from __future__ import print_function #,unicode_literals
'escape/unescape strings'
class Codec:
_escaper = '\\'
_encoding = {
_escaper: '\\a',
'\n': '\\n',
'\t': '\\t',
'\r': '\\r',
}
def __init__( me, encoding =_encoding, escaper =_escaper ):
assert escaper in encoding
me.escaper = escaper
me.encoding = encoding #dont change it! #or .copy() just in case
import re
rf = '(' + '|'.join( [ re.escape(e) for e in encoding ] ) + ')'
me.rf = re.compile( rf )
me.decoding = dict( (v,k) for k,v in encoding.items())
rb = '(' + '|'.join( [ re.escape(e) for e in me.decoding ] ) + ')'
me.rb = re.compile( rb )
if 0: #using string.replace
def encode( me, s):
' escape chars like \n, | '
escaper = me.escaper
encoding = me.encoding
s = s.replace( escaper, encoding[ escaper ]) #first
for k,v in encoding.items():
if k != escaper:
s = s.replace(k,v)
return s
def decode( me, s):
' unescape back chars like like \n, | '
escaper = me.escaper
encoding = me.encoding
for k,v in encoding.items():
if k != escaper:
s = s.replace(v,k)
s = s.replace( encoding[ escaper ], escaper) #last
return s
else: #using re.sub
#def replacer_f( match): return me.encoding[ match.group(0) ]
#def replacer_b( match): return me.decoding[ match.group(0) ]
#lambda should be faster for big things - no getattr( me.encoding) inside
def encode( me, s): return me.rf.sub( lambda match: me.encoding[ match.group(0) ], s)
def decode( me, s): return me.rb.sub( lambda match: me.decoding[ match.group(0) ], s)
if __name__=='__main__':
escaper = '\\'
encoding = {
escaper: '\\a',
'\n': '\\n',
'\r': '\\r',
#'\032': '\\z', #ctrl-z?
'|' : '\\p',
}
codec = Codec( encoding, escaper)
encode = codec.encode
decode = codec.decode
verbose = 0
import sys
for a in sys.argv[1:]:
e = encode(a)
d = decode(e)
_verbose = verbose or d != a
if _verbose: print( repr(a),)
if _verbose: print( '->', repr(e))
if _verbose: print( repr(d), '<-')
if d != a:
print( '!!!error!')
#enc.py `cat enc.py` - space-delimited words by word
#enc.py "`cat enc.py`" - whole file at once
# vim:ts=4:sw=4:expandtab