Attachment 'CalDate_0.1.py'

Download

   1 # To use this macro, place in a page which is under a calendar:
   2 #   <<CalDate("%d %B %Y")>>
   3 # If it's under a calendar which has the date "2004-10-28",
   4 # it will render as "28 October, 2004".  It also allows you
   5 # to use:
   6 #   <<CalDate("%d %B %Y",7)>>
   7 # This will advance the output by 7 days.  So you get
   8 # "4 November, 2004".  I use this on my chorus rehearsal
   9 # notes page where I want to have a section describing the
  10 # last rehearsal, and then a section describing the upcoming
  11 # rehearsal.
  12 #
  13 # Gary Godfrey
  14 # Austin, TX USA
  15 # 11 April, 2008
  16 # ggodfrey+caldate@io.com
  17 # Version 0.1
  18 
  19 import datetime
  20 import re
  21 Dependencies = []
  22 # Look for:
  23 #   "anything in quotes"
  24 #   "anything in quotes",5
  25 fmtre = re.compile(r'^"([^"]+)"(?:|,\s*(-*\d+))$')
  26 fmtdt = re.compile(r'^(\d+)-(\d+)-(\d+)')
  27 
  28 def execute(macro, args):
  29     # Args: (fmt,[days offset])
  30     #       Fmt is a date/time format (see strftime(3))
  31     #       Days offset is the number of days to offset the calendar day.
  32     args = args.encode()        # Make string instead of unicode
  33     try:
  34         (datefmt,dayOffset) = fmtre.match(args).groups()
  35     except:
  36         return "Badly formatted CalDate options"
  37 
  38     # Get the url from the request object.  Parse the date and alter
  39     # the date based on the args
  40     req = macro.request
  41     try:
  42 	datetext = req.getPathinfo().split('/')[-1]
  43 	(year, month, day) = fmtdt.match(datetext).groups()
  44         caldate = datetime.date(int(year),int(month),int(day))
  45     except:
  46 	return "Can't figure out date from url.  Is this under a calendar?"
  47 
  48     if dayOffset:
  49         caldate = caldate + datetime.timedelta(days=int(dayOffset))
  50 
  51     str = caldate.strftime(datefmt)
  52     try:
  53         str = caldate.strftime(datefmt)
  54     except:
  55         str = "Badly Formated Date"
  56 
  57     return str
  58 
  59 if __name__ == '__main__':
  60     class tst:
  61 	def __init__(self):
  62 	    self.request = self
  63 	def getPathinfo(self):
  64 	    return 'calendarpath/2008-04-24'
  65 
  66     t = tst()
  67     assert execute(t,'"%d %B, %Y"') == '24 April, 2008'
  68     assert execute(t,'"%d %B, %Y",2') == '26 April, 2008'
  69     print "Test done"

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2008-04-12 16:08:08, 2.0 KB) [[attachment:CalDate_0.1.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.