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