Attachment 'AlertDate.py'

Download

   1 # format python
   2 # -*- coding: iso-8859-1 -*-
   3 r"""
   4     MoinMoin - AlertDate Macro
   5 
   6     This very complicated macro takes in a date
   7     and produces a label automatically based on
   8     how soon that date will occur. 
   9     
  10     Depending  on when  it is,  by  default the
  11     following labels are used:
  12         Next Week (or Weekend)
  13         This Week (or Weekend)
  14         Tomorrow! or,
  15         Today!
  16     
  17 
  18     Also, a  label for  things further than two
  19     weeks into the future,  or past  events can
  20     be  configured.  Images could just  as well
  21     be inserted.
  22     
  23     Configure  the labels in moin_config.py  by
  24     adding: 
  25          alert_date_format = ""    # format str
  26          alert_labels = {}      # dict defining
  27                                 # the following
  28                                 # labels:
  29            "today", "tomorrow",
  30            "thisweek", "thisweekend",
  31            "nextweek", "nextweekend",
  32            "future", "passed"
  33 
  34     default usage is [[AlertDate(mm/dd/yyyy)]]
  35 
  36     @copyright: 2004 by Arel Cordero <arel@cs.berkeley.edu>
  37     @license: GNU GPL, see COPYING for details.
  38 """
  39 
  40 import time
  41 import moin_config
  42 
  43 Dependencies = []
  44 
  45 # default variables. These can be overwritten in moin_config.py
  46 class glob:
  47     alert_date_fmt = "%m/%d/%Y"      # see time module for expl.
  48     alert_labels = {                 # could just as well insert icons...
  49           "nextweek":   '(Next Week)',
  50           "nextweekend":'(Next Weekend)',
  51           "thisweek":   '(This Week)',
  52           "thisweekend":'(This Weekend)',
  53           "tomorrow":   '(Tomorrow!)',
  54           "today":      '(<b>Today!</b>)',
  55           "passed":     '',
  56           "future":     ''}
  57 
  58 # converts time tuple t into:
  59 #    d     number of days since epoch.
  60 #    wd    day of the week (with monday being 0)
  61 def indays(t):
  62     a = list(t)
  63     a[3:6] = [0, 0, 0]
  64     d = int(time.mktime(a) / 86400.0) # divide by sec./day
  65     wd = t[6]                         # day of week, 0 = monday
  66     return (d,wd)
  67 
  68 # Expects a single string in args that contains due date
  69 # formatted to configured format. Default is mm/dd/yyyy 
  70 def execute(macro, args):
  71     
  72     # Check and import config variables
  73     if hasattr(moin_config, "alert_date_config"):
  74         glob.alert_date_config = moin_config.alert_date_config
  75     if hasattr(moin_config, "alert_labels"):
  76         glob.alert_labels = moin_config.alert_labels
  77 
  78     # Attempt to parse time according to format
  79     try:
  80 
  81        (due,   d_dow) = indays( time.strptime(args, glob.alert_date_fmt) )
  82        (local, l_dow) = indays( time.localtime() )
  83        delta          = due - local
  84        monday         = due - d_dow
  85 
  86        if   delta <  0: return glob.alert_labels.get("passed",   "")
  87        elif delta == 0: return glob.alert_labels.get("today",    "")
  88        elif delta == 1: return glob.alert_labels.get("tomorrow", "")
  89        elif local >= monday:
  90           if d_dow in [5, 6]: return glob.alert_labels.get("thisweekend", "")
  91           else:               return glob.alert_labels.get("thisweek",    "")
  92        elif local >= monday - 7: 
  93           if d_dow in [5, 6]: return glob.alert_labels.get("nextweekend", "")
  94           else:               return glob.alert_labels.get("nextweek",    "")
  95        else: return glob.alert_labels.get("future", "")
  96 
  97     except:
  98        # Format was not understood.
  99        if args :
 100           return   """<p> [ <u>Error:</u> The date <%s> could not be parsed.
 101                       It should look like: AlertDate(%s) ] </p>""" % (
 102                       args, time.strftime(glob.alert_date_fmt))
 103        else:
 104           return   """<p> [ <u>Usage:</u>
 105                       <b>AlertDate(<i>some_future_date</i>)</b><br> Where
 106                       <i>some_future_date</i> takes the format: (%s).<br>
 107                       For example, AlertDate(%s) ] </p>""" % (
 108                       glob.alert_date_fmt,
 109                       time.strftime(glob.alert_date_fmt))

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] (2007-12-22 21:20:46, 4.1 KB) [[attachment:AlertDate-1.6.py]]
  • [get | view] (2004-10-29 07:10:53, 3.8 KB) [[attachment:AlertDate.py]]
 All files | Selected Files: delete move to page copy to page

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