forked from skydark/nstools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portable.py
64 lines (53 loc) · 1.69 KB
/
portable.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
# -*- coding: utf-8 -*-
import sys
py = sys.version_info
py3k = py >= (3, 0, 0)
default_encoding = sys.getfilesystemencoding()
if default_encoding.lower() == 'ascii':
default_encoding = 'utf-8'
from codecs import open
if py3k:
is_bytes = lambda s: isinstance(s, bytes)
is_unicode = lambda s: isinstance(s, str)
is_str = lambda s: isinstance(s, (bytes, str))
encode = lambda s, encoding=None: bytes(s, encoding or default_encoding)
unistr = str
bytechr = lambda c: bytes(chr(c), 'charmap')
chr = chr
UEMPTY = ''
b_ord = lambda c: c
chr_join = lambda l: bytes(l)
else:
is_bytes = lambda s: isinstance(s, str)
is_unicode = lambda s: isinstance(s, unicode)
is_str = lambda s: isinstance(s, basestring)
encode = lambda s, encoding=None: s.encode(encoding or default_encoding)
unistr = unicode
bytechr = chr
chr = unichr
UEMPTY = ''.decode('utf-8')
b_ord = lambda c: ord(c)
chr_join = lambda l: b''.join(bytechr(c) for c in l)
decode = lambda s, encoding=None: s.decode(encoding or default_encoding)
to_bytes = lambda s, encoding=None: \
encode(s, encoding) if is_unicode(s) else s
to_unicode = lambda s, encoding=None: \
s if is_unicode(s) else decode(s, encoding)
B = to_bytes
U = lambda s: to_bytes(s).decode('raw_unicode_escape')
if py3k:
from urllib.request import urlopen, Request
else:
from urllib2 import urlopen, Request
if py3k:
from io import BytesIO as StringIO
else:
from cStringIO import StringIO
if py3k:
import tkinter
import tkinter.filedialog as tkFileDialog
import tkinter.messagebox as tkMessageBox
else:
import Tkinter as tkinter
import tkFileDialog
import tkMessageBox