2012-01-03 14:32:40 +00:00
|
|
|
from datetime import datetime
|
2013-12-24 00:44:53 +00:00
|
|
|
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
|
2005-03-23 13:49:29 +00:00
|
|
|
|
|
|
|
|
2022-08-15 07:55:50 +00:00
|
|
|
class UIDGenerator:
|
2013-03-18 12:11:53 +00:00
|
|
|
"""If you are too lazy to create real uid's.
|
2005-11-07 21:07:56 +00:00
|
|
|
|
2013-03-18 12:11:53 +00:00
|
|
|
"""
|
2005-03-23 13:49:29 +00:00
|
|
|
chars = list(ascii_letters + digits)
|
2005-11-07 21:07:56 +00:00
|
|
|
|
2022-01-15 19:57:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def rnd_string(length=16):
|
2013-03-15 15:34:42 +00:00
|
|
|
"""Generates a string with random characters of length.
|
|
|
|
"""
|
2022-01-15 19:57:37 +00:00
|
|
|
return ''.join([random.choice(UIDGenerator.chars) for _ in range(length)])
|
2005-03-23 13:49:29 +00:00
|
|
|
|
2022-01-15 19:57:37 +00:00
|
|
|
@staticmethod
|
|
|
|
def uid(host_name='example.com', unique=''):
|
2013-03-15 15:34:42 +00:00
|
|
|
"""Generates a unique id consisting of:
|
|
|
|
datetime-uniquevalue@host.
|
|
|
|
Like:
|
|
|
|
20050105T225746Z-HKtJMqUgdO0jDUwm@example.com
|
2005-03-23 13:49:29 +00:00
|
|
|
"""
|
2013-10-24 22:01:45 +00:00
|
|
|
host_name = to_unicode(host_name)
|
2022-01-15 19:57:37 +00:00
|
|
|
unique = unique or UIDGenerator.rnd_string()
|
2013-10-24 22:01:45 +00:00
|
|
|
today = to_unicode(vDatetime(datetime.today()).to_ical())
|
2022-10-31 17:51:52 +00:00
|
|
|
return vText(f'{today}-{unique}@{host_name}')
|