Attachment 'IncVar.py'

Download

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