view MoinMoin/script/import/ @ 4399:9d161d1dbdfc

import.irclog: use irssi parser to format logs, mimetype added .irc to text/plain
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Wed, 29 Oct 2008 11:49:51 +0100
parents 2572688e031a
children a20de9383481
line wrap: on
line source

# -*- coding: iso-8859-1 -*-
MoinMoin - Push files into the wiki.

@copyright: 2005-2007 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])

class IAmRoot(object):
    def __getattr__(self, name):
        return lambda *args, **kwargs: True

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):
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.

Detailed Instructions:
General syntax: moin [options] import irclog [irclog-options]

[options] usually should be:

[irclog-options] see below:
    0. To add all the files in the current directory to the wiki as the user 'JohnSmith'
       moin ... import irclog --author=JohnSmirh --file-dir=.

    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"
        self.parser.add_option("--acl", dest="acl", default="", help="Set a specific ACL for the pages.")

    def mainloop(self):
        request = self.request
        request.user.may = IAmRoot()
        request.cfg.mail_enabled = False
        for root, dirs, files in os.walk(self.options.file_dir):
            for filename in files:
                pagename = + filename_function(filename)
                #print "Pushing %r as %r" % (filename, pagename)
                p = PageEditor(request, pagename, do_editor_backup=0,, do_revision_backup=0)
                if p.exists():
                    if filename != files[-1]:
                    p = PageEditor(request, pagename, do_editor_backup=0,

                fileObj = open(os.path.join(root, filename), 'rb')
                    acl = ""
                    if self.options.acl:
                        acl = "#acl %s\n" % (self.options.acl, )
                    p.saveText(acl + "#format irc\n" + decodeLinewise(, 0)
                except PageEditor.Unchanged, e:
                except PageEditor.SaveError, e:
                    print "Got %r" % (e, )
        #print "Finished."