/
tools.py
33 lines (27 loc) · 1004 Bytes
/
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
from datetime import datetime
from icalendar.parser_tools import to_unicode
from icalendar.prop import vDatetime
from icalendar.prop import vText
from string import ascii_letters
from string import digits
import random
class UIDGenerator:
"""If you are too lazy to create real uid's.
"""
chars = list(ascii_letters + digits)
@staticmethod
def rnd_string(length=16):
"""Generates a string with random characters of length.
"""
return ''.join([random.choice(UIDGenerator.chars) for _ in range(length)])
@staticmethod
def uid(host_name='example.com', unique=''):
"""Generates a unique id consisting of:
datetime-uniquevalue@host.
Like:
20050105T225746Z-HKtJMqUgdO0jDUwm@example.com
"""
host_name = to_unicode(host_name)
unique = unique or UIDGenerator.rnd_string()
today = to_unicode(vDatetime(datetime.today()).to_ical())
return vText(f'{today}-{unique}@{host_name}')