Attachment 'RandomGroups-1.2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - RandomGroups Version 1.2, 26.03.2019
   4 
   5     Syntax:
   6        <<RandomGroup([class=]"filename"[,[size=]size][,[sort=]sort][,[head1=]head1][,[head2=]head2])>>
   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(headings, groups):
  54     result = "||'''%s'''||'''%s'''||\n" % headings
  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%(head1)s%(head2)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 < 1):
  82         size = 1
  83     if (size > 99):
  84         size = 99
  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     head1 = args_dict['head1']
  95     if (head1 == '') or (head1 == None):
  96         head1 = 'Group'
  97     head2 = args_dict['head2']
  98     if (head2 == '') or (head2 == None):
  99         head2 = 'Members'
 100     headings = (head1, head2)
 101 
 102 	# get list from attached file
 103     error, text = get_attachment(request, page_name, filename)
 104     if error:
 105         return text
 106     classlist = wikiutil.escape(text) # make sure nothing harmful
 107     classlist = classlist.rstrip(eol)
 108 
 109     if not classlist:
 110         classlist = ''
 111     else:
 112         members = (classlist.split(eol))
 113         shuffle(members)
 114 
 115     result = chunks(members, size, show_sorted)
 116     result = tabulate_groups(headings, result)
 117     return wikiutil.renderText(macro.request, wiki, result)

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