Attachment 'table2dict.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3 MoinMoin - creates a dict page from an existing table
   4 
   5 @copyright: 2009 MoinMoin:ReimarBauer
   6 @license: GNU GPL, see COPYING for details.
   7 """
   8 from MoinMoin import user, wikiutil
   9 from MoinMoin.Page import Page
  10 from MoinMoin.PageEditor import PageEditor
  11 from MoinMoin.script import MoinScript
  12 
  13 class PluginScript(MoinScript):
  14     """\
  15 Purpose:
  16 ========
  17 This tool allows you to create subpages having definition lists from a given table
  18 in wiki markup. The first column entry is used for the subpage name while the first
  19 line is used to get the keys.
  20 
  21 
  22 Detailed Instructions:
  23 ======================
  24 General syntax: moin [options] account create [create-options]
  25 
  26 [options] usually should be:
  27     --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
  28 
  29 1. Use, the --file argument could be added to the above examples,
  30 causing the script to respect ACLs.
  31 1. Use a base --base_page argument for the page name
  32 """
  33 
  34     def __init__(self, argv, def_values):
  35         MoinScript.__init__(self, argv, def_values)
  36 
  37         self.parser.add_option(
  38             "-u", "--user", dest="homepage_creater",
  39             help="User as whom the homepage creation operation will be performed as. "
  40             )
  41 
  42         self.parser.add_option(
  43             "-b", "--base_page", dest="base_pagename",
  44             help="the base page of the subpages "
  45             )
  46 
  47         self.parser.add_option(
  48             "-f", "--file", dest="input_file",
  49             help="filename to proceed on"
  50             )
  51 
  52     def mainloop(self):
  53         # we don't expect non-option arguments
  54         self.init_request()
  55         request = self.request
  56         # Check for user
  57                 # Check for user
  58         if self.options.homepage_creater:
  59             request.user = user.User(request, name=self.options.homepage_creater)
  60 
  61         filename = self.options.input_file
  62         base_page_name = self.options.base_pagename or u'Example'
  63 
  64         if not filename:
  65             print "Please provide the filename -f"
  66             return
  67         raw = open(filename, 'r').readlines()
  68 
  69         keys = (raw[0]).split('||')[1:-1]
  70         for line in raw[1:]:
  71             text = []
  72             if line.startswith('||'):
  73                 row = line.split('||')[1:-1]
  74                 idx = 0
  75                 for key in keys:
  76                     text.append(' %s:: %s' % (key.strip(), (row[idx]).strip()))
  77                     idx += 1
  78                 text = '\n'.join(text)
  79 
  80                 pagename = '%s/%s' % (base_page_name, (row[0]).strip())
  81                 print pagename
  82                 page = PageEditor(request, pagename)
  83                 if not request.user.disabled and not Page(request, pagename).exists():
  84                     try:
  85                         page._write_file(text, action='SAVE')
  86                         print "INFO page for %s created." % pagename
  87                     except page.Unchanged:
  88                             print "You did not change the page content, not saved!"
  89                 else:
  90                     print "INFO page for %s already exists." % pagename

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] (2009-08-12 03:20:27, 6.9 KB) [[attachment:CollectLists.py.neu]]
  • [get | view] (2009-08-12 03:30:30, 7.7 KB) [[attachment:CollectLists.py.neu.mod]]
  • [get | view] (2009-02-11 07:33:36, 56.0 KB) [[attachment:problem_iex.jpg]]
  • [get | view] (2009-02-04 17:41:12, 3.0 KB) [[attachment:table2dict.py]]
 All files | Selected Files: delete move to page copy to page

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