forked from danigm/gitlab-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
78 lines (58 loc) · 1.89 KB
/
util.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
#!/usr/bin/env python
from hashlib import sha256
import secrets
import string
import time
from flask import json
def digest(string):
return sha256(string.encode()).hexdigest()
# shortcut to json.dumps with friendlier formatting for lists and timestamps
def dumpjson(o):
def fmt(o):
return {k: (v if k != 'refresh' else 'in ' + tdif(v))
for k, v in o.items()}
if type(o) is list:
return ('(no entries)' if not o
else '\n'.join([str(i) + ': ' + json.dumps(fmt(x), indent=2)
for i, x in enumerate(o)]))
return json.dumps(o, indent=2)
# generate a secret
def new_secret(length):
charset = string.ascii_letters + string.digits
while True:
s = ''.join(secrets.choice(charset) for _ in range(length))
if (any(char.islower() for char in s)
and any(char.isupper() for char in s)
and sum(char.isdecimal() for char in s) >= 3):
return s
# string to range
def strange(s):
r = []
try:
for x in s.split(','):
*xs, = x.split('-')
xs = list(sorted(map(int, xs)))
r.extend(list(range(xs[0], xs[-1] + 1, 1)))
except:
return None
return sorted(list(set(r)))
# human-friendly timestamp difference
def tdif(t1, t2=None):
[t1, t2] = sorted(map(int, [t1, t2 or time.time()]))
d, h = divmod(t2 - t1, 86400)
h, m = divmod(h, 3600)
m, s = divmod(m, 60)
s, _ = divmod(s, 1)
t = [[k, v] for k, v
in zip(['day', 'hour', 'minute', 'second'], [d, h, m, s])
if v]
def cont(i):
if not i:
return ''
return ', ' if i < len(t) - 1 else ' and '
r = ''
for i, [k, v] in enumerate(t):
r += '{0}{1} {2}{3}'.format(cont(i), v, k, '' if v == 1 else 's')
return r
def timestamp(lifetime):
return int(time.time()) + lifetime * 60