#!/usr/bin/env python
"""
Write text to a wiki page
==========================

Configuration::

Before this script will work, you need to edit this script to include some
information about your environment.  This script needs to know which MoinMoin
user it should run as, where your wikiconfig.py file is, and where the MoinMoin
python libraries are.

Just below these configuration and usage instructions, you'll find a python
array which appends paths to sys.path.  Add the directory containing
wikiconfig.py and the directory containing the MoinMoin libraries to this.

Below that you'll see a variable "editor_email = None".  This needs to be set
so MoinMoin can process ACLS and allow or deny access to the files you want to
edit.  If you leave this as None, it's the equivalent of a user who isn't
logged in, so I reccommend using this so you can use more restrictive ACLS on
the files you generate in this way.  It asks for an email address because that
is how MoinMoin can look up individual users, not so it can send you email.
This is the email address stored in the appropriate MoinMoin user's account
settings.

Usage::
    
    python write-text url pagename file

Write the text in filename to the page url/pagename, saving a backup
of the old revision and updating the logs. This should be equivalent to
editing the page manually with a browser.

file is just an example, you might want to customize this script to
read the data from another process, the web or whatever.

If pagename does not exists, it is created automatically. If pagename
is an underlay page, it will be copied into the data directory before
the edit is performed.

You should run the script with the same user and group as used by the
the wiki server, e.g. 'www-data', or the new revisions might be saved
with wrong owner, which will cause an IOError later when browsing the
wiki.

This script, "write-text.py" is different from "append-text.py" only
in that it does not append, but writes new text every time.  This is 
good for updating text that changes, but not good for adding items to 
an existing page.  If you want to append, try "append-text.py".

Examples::

    sudo -u www-data python write-text.py 'localhost/mywiki/' \
        'Additions'  'additions.txt'

Tested with MoinMoin 1.6 

WARNING: EXPERIMENTAL, USED WITH CAUTION!

Append-text.py
@copyright: 2005 by Nir Soffer <nirs@freeshell.org>
@license: GNU GPL, see COPYING for details.

Write-text.py - hacked from Append-text.py 
All the hard word was done by Nir Soffer, I just tweaked his 
script until it did what I wanted.
@copyright: 2008 by Dylan Martin <dmartin@NOSPAM.cpan.org>
@license: GNU GPL, see COPYING for details.

"""

# Path configuration - append paths to your wiki files and the MoinMoin files
import sys
sys.path = [
            '/home/dmartin/mywiki',  # contains wikiconfig.py
            '/home/dmartin/python/', # contains MoinMoin/__init__.py ...etc...
            ] + sys.path

# set this to the email addr of the moin user who this script should act as
# or leave as None
# editor_email = 'editor@example.com'
editor_email = None 

from MoinMoin.PageEditor import PageEditor
from MoinMoin.request.request_cli import Request
from MoinMoin.user import get_by_email_address

import MoinMoin.PageEditor

def write (url, pagename, text):
    request = Request(url=url, pagename=pagename)
    editor = PageEditor(request, pagename)
    if  editor_email:
        if not get_by_email_address(request,editor_email):
            raise Exception, 'No MoinMoin user found with email address ' + editor_email
        request.user = get_by_email_address(request,editor_email)
    #text = editor.get_raw_body() + editor.normalizeText(text)
    text = editor.normalizeText(text)
    dummy, revision, exists = editor.get_rev()
    return editor.saveText(text, revision)


if __name__ == '__main__':
    """ Example: reading from file on disk using config.charset """
    import codecs
    from MoinMoin import config
    try:
        url, pagename, filename = sys.argv[1:]
        text = codecs.open(filename, 'r', config.charset).read()        
        write(url, pagename, text)
    except ValueError:
        print __doc__
    except PageEditor.Unchanged, e:
        exit
