Attachment 'RandomGroups-1.1.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - RandomGroups Version 1.1, 20.02.2018
   4     
   5     Syntax: 
   6        <<RandomGroup([class=]filename[,[size=]size][,[sort=]sort])>>
   7        
   8     @copyright: 2017 Ian Riley <ian@riley.asia>
   9     license: GNU GPL, see COPYING for details.
  10 
  11 """
  12 
  13 from random import shuffle
  14 from MoinMoin import wikiutil
  15 from MoinMoin.Page import Page
  16 from MoinMoin.action import AttachFile
  17 from MoinMoin.parser.text_moin_wiki import Parser as wiki
  18 
  19 empty = u''
  20 eol = u'\n'
  21 rtn = u'\r'
  22 sep_dir = u'/'
  23 _msg = u'<strong class="error">RandomGroups(): %s</strong>'
  24 _msg_bad_arg = _msg % (u'invalid arguments {%s}')
  25 _msg_bad_file = _msg % (u'invalid file {%s}')
  26 
  27 def get_attachment(request, page_name, source):
  28     if AttachFile.exists(request, page_name, source):
  29         file_name = Page(request, page_name).getPagePath("attachments")
  30         file_name = file_name+sep_dir+source
  31         f = open(file_name,'r')
  32         text = f.read()
  33         f.close()
  34         text = text.decode("utf-8").replace(rtn,empty)
  35         if text == empty:
  36             text = _msg_bad_file % ('attachment empty: ' + source)
  37             return True, text
  38     else:
  39         text = _msg_bad_file % ('not found: ' + source)
  40         return True, text
  41     return False, text
  42 
  43 def chunks(l, n, show_sorted):
  44     """Return successive n-sized chunks from l."""
  45     result = []
  46     for i in range(0, len(l), n):
  47         chunk = l[i:i + n]
  48         if show_sorted:
  49             chunk = sorted(chunk)
  50         result.append(chunk)
  51     return result
  52 
  53 def tabulate_groups(groups):
  54     result = "||'''Group'''||'''Members'''||\n"
  55     for i in range(0, len(groups)):
  56         result = result + '||%s||%s||\n' % (str(i+1), ', '.join(groups[i]))
  57     return result
  58 
  59 def execute(macro, args):
  60     request = macro.request
  61     page = macro.formatter.page
  62     page_name = page.page_name
  63 
  64     # parse and check arguments
  65     try:
  66         args_parser = wikiutil.ParameterParser('%(class)s%(size)s%(sort)s')
  67         (count,args_dict) = args_parser.parse_parameters(args)
  68     except:
  69         return (_msg_bad_arg % ('cannot parse: ' + args))
  70 
  71     filename = args_dict['class']
  72     if (filename == '') or (filename == None):
  73         filename = 'class.txt'
  74     if ".." in filename:
  75          return (_msg_bad_file % ('.. not permitted in: ' + filename))
  76 
  77     size = args_dict['size']
  78     if size == None:
  79         size = '2'
  80     size = int(size)
  81     if (size < 2): 
  82         size = 2
  83     if (size > 6):
  84         size = 6
  85 
  86     show_sorted = args_dict['sort']
  87     if show_sorted == None:
  88         show_sorted = ''
  89     if show_sorted.lower() in ['', '1', 't', 'true', 'yes']:
  90         show_sorted = True
  91     else:
  92         show_sorted = False
  93 	
  94 	# get list from attached file
  95     error, text = get_attachment(request, page_name, filename)
  96     if error:
  97         return text 
  98     classlist = wikiutil.escape(text) # make sure nothing harmful
  99     classlist = classlist.rstrip(eol)
 100 
 101     if not classlist:
 102         classlist = ''
 103     else:
 104         members = (classlist.split(eol))
 105         shuffle(members)
 106     
 107     result = chunks(members, size, show_sorted)
 108     result = tabulate_groups(result)
 109     return wikiutil.renderText(macro.request, wiki, result)

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