Back to index

plone3  3.1.7
tools.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 uid's. Notice, this doctest is disabled!
00013 
00014     Automatic semi-random uid
00015     >> g = UIDGenerator()
00016     >> uid = g.uid()
00017     >> uid.ical()
00018     '20050109T153222-7ekDDHKcw46QlwZK@example.com'
00019 
00020     You Should at least insert your own hostname to be more complient
00021     >> g = UIDGenerator()
00022     >> uid = g.uid('Example.ORG')
00023     >> uid.ical()
00024     '20050109T153549-NbUItOPDjQj8Ux6q@Example.ORG'
00025 
00026     You can also insert a path or similar
00027     >> g = UIDGenerator()
00028     >> uid = g.uid('Example.ORG', '/path/to/content')
00029     >> uid.ical()
00030     '20050109T153415-/path/to/content@Example.ORG'
00031     """
00032 
00033     chars = list(ascii_letters + digits)
00034 
00035     def rnd_string(self, length=16):
00036         "Generates a string with random characters of length"
00037         return ''.join([random.choice(self.chars) for i in range(length)])
00038 
00039     def uid(self, host_name='example.com', unique=''):
00040         """
00041         Generates a unique id consisting of:
00042         datetime-uniquevalue@host. Like:
00043         20050105T225746Z-HKtJMqUgdO0jDUwm@example.com
00044         """
00045         from PropertyValues import vText, vDatetime
00046         unique = unique or self.rnd_string()
00047         return vText('%s-%s@%s' % (vDatetime.today().ical(), unique, host_name))
00048 
00049 
00050 if __name__ == "__main__":
00051     import os.path, doctest, tools
00052     # import and test this file
00053     doctest.testmod(tools)