forked from alexksikes/evernote-classify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
executable file
·84 lines (67 loc) · 1.93 KB
/
tools.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
# -*- coding: utf-8 -*-
import out
from log import logging
import sys
import shlex
import time
def checkIsInt(value):
try:
int(value)
return True
except ValueError:
return False
def getch():
"""
Interrupting program until pressed any key
"""
try:
import msvcrt
return msvcrt.getch()
except ImportError:
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def strip(data):
if not data:
return data
if isinstance(data, dict):
return dict([ [key.strip(' \t\n\r\"\''), val ] for key, val in data.iteritems() ])
if isinstance(data, list):
return map(lambda val: val.strip(' \t\n\r\"\''), data)
if isinstance(data, str):
return data.strip(' \t\n\r\"\'')
raise Exception("Unexpected args type: %s. Expect list or dict" % type(data))
class ExitException(Exception):
pass
def exit(message='exit'):
out.preloader.exit()
time.sleep(0.33)
raise ExitException(message)
def KeyboardInterruptSignalHendler(signal, frame):
exit()
class Struct:
def __init__(self, **entries):
self.__dict__.update(entries)
def decodeArgs(args):
return map(lambda val: stdinEncode(val), args)
def stdoutEncode(data):
try:
return data.decode("utf8").encode(sys.stdout.encoding)
except:
return data
def stdinEncode(data):
try:
return data.decode(sys.stdin.encoding).encode("utf8")
except:
return data
def to_unicode(s):
if isinstance(s, (unicode, type(None))):
return s
assert isinstance(s, str)
return s.decode("utf8")