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