annotate MoinMoin/script/xmlrpc/write.py @ 2939:6259bb49366c

trivial xmlrpc examples (scripts)
author Johannes Berg <johannes AT sipsolutions DOT net>
date Fri, 02 Nov 2007 12:43:42 +0000
parents
children ed1a433803c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2939
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
2 """
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
3 MoinMoin - page contents writer
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
4
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
5 Commented example of how to edit a page with xmlrpc.
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
6
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
7 @copyright: 2007 MoinMoin:JohannesBerg
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
8 @license: GNU GPL, see COPYING for details.
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
9 """
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
10
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
11 import xmlrpclib
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
12 import sys
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
13
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
14 from MoinMoin.script import MoinScript
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
15 from MoinMoin.support.multicall import MultiCall
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
16
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
17 class PluginScript(MoinScript):
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
18 def __init__(self, argv, def_values):
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
19 MoinScript.__init__(self, argv, def_values)
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
20 self.argv = argv
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
21
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
22 # script entrypoint
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
23 def mainloop(self):
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
24 # grab parameters
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
25 url = self.argv[0]
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
26 user = self.argv[1]
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
27 passwd = self.argv[2]
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
28 pagename = self.argv[3]
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
29
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
30 # get auth token from server giving username/password
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
31 s = xmlrpclib.ServerProxy(url)
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
32 token = s.getAuthToken(user, passwd)
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
33
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
34 if token == '':
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
35 print 'Invalid username/password'
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
36 return
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
37
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
38 # Verify that the token is valid by using it
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
39 # and checking that the result is 'SUCCESS'.
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
40 # The token should be valid for 15 minutes.
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
41 assert s.applyAuthToken(token) == 'SUCCESS'
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
42
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
43 try:
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
44 # read new page contents
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
45 content = sys.stdin.read()
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
46
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
47 # build a multicall object that
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
48 mcall = MultiCall(s)
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
49 # first applies the token and
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
50 mcall.applyAuthToken(token)
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
51 # then edits the page
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
52 mcall.putPage(pagename, content)
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
53
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
54 # now execute the multicall
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
55 results = mcall()
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
56
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
57 # everything should have worked
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
58 # instead of the asserts you can have anything else
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
59 # but you should definitely access all the results
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
60 # once so that faults are checked and raised
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
61 assert results[0] == 'SUCCESS'
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
62 assert results[1] == True
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
63
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
64 finally:
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
65 # be nice to the server and clean up the token
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
66 # regardless of what happened
6259bb49366c trivial xmlrpc examples (scripts)
Johannes Berg <johannes AT sipsolutions DOT net>
parents:
diff changeset
67 assert s.deleteAuthToken(token) == 'SUCCESS'