Attachment 'write-text.py'

Download

   1 #!/usr/bin/env python
   2 """
   3 Write text to a wiki page
   4 ==========================
   5 
   6 Configuration::
   7 
   8 Before this script will work, you need to edit this script to include some
   9 information about your environment.  This script needs to know which MoinMoin
  10 user it should run as, where your wikiconfig.py file is, and where the MoinMoin
  11 python libraries are.
  12 
  13 Just below these configuration and usage instructions, you'll find a python
  14 array which appends paths to sys.path.  Add the directory containing
  15 wikiconfig.py and the directory containing the MoinMoin libraries to this.
  16 
  17 Below that you'll see a variable "editor_email = None".  This needs to be set
  18 so MoinMoin can process ACLS and allow or deny access to the files you want to
  19 edit.  If you leave this as None, it's the equivalent of a user who isn't
  20 logged in, so I reccommend using this so you can use more restrictive ACLS on
  21 the files you generate in this way.  It asks for an email address because that
  22 is how MoinMoin can look up individual users, not so it can send you email.
  23 This is the email address stored in the appropriate MoinMoin user's account
  24 settings.
  25 
  26 Usage::
  27     
  28     python write-text url pagename file
  29 
  30 Write the text in filename to the page url/pagename, saving a backup
  31 of the old revision and updating the logs. This should be equivalent to
  32 editing the page manually with a browser.
  33 
  34 file is just an example, you might want to customize this script to
  35 read the data from another process, the web or whatever.
  36 
  37 If pagename does not exists, it is created automatically. If pagename
  38 is an underlay page, it will be copied into the data directory before
  39 the edit is performed.
  40 
  41 You should run the script with the same user and group as used by the
  42 the wiki server, e.g. 'www-data', or the new revisions might be saved
  43 with wrong owner, which will cause an IOError later when browsing the
  44 wiki.
  45 
  46 This script, "write-text.py" is different from "append-text.py" only
  47 in that it does not append, but writes new text every time.  This is 
  48 good for updating text that changes, but not good for adding items to 
  49 an existing page.  If you want to append, try "append-text.py".
  50 
  51 Examples::
  52 
  53     sudo -u www-data python write-text.py 'localhost/mywiki/' \
  54         'Additions'  'additions.txt'
  55 
  56 Tested with MoinMoin 1.6 
  57 
  58 WARNING: EXPERIMENTAL, USED WITH CAUTION!
  59 
  60 Append-text.py
  61 @copyright: 2005 by Nir Soffer <nirs@freeshell.org>
  62 @license: GNU GPL, see COPYING for details.
  63 
  64 Write-text.py - hacked from Append-text.py 
  65 All the hard word was done by Nir Soffer, I just tweaked his 
  66 script until it did what I wanted.
  67 @copyright: 2008 by Dylan Martin <dmartin@NOSPAM.cpan.org>
  68 @license: GNU GPL, see COPYING for details.
  69 
  70 """
  71 
  72 # Path configuration - append paths to your wiki files and the MoinMoin files
  73 import sys
  74 sys.path = [
  75             '/home/dmartin/mywiki',  # contains wikiconfig.py
  76             '/home/dmartin/python/', # contains MoinMoin/__init__.py ...etc...
  77             ] + sys.path
  78 
  79 # set this to the email addr of the moin user who this script should act as
  80 # or leave as None
  81 # editor_email = 'editor@example.com'
  82 editor_email = None 
  83 
  84 from MoinMoin.PageEditor import PageEditor
  85 from MoinMoin.request.request_cli import Request
  86 from MoinMoin.user import get_by_email_address
  87 
  88 import MoinMoin.PageEditor
  89 
  90 def write (url, pagename, text):
  91     request = Request(url=url, pagename=pagename)
  92     editor = PageEditor(request, pagename)
  93     if  editor_email:
  94         if not get_by_email_address(request,editor_email):
  95             raise Exception, 'No MoinMoin user found with email address ' + editor_email
  96         request.user = get_by_email_address(request,editor_email)
  97     #text = editor.get_raw_body() + editor.normalizeText(text)
  98     text = editor.normalizeText(text)
  99     dummy, revision, exists = editor.get_rev()
 100     return editor.saveText(text, revision)
 101 
 102 
 103 if __name__ == '__main__':
 104     """ Example: reading from file on disk using config.charset """
 105     import codecs
 106     from MoinMoin import config
 107     try:
 108         url, pagename, filename = sys.argv[1:]
 109         text = codecs.open(filename, 'r', config.charset).read()        
 110         write(url, pagename, text)
 111     except ValueError:
 112         print __doc__
 113     except PageEditor.Unchanged, e:
 114         exit

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-12-15 19:16:47, 4.1 KB) [[attachment:write-text.py]]
 All files | Selected Files: delete move to page copy to page

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