repos
/
irgramd
/ annotate_shade
telegram, irc: Set topic in IRC with Telegram channel/chat description
Annotate for file utils.py
2022-02-20 E.
1
# irgramd: IRC-Telegram gateway
01:25:27 '
2
# utils.py: Helper functions
'
3
#
'
4
# Copyright (c) 2019 Peter Bui <pbui@bx612.space>
2024-04-27 E.
5
# Copyright (c) 2020-2024 E. Bosch <presidev@AT@gmail.com>
2022-02-20 E.
6
#
01:25:27 '
7
# Use of this source code is governed by a MIT style license that
'
8
# can be found in the LICENSE file included in this project.
2020-11-19 E.
9
19:41:24 '
10
import itertools
2022-01-24 E.
11
import textwrap
2022-01-30 E.
12
import re
2022-03-19 E.
13
import datetime
2023-05-06 E.
14
import zoneinfo
2023-04-15 E.
15
import difflib
2023-12-20 E.
16
import logging
2020-11-19 E.
17
2022-02-09 E.
18
# Constants
21:58:31 '
19
2023-12-17 E.
20
FILENAME_INVALID_CHARS = re.compile('[/{}<>()"\'\\|&#%?]')
2022-02-19 E.
21
SIMPLE_URL = re.compile('http(|s)://[^ ]+')
2022-02-09 E.
22
2024-11-02 E.
23
from include import MAX_LINE
19:20:45 '
24
2020-11-19 E.
25
# Utilities
19:41:24 '
26
2023-06-22 E.
27
class command:
19:57:16 '
28
async def parse_command(self, line, nick):
2023-06-25 E.
29
command = line.partition(' ')[0].lower()
2023-06-22 E.
30
self.tmp_ircnick = nick
19:57:16 '
31
if command in self.commands.keys():
2023-06-25 E.
32
handler, min_args, max_args, maxsplit = self.commands[command]
22:17:22 '
33
words = line.split(maxsplit=maxsplit)[1:]
2023-06-22 E.
34
num_words = len(words)
19:57:16 '
35
if num_words < min_args or num_words > max_args:
'
36
reply = ('Wrong number of arguments',)
'
37
else:
'
38
reply = await handler(*words)
'
39
else:
'
40
reply = ('Unknown command',)
'
41
'
42
return reply
'
43
2023-06-25 E.
44
class HELP:
22:17:22 '
45
desc = 1
'
46
brief = 2
'
47
2024-08-30 E.
48
class LOGL:
17:00:53 '
49
debug = False
'
50
2020-11-19 E.
51
def chunks(iterable, n, fillvalue=None):
19:41:24 '
52
''' Return iterable consisting of a sequence of n-length chunks '''
'
53
args = [iter(iterable)] * n
'
54
return itertools.zip_longest(*args, fillvalue=fillvalue)
2021-02-21 E.
55
00:12:58 '
56
def set_replace(set, item, new_item):
'
57
if item in set:
'
58
set.remove(item)
'
59
set.add(new_item)
2022-01-24 E.
60
21:35:55 '
61
def get_continued(items, mark, length):
'
62
# Add "continued" mark to lines, except last one
'
63
return (x + mark if n != length else x for n, x in enumerate(items, start=1))
'
64
'
65
def split_lines(message):
'
66
messages_limited = []
2024-11-02 E.
67
wr = textwrap.TextWrapper(width=MAX_LINE)
2022-01-24 E.
68
21:35:55 '
69
# Split when Telegram original message has breaks
'
70
messages = message.splitlines()
'
71
lm = len(messages)
'
72
if lm > 1:
'
73
# Add "continued line" mark (\) for lines that belong to the same message
'
74
# (split previously)
'
75
messages = get_continued(messages, ' \\', lm)
'
76
for m in messages:
'
77
wrapped = wr.wrap(text=m)
'
78
lw = len(wrapped)
'
79
if lw > 1:
'
80
# Add double "continued line" mark (\\) for lines that belong to the same message
'
81
# and have been wrapped to not exceed IRC limits
'
82
messages_limited += get_continued(wrapped, ' \\\\', lw)
'
83
else:
'
84
messages_limited += wrapped
'
85
del wr
'
86
return messages_limited
2022-01-30 E.
87
00:29:08 '
88
def sanitize_filename(fn):
2023-12-18 E.
89
cn = str(sanitize_filename.cn)
20:18:42 '
90
new_fn, ns = FILENAME_INVALID_CHARS.subn(cn, fn)
'
91
if ns:
'
92
sanitize_filename.cn += 1
'
93
return new_fn.strip('-').replace(' ','_')
'
94
sanitize_filename.cn = 0
2022-02-08 E.
95
2023-12-17 E.
96
def add_filename(filename, add):
01:49:18 '
97
if add:
'
98
aux = filename.rsplit('.', 1)
'
99
name = aux[0]
2024-04-27 E.
100
try:
22:28:22 '
101
ext = aux[1]
'
102
except:
'
103
ext = ''
2023-12-17 E.
104
return '{}-{}.{}'.format(name, add, ext)
01:49:18 '
105
else:
'
106
return filename
'
107
2022-02-08 E.
108
def remove_slash(url):
01:04:55 '
109
return url[:-1] if url[-1:] == '/' else url
'
110
'
111
def remove_http_s(url):
'
112
if url[:8] == 'https://':
'
113
surl = url[8:]
'
114
elif url[:7] == 'http://':
'
115
surl = url[7:]
'
116
else:
'
117
surl = url
'
118
return remove_slash(surl)
2022-02-09 E.
119
2022-02-19 E.
120
def is_url_equiv(url1, url2):
01:10:00 '
121
if url1 and url2:
'
122
return url1 == url2 or remove_slash(remove_http_s(url1)) == remove_slash(remove_http_s(url2))
'
123
else:
'
124
return False
'
125
'
126
def extract_url(text):
'
127
url = SIMPLE_URL.search(text)
'
128
return url.group() if url else None
'
129
2022-02-09 E.
130
def get_human_size(size):
22:52:06 '
131
human_units = ('', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
'
132
'
133
def get_human_size_values(size, unit_pos=0):
'
134
aux = size / 1024.0
'
135
if aux > 1: return get_human_size_values(aux, unit_pos + 1)
'
136
else: return size, human_units[unit_pos]
'
137
'
138
if size <= 1237940039285380274899124224: # 1024Y
'
139
num, unit = get_human_size_values(size)
'
140
else:
'
141
num = size / 1208925819614629174706176 # 1Y
'
142
unit = 'Y'
'
143
'
144
fs = '{:.1f}{}' if num < 10 else '{:.0f}{}'
'
145
'
146
return fs.format(num, unit)
'
147
'
148
def get_human_duration(duration):
'
149
res = ''
'
150
x, s = divmod(duration, 60)
'
151
h, m = divmod(x, 60)
'
152
'
153
if h > 0: res = str(h) + 'h'
'
154
if m > 0: res += str(m) + 'm'
2023-12-14 E.
155
if s > 0 or duration < 60: res += str(s) + 's'
2022-02-09 E.
156
return res
2022-03-19 E.
157
2023-05-07 E.
158
def compact_date(date, tz):
2024-08-29 E.
159
delta = current_date() - date
2023-05-07 E.
160
date_local = date.astimezone(zoneinfo.ZoneInfo(tz))
2022-03-19 E.
161
22:26:56 '
162
if delta.days < 1:
2023-05-07 E.
163
compact_date = date_local.strftime('%H:%M')
2023-03-19 E.
164
elif delta.days < 365:
2023-05-07 E.
165
compact_date = date_local.strftime('%d-%b')
2022-03-19 E.
166
else:
2023-05-07 E.
167
compact_date = date_local.strftime('%Y')
2022-03-19 E.
168
22:26:56 '
169
return compact_date
2023-04-15 E.
170
2024-08-29 E.
171
def current_date():
23:58:06 '
172
return datetime.datetime.now(datetime.timezone.utc)
'
173
2023-04-15 E.
174
def get_highlighted(a, b):
23:13:07 '
175
awl = len(a.split())
'
176
bwl = len(b.split())
'
177
delta_size = abs(awl - bwl)
'
178
highlighted = True
'
179
'
180
if not a:
'
181
res = '> {}'.format(b)
'
182
elif delta_size > 5:
'
183
res = b
'
184
highlighted = False
'
185
else:
'
186
al = a.split(' ')
'
187
bl = b.split(' ')
'
188
diff = difflib.ndiff(al, bl)
'
189
ld = list(diff)
'
190
res = ''
'
191
d = ''
'
192
eq = 0
'
193
'
194
for i in ld:
'
195
if i == '- ' or i[0] == '?':
'
196
continue
'
197
elif i == ' ' or i == '+ ':
'
198
res += ' '
'
199
continue
2023-05-07 E.
200
# deletion of words
2023-04-15 E.
201
elif i[0] == '-':
2023-05-07 E.
202
res += '-{}- '.format(i[2:])
00:24:08 '
203
# addition of words
2023-04-15 E.
204
elif i[0] == '+':
2023-11-18 E.
205
res += '+{}+ '.format(i[2:])
2023-04-15 E.
206
else:
23:13:07 '
207
res += '{} '.format(i[2:])
'
208
eq += 1
'
209
'
210
delta_eq = bwl - eq
'
211
if delta_eq > 3:
'
212
res = b
'
213
highlighted = False
'
214
'
215
return res, highlighted
2023-04-26 E.
216
18:45:17 '
217
def fix_braces(text):
'
218
# Remove braces not closed, if the text was truncated
'
219
if text.endswith(' {...'):
'
220
subtext = text[:-5]
'
221
if not '{}' in subtext:
'
222
return '{}...'.format(subtext)
'
223
return text
2023-05-06 E.
224
23:31:34 '
225
def format_timestamp(format, tz, date):
'
226
date_local = date.astimezone(zoneinfo.ZoneInfo(tz))
'
227
return date_local.strftime(format)
2023-12-20 E.
228
00:50:56 '
229
def parse_loglevel(level):
'
230
levelu = level.upper()
2024-08-30 E.
231
if levelu == 'DEBUG':
17:00:53 '
232
LOGL.debug = True
2023-12-20 E.
233
if levelu == 'NONE':
00:50:56 '
234
l = None
'
235
elif levelu in ('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'):
'
236
l = getattr(logging, levelu)
'
237
else:
'
238
l = False
'
239
return l
2024-08-24 E.
240
23:21:01 '
241
def pretty(object):
2024-08-30 E.
242
return object.stringify() if LOGL.debug and object else object