icalendar/src/icalendar/tools.py

34 wiersze
1004 B
Python
Czysty Zwykły widok Historia

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
2005-03-23 13:49:29 +00:00
class UIDGenerator:
2013-03-18 12:11:53 +00:00
"""If you are too lazy to create real uid's.
2013-03-18 12:11:53 +00:00
"""
2005-03-23 13:49:29 +00:00
chars = list(ascii_letters + digits)
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())
return vText(f'{today}-{unique}@{host_name}')