Back to index

moin  1.9.0~rc2
irclog.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003 MoinMoin - Push files into the wiki.
00004 
00005 @copyright: 2005-2007 MoinMoin:AlexanderSchremmer
00006             2006 MoinMoin:ThomasWaldmann
00007 @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 # this function generates a pagename from the file name
00011 def filename_function(filename):
00012     filename = filename.lstrip('#')
00013     splitted = filename.split('.')
00014     return '/'.join(splitted[0:2])
00015 
00016 class IAmRoot(object):
00017     def __getattr__(self, name):
00018         return lambda *args, **kwargs: True
00019 
00020 
00021 import os
00022 
00023 from MoinMoin.PageEditor import PageEditor
00024 from MoinMoin.script import MoinScript
00025 
00026 def decodeLinewise(text):
00027     resultList = []
00028     for line in text.splitlines():
00029         try:
00030             decoded_line = line.decode("utf-8")
00031         except UnicodeDecodeError:
00032             decoded_line = line.decode("iso-8859-1")
00033         resultList.append(decoded_line)
00034     return '\n'.join(resultList)
00035 
00036 
00037 class PluginScript(MoinScript):
00038     """\
00039 Purpose:
00040 ========
00041 This script pushes files from a directory into the wiki (to be exact: it
00042 pushes all except the last file, as this is maybe still written to in
00043 case of irc logs).
00044 One application is to use it to store IRC logs into the wiki.
00045 
00046 Detailed Instructions:
00047 ======================
00048 General syntax: moin [options] import irclog [irclog-options]
00049 
00050 [options] usually should be:
00051     --config-dir=/path/to/my/cfg/ --wiki-url=wiki.example.org/
00052 
00053 [irclog-options] see below:
00054     0. To add all the files in the current directory to the wiki as the user 'JohnSmith'
00055        moin ... import irclog --author=JohnSmirh --file-dir=.
00056 """
00057 
00058     def __init__(self, argv, def_values):
00059         MoinScript.__init__(self, argv, def_values)
00060         self.parser.add_option(
00061             "--author", dest="author", default="IrcLogImporter",
00062             help="Use AUTHOR for edit history / RecentChanges"
00063         )
00064         self.parser.add_option(
00065             "--file-dir", dest="file_dir", default='.',
00066             help="read files from DIRECTORY"
00067         )
00068         self.parser.add_option("--acl", dest="acl", default="", help="Set a specific ACL for the pages.")
00069 
00070     def mainloop(self):
00071         self.init_request()
00072         request = self.request
00073         request.user.may = IAmRoot()
00074         request.cfg.mail_enabled = False
00075         for root, dirs, files in os.walk(self.options.file_dir):
00076             files.sort()
00077             for filename in files:
00078                 pagename = self.options.page + filename_function(filename)
00079                 #print "Pushing %r as %r" % (filename, pagename)
00080                 p = PageEditor(request, pagename, do_editor_backup=0, uid_override=self.options.author, do_revision_backup=0)
00081                 if p.exists():
00082                     if filename != files[-1]:
00083                         continue
00084                 else:
00085                     p = PageEditor(request, pagename, do_editor_backup=0, uid_override=self.options.author)
00086 
00087                 fileObj = open(os.path.join(root, filename), 'rb')
00088                 try:
00089                     acl = ""
00090                     if self.options.acl:
00091                         acl = "#acl %s\n" % (self.options.acl, )
00092                     p.saveText(acl + "#format irc\n" + decodeLinewise(fileObj.read()), 0)
00093                 except PageEditor.Unchanged, e:
00094                     pass
00095                 except PageEditor.SaveError, e:
00096                     print "Got %r" % (e, )
00097                 fileObj.close()
00098         #print "Finished."
00099