1 """
   2 MoinMoin macro to create a list of current users
   3 UserList.py
   4 ark  06/13/04  0.1
   5 
   6     ***********
   7     by Antti Kuntsi <antti@kuntsi.com>
   8     As far as the author is concerned, this code is released to the public
   9     domain, but some restrictions in the MoinMoin COPYING file may apply. Ask a
  10     lawyer.
  11     _parse_arguments -method from ImageLink.py by jkk
  12     ***********
  13     This code has not been tested exhaustively. Use at your own risk.
  14     This code opens the userdict.pickle, and if it gets corrupted, you have a
  15     problem. You have been warned.
  16     The multiple column support has not been tested, but is assumed to be
  17     working.
  18     ***********
  19 
  20     This macro displays a list of currently known users.
  21 
  22     Usage:
  23         [[UserList]]
  24     [[UserList(column1, column2, ...)]]
  25 
  26     Demo. Try pasting the above examples into a MoinMoin sandbox page.
  27 
  28 
  29 """
  30 
  31 
  32 import string, pickle, time
  33 from math import ceil
  34 from MoinMoin import wikiutil, config
  35 
  36 def sorted_users(users):
  37     usernames=users.keys()
  38     usernames.sort()
  39     for username in usernames:
  40         usertime = time.localtime(float(users[username].split(".")[0]))
  41         yield username, usertime
  42 
  43 def execute(macro, args):
  44     '''handle the UserList Macro. return the generated display HTML
  45     ark  06/13/04'''
  46     # parse the arguments
  47     if args==None:
  48         args = ''
  49     parseargs = _parse_arguments(args, ',', '"')
  50     columnlist=[]
  51     if len(parseargs)>0:
  52         columnlist=filter(None, [name.strip() for name in parseargs])
  53 
  54     result = [macro.formatter.paragraph(0)]
  55     userdict = pickle.load(file(config.data_dir+"/user/userdict.pickle"))
  56 
  57     if not userdict:
  58         return
  59 
  60     userlist = sorted_users(userdict)
  61 
  62     if columnlist:
  63             columnsize=int(ceil(len(userdict)/float(len(columnlist))))
  64     
  65     initials = []
  66     initial=''
  67     currentcount=0
  68     for name, creationtime in userlist:
  69         if initial != name[0]:
  70             initial=name[0]
  71             initials.append(initial)
  72             if currentcount:
  73                 result.append(macro.formatter.bullet_list(0))
  74             if columnlist and (currentcount > columnsize or currentcount == 0):
  75                 newcolumn=columnlist.pop(0)
  76                 if currentcount:
  77                     result.append(macro.formatter.rawHTML('</div>'))
  78                 result.append(macro.formatter.rawHTML('<div id="%s">'%newcolumn))
  79             result.append(macro.formatter.anchordef(initial))
  80             result.append(macro.formatter.heading(2, name[0]))
  81             result.append(macro.formatter.bullet_list(1))
  82         result.append(macro.formatter.listitem(1))
  83         result.append(macro.formatter.paragraph(1))
  84         name = macro.formatter.pagelink(name, name)
  85         result.append("%s -- %s"%(name, time.strftime(config.date_fmt, creationtime)))
  86         result.append(macro.formatter.paragraph(0))
  87         result.append(macro.formatter.listitem(0))
  88         currentcount+=1
  89     
  90     result.append(macro.formatter.bullet_list(0))
  91     if parseargs:
  92         result.append(macro.formatter.rawHTML('</div>'))
  93     while columnlist:
  94         result.append(macro.formatter.rawHTML('<div id="%s">&nbsp;</div>'%columnlist.pop(0)))
  95     anchorlinks = "".join([macro.formatter.anchorlink(a, a) for a in initials])
  96     result.append(macro.formatter.paragraph(1))
  97     result.append(macro.formatter.rawHTML('<br clear="both">'))
  98     result.insert(0, anchorlinks)
  99     result.append(anchorlinks)
 100     return "\n".join(result)
 101 
 102 
 103 def _parse_arguments(argstring, delimchar=',', quotechar='"'):
 104     '''parse argstring into separate arguments originally separated by delimchar.
 105     occurrences of delimchar found within quotechars are ignored.
 106     This parser is not particularly elegant. Improvements are welcome.
 107     jjk  03/28/01'''
 108     # replace delimiters not in quotes with a special delimiter
 109     delim2 = chr(0)
 110     inquote = 0
 111     for i1 in range(len(argstring)):
 112         cchar = argstring[i1]
 113         if cchar==quotechar:
 114             inquote = not inquote
 115         elif (not inquote) and cchar==delimchar:
 116             argstring = argstring[:i1] + delim2 + argstring[i1+1:]
 117     # split the argument string on the special delimiter, and remove external quotes
 118     args = string.split(argstring, delim2)
 119     for i1 in range(len(args)):
 120         arg = args[i1]
 121         if arg[:1]==quotechar and arg[-1:]==quotechar:
 122             args[i1] = arg[1:-1]
 123     return args

MoinMoin: MacroMarket/UserList/SourceCode (last edited 2007-10-29 19:12:07 by localhost)