forked from ekonda/sketal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
149 lines (109 loc) · 4.19 KB
/
utils.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Various helpers
import html
import urllib
from typing import Callable, List
import asyncio
import collections
import hues
def schedule_coroutine(target):
"""Schedules target coroutine in the given event loop
If not given, *loop* defaults to the current thread's event loop
Returns the scheduled task.
"""
if asyncio.iscoroutine(target):
return asyncio.ensure_future(target, loop=asyncio.get_event_loop())
else:
raise TypeError("target must be a coroutine, "
"not {!r}".format(type(target)))
# http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
class Attachment(object):
__slots__ = ('type', 'owner_id', 'id', 'access_key', 'link')
def __init__(self, attach_type: str, owner_id: int, aid: int, access_key: str, link: str):
self.type = attach_type
self.owner_id = owner_id
self.id = aid
self.access_key = access_key
self.link = link
def as_str(self):
"""Возвращает приложение в формате ownerid_id_accesskey"""
if self.access_key:
return f'{self.owner_id}_{self.id}_{self.access_key}'
return f'{self.owner_id}_{self.id}'
def __repr__(self):
return f'{self.type}{self.as_str()}'
class MessageEventData(object):
__slots__ = ('conf', 'peer_id', 'user_id', 'body', 'time', "msg_id", "attaches")
def __init__(self, conf: bool, pid: int, uid: int, body: str, time: int, msg_id: int, attaches: List):
self.conf = conf
self.peer_id = pid
self.user_id = uid
self.body = body
self.time = time
self.msg_id = msg_id
self.attaches = attaches
def __repr__(self):
return self.body
def fatal(*args):
"""Отправляет args в hues.error() и выходит"""
hues.error(*args)
exit()
# Characters are taken from http://gsgen.ru/tools/perevod-raskladki-online/
ENGLISH = "Q-W-E-R-T-Y-U-I-O-P-A-S-D-F-G-H-J-K-L-Z-X-C-V-B-N-M"
ENG_EXPR = ENGLISH + ENGLISH.lower() + "-" + ":-^-~-`-{-[-}-]-\"-'-<-,->-.-;-?-/-&-@-#-$"
RUS_EXPR = "Й-Ц-У-К-Е-Н-Г-Ш-Щ-З-Ф-Ы-В-А-П-Р-О-Л-Д-Я-Ч-С-М-И-Т-Ь"
rus_expr = RUS_EXPR + RUS_EXPR.lower() + "-" + "Ж-:-Ё-ё-Х-х-Ъ-ъ-Э-э-Б-б-Ю-ю-ж-,-.-?-\"-№-;"
ENG_TO_RUS = str.maketrans(ENG_EXPR, rus_expr)
RUS_TO_ENG = str.maketrans(rus_expr, ENG_EXPR)
def convert_to_rus(text: str) -> str:
"""Конвертировать текст, написанный на русском с английской раскладкой в русский"""
return text.translate(ENG_TO_RUS)
def convert_to_en(text: str) -> str:
"""Конвертировать текст, написанный на русском с русской раскладкой в английскую раскладку"""
return text.translate(RUS_TO_ENG)
keys = [
'unread',
'outbox',
'replied',
'important',
'chat',
'friends',
'spam',
'deleted',
'fixed',
'media'
]
def parse_msg_flags(bitmask: int) -> dict:
"""Функция для чтения битовой маски и возврата словаря значений"""
start = 1
values = []
for x in range(1, 11):
result = bitmask & start
start *= 2
values.append(bool(result))
return dict(zip(keys, values))
def unquote(data):
temp = data
if issubclass(temp.__class__, str):
return html.unescape(html.unescape(temp))
if issubclass(temp.__class__, dict):
for k, v in temp.items():
temp[k] = unquote(v)
if issubclass(temp.__class__, list):
for i in range(len(temp)):
temp[i] = unquote(temp[i])
return temp
def quote(data):
temp = data
if issubclass(temp.__class__, str):
return urllib.parse.quote(urllib.parse.quote(temp))
if issubclass(temp.__class__, dict):
for k, v in temp.items():
data[k] = quote(v)
if issubclass(temp.__class__, list):
for i in range(len(temp)):
temp[i] = quote(temp[i])
return temp