Attachment 'ProgressBar-0.1.1.py'

Download

   1 """
   2 MoinMoin - ProgressBar Macro
   3 Generates a progress bar (in the form of a table)
   4 
   5 @copyright: Pascal Bauermeister <pascal DOT bauermeister AT gmail DOT cm>
   6 @license: GPL
   7 
   8 Updates:
   9 
  10   * [v0.1.1] Sun Dec 18 21:31:17 CET 2005
  11     Changed table cell percentage markup.
  12 
  13   * [v0.1.0] Fri Dec 16 22:30:10 CET 2005
  14     Original version
  15 
  16 ----
  17 
  18 The ProgressBar macro generates a table showing a progress indicator.
  19 
  20 Usage:
  21   [[ ProgressBar ]]
  22   [[ ProgressBar (TABLEWIDTH TABLEFORMAT PROGRESS%) ]]
  23   [[ ProgressBar (TABLEWIDTH TABLEFORMAT CURRENT/STEPS) ]]
  24   [[ ProgressBar (TABLEWIDTH TABLEFORMAT STARTDATE,ENDDATE) ]]  
  25 
  26 If no arguments are given, the usage is inserted in the HTML result.
  27 
  28 Options:
  29 
  30   TABLEWIDTH (optional prefix)
  31     A wiki tablewidth attribute value between []'s
  32     Examples:
  33       [100%]
  34       [80px]
  35 
  36   TABLEFORMAT (optional prefix)
  37     A pair of wiki table attribute, to format the inactive and active cells.
  38     Examples:
  39       <bgcolor="black"><bgcolor="white">                   # black on white bar
  40       <tablewidth="90%" bgcolor="black"><bgcolor="white">  # same, 90% table
  41 
  42     A third format may be given for STARTDATE,ENDDATE usage
  43 
  44     By default: <tablewidth="100px"#808080><><#8080ff>
  45 
  46   PROGRESS
  47     Will display a table with two cells:
  48     - left: completion, taking PROGRESS % of the table width
  49     - right: remaining
  50 
  51   CURRENT/STEPS
  52     Will display a table with STEPS cells, CURRENT of which are active.
  53 
  54   STARTDATE,ENDDATE
  55     Will display a table with the number of days, with the cell
  56     representing today in active format and background in inactive format.
  57 
  58     If today is before STARTDATE, the left-most cell will be in the
  59     3rd format. If today is after ENDDATE the rightmost cell will be
  60     in the 3rd format.
  61 
  62     Dates are in this format: YYYY-MM-DD
  63 
  64 Debugging
  65   Please prepend a '?' to the arguments.
  66 
  67 Examples:
  68   [[ProgressBar(60%)]]
  69   [[ProgressBar(6/10)]]
  70   [[ProgressBar(2005-11-01,2006-01-06)]]
  71 
  72   [[ProgressBar([50%] 60%)]]
  73   [[ProgressBar([50px] 60%)]]
  74   [[ProgressBar([90%]<#8080ff><#808080> 6/10)]]
  75 ----
  76 """
  77 
  78 
  79 # Imports
  80 import time, re, StringIO
  81 from MoinMoin import version
  82 from MoinMoin.parser import wiki
  83 
  84 Dependencies = ["time"] # macro cannot be cached
  85 
  86 
  87 class _Error (Exception):
  88     pass
  89 
  90 
  91 def escape (str):
  92     return str.replace ('&','&amp;').replace ('<', '&lt;').replace ('>', '&gt;')
  93 
  94 def usage (full = False):
  95 
  96     """Returns the interesting part of the module's doc"""
  97 
  98     if full:
  99         return __doc__
 100     else:
 101         rx = re.compile ("--$(.*)^--", re.DOTALL + re.MULTILINE)
 102         return rx.findall (__doc__) [0].strip ()
 103 
 104 
 105 def s2t (s):
 106     return time.mktime (time.strptime (s, "%Y-%m-%d"))
 107 
 108 
 109 def execute (macro, text, args_re=None):
 110 
 111     try:     res = _execute (macro, text)
 112     except Exception, msg:
 113         return """
 114         <p><strong class="error">
 115         Error: macro ProgressBar: %s</strong> </p>
 116         """ % escape ("%s" % msg)
 117     return res
 118 
 119 
 120 def _execute (macro, text):
 121 
 122     fmt = ['#808080','','#8080ff']
 123     width ="100px"
 124     res = ""
 125     text = text.strip ()
 126 
 127     # help if empty text
 128     help = len (text) == 0
 129 
 130     # debug if starts with '?'
 131     if text.startswith ('?'):
 132         debug = True
 133         text = text [1:]
 134     else:
 135         debug = False
 136     orig_text = text
 137 
 138     # Formats
 139     try:
 140         # Table width
 141         if text.startswith ('['):
 142             pos = text.rfind (']')
 143             width = text [1:pos]
 144             text = text [pos+1:].strip ()
 145 
 146         # Cells format
 147         if text.startswith ('<'):
 148             pos = text.rfind ('>')
 149             f = text [1:pos].split ('><')
 150             text = text [pos+1:].strip ()
 151             fmt [:len (f)] = f
 152     except:
 153         help = True
 154 
 155     # Show help
 156     if help:
 157         return """
 158         <p>
 159         <pre>%s</pre></p>
 160         """ % escape (usage (0))
 161 
 162     # Cell formatting utility
 163     def cell (txt, fmt):
 164         if len (txt) == 0:
 165             fmt = 'tablewidth="%s" ' % width + fmt
 166             txt = "||"
 167         if len (fmt): t = "<%s> ||" % fmt
 168         else: t = " ||"
 169         return txt + t
 170     
 171     # Progress
 172     if text.endswith ('%'):
 173         for f in fmt [0] + ' %s' % text, fmt [1] :
 174             res = cell (res, f)
 175 
 176     # Current/Steps
 177     elif text.find ('/') > 0:
 178         cur, steps = map (int, text.split ('/'))
 179         for i in range (steps):
 180             res = cell (res, fmt [i>=cur])
 181 
 182     # Start/end date
 183     else:
 184         starts, ends = map (lambda s:s.strip (),  text.split (","))
 185         start, end = s2t (starts), s2t (ends)
 186         now = time.mktime (time.localtime ())
 187 
 188         duration = int ( (end-start) / 86400)
 189         progress = int ( (now-start) / 86400) -1
 190         pcent = int (90 / duration)
 191 
 192         for i in range (duration):
 193             if i == 0 and progress < 0:
 194                 f = fmt [2]
 195             elif i == progress:
 196                 f = fmt [0]
 197             else:
 198                 f = fmt [1]
 199             res = cell (res, f)
 200                             
 201         if progress >= duration:
 202             res = cell (res, fmt [2])
 203         else:
 204             res = cell (res, fmt [1])
 205 
 206     # Output
 207     if debug:
 208         res = "{{{[[ProgressBar(%s)]]\n%s}}}\n%s" % (orig_text, res, res)
 209     return _format (res, macro.request, macro.formatter)
 210 
 211 
 212 def _format (src_text, request, formatter):
 213     # parse the text (in wiki source format) and make HTML,
 214     # after diverting sys.stdout to a string
 215     str_out = StringIO.StringIO ()      # create str to collect output
 216     request.redirect (str_out)          # divert output to that string
 217     # parse this line
 218     wiki.Parser (src_text, request).format (formatter)
 219     request.redirect ()                 # restore output
 220     return str_out.getvalue ()          # return what was generated

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] (2005-12-18 21:11:39, 5.6 KB) [[attachment:ProgressBar-0.1.1.py]]
  • [get | view] (2006-11-22 08:35:56, 6.4 KB) [[attachment:ProgressBar.py]]
  • [get | view] (2010-02-17 07:13:24, 6.6 KB) [[attachment:ProgressBar.py.moin191]]
  • [get | view] (2009-02-19 14:08:47, 1.2 KB) [[attachment:patch_if_has_import_wiki_error.patch]]
 All files | Selected Files: delete move to page copy to page

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