Back to index

plone3  3.1.7
util.py
Go to the documentation of this file.
00001 from string import ascii_letters, digits
00002 import random
00003 
00004 """
00005 This module contains non-essential tools for iCalendar. Pretty thin so far eh?
00006 
00007 """
00008 
00009 class UIDGenerator:
00010 
00011     """
00012     If you are too lazy to create real uids.
00013 
00014     NOTE: this doctest is disabled
00015     (only two > instead of three)
00016 
00017     Automatic semi-random uid
00018     >> g = UIDGenerator()
00019     >> uid = g.uid()
00020     >> uid.ical()
00021     '20050109T153222-7ekDDHKcw46QlwZK@example.com'
00022 
00023     You should at least insert your own hostname to be more compliant
00024     >> g = UIDGenerator()
00025     >> uid = g.uid('Example.ORG')
00026     >> uid.ical()
00027     '20050109T153549-NbUItOPDjQj8Ux6q@Example.ORG'
00028 
00029     You can also insert a path or similar
00030     >> g = UIDGenerator()
00031     >> uid = g.uid('Example.ORG', '/path/to/content')
00032     >> uid.ical()
00033     '20050109T153415-/path/to/content@Example.ORG'
00034     """
00035 
00036     chars = list(ascii_letters + digits)
00037 
00038     def rnd_string(self, length=16):
00039         "Generates a string with random characters of length"
00040         return ''.join([random.choice(self.chars) for i in range(length)])
00041 
00042     def uid(self, host_name='example.com', unique=''):
00043         """
00044         Generates a unique id consisting of:
00045         datetime-uniquevalue@host. Like:
00046         20050105T225746Z-HKtJMqUgdO0jDUwm@example.com
00047         """
00048         from PropertyValues import vText, vDatetime
00049         unique = unique or self.rnd_string()
00050         return vText('%s-%s@%s' % (vDatetime.today().ical(), unique, host_name))