Back to index

moin  1.9.0~rc2
write.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - page contents writer
00004 
00005     @copyright: 2007 MoinMoin:JohannesBerg
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 import xmlrpclib
00010 import sys
00011 
00012 from MoinMoin.script import MoinScript
00013 
00014 
00015 class PluginScript(MoinScript):
00016     """\
00017 Purpose:
00018 ========
00019 This tool allows you to edit a page with xmlrpc. It is more of a commented
00020 example than an actual script.
00021 
00022 Detailed Instructions:
00023 ======================
00024 General syntax: moin xmlrpc write <targeturl> <username> <password> <pagename>
00025 
00026 Example:
00027     To edit the page 'FrontPage' on 'http://wiki.example.org/' using the username
00028     'JohnSmith' and the password 'MyPass', changing the page contents
00029     to 'This will be the new contents of the page!' use:
00030     moin xmlrpc write http://wiki.example.org/ JohnSmith MyPass FrontPage
00031     This will be the new contents of the page!
00032     ^D
00033 
00034 Note: we automatically append ?action=xmlrpc2 to the target url given.
00035 
00036 """
00037 
00038     def __init__(self, argv, def_values):
00039         MoinScript.__init__(self, argv, def_values)
00040         self.argv = argv
00041 
00042     # script entrypoint
00043     def mainloop(self):
00044         # grab parameters
00045         url = self.argv[0] + '?action=xmlrpc2'
00046         user = self.argv[1]
00047         passwd = self.argv[2]
00048         pagename = self.argv[3]
00049 
00050         # get auth token from server giving username/password
00051         s = xmlrpclib.ServerProxy(url)
00052         token = s.getAuthToken(user, passwd)
00053 
00054         if not token:
00055             print 'Invalid username/password'
00056             return
00057 
00058         # Verify that the token is valid by using it
00059         # and checking that the result is 'SUCCESS'.
00060         # The token should be valid for 15 minutes.
00061         assert s.applyAuthToken(token) == 'SUCCESS'
00062 
00063         try:
00064             # read new page contents
00065             content = sys.stdin.read()
00066 
00067             # build a multicall object that
00068             mcall = xmlrpclib.MultiCall(s)
00069             # first applies the token and
00070             mcall.applyAuthToken(token)
00071             # then edits the page
00072             mcall.putPage(pagename, content)
00073 
00074             # now execute the multicall
00075             results = mcall()
00076 
00077             # everything should have worked
00078             # instead of the asserts you can have anything else
00079             # but you should definitely access all the results
00080             # once so that faults are checked and raised
00081             assert results[0] == 'SUCCESS'
00082             assert results[1] is True
00083 
00084         finally:
00085             # be nice to the server and clean up the token
00086             # regardless of what happened
00087             assert s.deleteAuthToken(token) == 'SUCCESS'
00088