Attachment 'MacroMarket-IncVar.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - IncVar Macro
   4 
   5     This macro tries to read data about a page stored in another page.
   6     Sometimes, it can be useful to have one piece of data updated on multiple pages.
   7 
   8     The data is formatted in the source page as such:
   9 
  10         VariableName: Data\n
  11 
  12     Where "VariableName" is the name given to the data (followed by a colon':'), and where
  13     "Data" can be any length of text, terminated with a newline. The "Data" is obtained by
  14     removing the variablename, the colon, and the space seperating them. (see code below).
  15     Also, only one variable / data pair per line. A #format plain is generally the best
  16     for data pages.
  17 
  18     Usage: try:
  19 
  20         [[IncVar(WikiSandBox,storedtext)]]
  21 
  22     ...on any page to get "storedtext" for page "WikiSandBox", or
  23 
  24         [[IncVar(storedtext)]]
  25 
  26     ...to get "storedtext" for the "current page"/RawData or...
  27 
  28         [[IncVar]]
  29 
  30     ...to get the time the data is/was refreshed. Generally, this will be the current time.
  31 
  32     It will return errors if there is no stored
  33     data, or if a page does not exist. 
  34 
  35     @copyright: 2005 by Derek Wilson <sp1d3rx@gmail.com>
  36     @license: GNU GPL, see COPYING for details.
  37 
  38     Portions of this code have been taken from or inspired by Thomas Waldmann and
  39     Gustavo Niemeyer. Originally based off of "RandomQuote.py".
  40 
  41 """
  42 
  43 import random, StringIO, time
  44 from MoinMoin.Page import Page, wikiutil
  45 
  46 Dependencies = ["time"]
  47 
  48 def execute(macro, args):
  49     _ = macro.request.getText
  50     
  51     if args: args = args.split(',')
  52     #If there aren't any args, just show the date and time...
  53     else: return macro.formatter.text("%s" % time.strftime("%A %b %Y - %I:%M%p",time.localtime()))
  54 
  55     #If there's more than 1 arg, then that's a page name and a variable.
  56     if len(args) > 1:
  57         pagename = args[0]
  58         srchvar = args[1]
  59     else: #Otherwise, it's just a variable, and the pagename is implied to be "/RawData".
  60         srchvar = args[0]
  61         pagename = macro.formatter.page.page_name + "/RawData"
  62         
  63     if macro.request.user.may.read(pagename):
  64         page = Page(macro.request, pagename)
  65         raw = page.get_raw_body()
  66     else:
  67         raw = ""
  68     srchvar = srchvar + ':'
  69     lines = raw.splitlines()
  70     lines = [line.strip() for line in lines]
  71     
  72     if not lines:
  73         return (macro.formatter.highlight(1) +
  74                 _('No variables on %(pagename)s.') % {'pagename': pagename} +
  75                 macro.formatter.highlight(0))
  76     #Search for the variable in the file. Note, stops at first instance of variable.
  77     for line in lines:
  78         result = ""
  79         if line.startswith( srchvar + ' ' ):
  80             result = line.lstrip(srchvar)[1:] #strip off the space between var and data.
  81             break
  82         else:
  83             result = "<< data not found on that page >>"
  84     return macro.formatter.text("%s") % result

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-01-29 17:51:32, 3.1 KB) [[attachment:IncVar.py]]
  • [get | view] (2005-01-25 16:49:19, 2.8 KB) [[attachment:MacroMarket-IncVar.py]]
 All files | Selected Files: delete move to page copy to page

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