view MoinMoin/script/import/ @ 1918:bb2e053067fb

fixing copyright headers: remove umlauts (encoding troubles), make epydoc compatible, reformat
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 27 Mar 2007 21:07:33 +0200
parents da5623bc6819
children 8f130438d8e8
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
    MoinMoin - Push files into the wiki.

    This script pushes files from a directory into the wiki (to be exact: it
    pushes all except the last file, as this is maybe still written to in
    case of irc logs).
    One application is to use it to store IRC logs into the wiki.

    moin --config-dir=... --wiki-url=... import irclog --author=IrcLogImporter --file-dir=.
    @copyright: 2005 MoinMoin:AlexanderSchremmer,
                2006 MoinMoin:ThomasWaldmann
    @license: GNU GPL, see COPYING for details.

# this function generates a pagename from the file name
def filename_function(filename):
    filename = filename.lstrip('#')
    splitted = filename.split('.')
    return '/'.join(splitted[0:2])

import os

from MoinMoin.PageEditor import PageEditor
from MoinMoin.script import MoinScript

def decodeLinewise(text):
    resultList = []
    for line in text.splitlines():
            decoded_line = line.decode("utf-8")
        except UnicodeDecodeError:
            decoded_line = line.decode("iso-8859-1")
    return '\n'.join(resultList)

class PluginScript(MoinScript):
    """ irclog importer script class """

    def __init__(self, argv, def_values):
        MoinScript.__init__(self, argv, def_values)
            "--author", dest="author", default="IrcLogImporter",
            help="Use AUTHOR for edit history / RecentChanges"
            "--file-dir", dest="file_dir", default='.',
            help="read files from DIRECTORY"

    def mainloop(self):
        request = self.request
        for root, dirs, files in os.walk(self.options.file_dir):
            for filename in files[:-1]: # do not push the last file as it is constantly written to
                pagename = + filename_function(filename)
                print "Pushing %r as %r" % (filename, pagename)
                p = PageEditor(request, pagename, do_editor_backup=0,
                if p.exists():
                fileObj = open(os.path.join(root, filename), 'rb')
                    p.saveText("#format plain\n" + decodeLinewise(, 0)
                except PageEditor.SaveError, e:
                    print "Got %r" % (e, )
        print "Finished."