Attachment 'xmlrpc_collection_scripts.py'
Download 1 #!/usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3
4 """
5 Just examples for using the xmlrpc calls.
6
7 """
8 import getpass
9 import os
10 import time
11 import xmlrpclib
12
13 NAME = os.environ.get("WIKIUSER", "")
14 PAGENAME = u"ExampleTestPage"
15 ATTACHNAME = u"example.zip"
16 PASSWORD = getpass.getpass()
17 WIKIURL = "http://localhost:8080/"
18
19 def get_filelist():
20 """"
21 this script creates a page PAGENAME
22 currently the pagename is hard coded
23 username NAME is read from an env var WIKIUSER
24 """
25 if NAME and PASSWORD:
26 xmlrpc_url = "%s?action=xmlrpc2" % WIKIURL
27 homewiki = xmlrpclib.ServerProxy(xmlrpc_url, allow_none=True)
28 multicall = xmlrpclib.MultiCall(homewiki)
29 auth_token = homewiki.getAuthToken(NAME, PASSWORD)
30 if auth_token:
31 multicall.applyAuthToken(auth_token)
32 multicall.listAttachments(PAGENAME)
33 result = multicall()
34 if tuple(result)[0] == "SUCCESS":
35 return tuple(result)[1]
36 return []
37
38 def put_page():
39 """"
40 this script creates a page PAGENAME
41 currently the pagename is hard coded
42 username NAME is read from an env var WIKIUSER
43 """
44 filelist = get_filelist()
45 filelist = [txt for txt in filelist if txt.endswith('.png')]
46
47 if NAME and PASSWORD and ATTACHNAME.endswith('.zip'):
48 xmlrpc_url = "%s?action=xmlrpc2" % WIKIURL
49 homewiki = xmlrpclib.ServerProxy(xmlrpc_url, allow_none=True)
50 multicall = xmlrpclib.MultiCall(homewiki)
51 auth_token = homewiki.getAuthToken(NAME, PASSWORD)
52 if auth_token:
53 multicall.applyAuthToken(auth_token)
54 text = [' * {{attachment:%s}} ' % txt for txt in filelist]
55 multicall.putPage(PAGENAME, '\n'.join(text))
56 result = multicall()
57 if isinstance(result, tuple) and tuple(result)[0] == "SUCCESS":
58 print "page '%s' created: %s" % (PAGENAME, tuple(result)[0])
59 else:
60 print 'You did not change the page content, not saved!'
61
62 def rename_page():
63 """"
64 this script renames a page PAGENAME hardcoded to a prefix of Trash_
65 and a time stamp
66 currently the pagename is hard coded
67 username NAME is read from an env var WIKIUSER
68 """
69 if NAME and PASSWORD:
70 xmlrpc_url = "%s?action=xmlrpc2" % WIKIURL
71 homewiki = xmlrpclib.ServerProxy(xmlrpc_url, allow_none=True)
72 multicall = xmlrpclib.MultiCall(homewiki)
73 auth_token = homewiki.getAuthToken(NAME, PASSWORD)
74 if auth_token:
75 multicall.applyAuthToken(auth_token)
76 trash_name = 'Trash_%d_%s' % (time.time(), PAGENAME)
77 multicall.renamePage(PAGENAME, trash_name)
78 result = multicall()
79 if tuple(result)[0] == "SUCCESS":
80 print "page '%s' renamed to %s: %s" % (PAGENAME,
81 trash_name, tuple(result)[0])
82 else:
83 print tuple(result)[1]
84
85 def unzip_attachment():
86 """"
87 this script unzips an attachment ATTACHNME on a page
88 currently the attachment name and the pagename is hard coded
89 username NAME is read from an env var WIKIUSER
90 """
91 if NAME and PASSWORD and ATTACHNAME.endswith('.zip'):
92 xmlrpc_url = "%s?action=xmlrpc2&target=%s" % (WIKIURL, ATTACHNAME)
93 homewiki = xmlrpclib.ServerProxy(xmlrpc_url, allow_none=True)
94 multicall = xmlrpclib.MultiCall(homewiki)
95 auth_token = homewiki.getAuthToken(NAME, PASSWORD)
96 if auth_token:
97 multicall.applyAuthToken(auth_token)
98 multicall.unzipAttachment(PAGENAME)
99 result = multicall()
100 print "zip file '%s' on page '%s' unpacked: %s" % (ATTACHNAME,
101 PAGENAME, tuple(result)[0])
102
103 def put_attachment():
104 """"
105 this script adds an attachment ATTACHNANE to a page
106 currently the attachment name and the pagename is hard coded
107 username NAME is read from an env var WIKIUSER
108 """
109 if NAME and PASSWORD and ATTACHNAME:
110 xmlrpc_url = "%s?action=xmlrpc2" % WIKIURL
111 homewiki = xmlrpclib.ServerProxy(xmlrpc_url, allow_none=True)
112 multicall = xmlrpclib.MultiCall(homewiki)
113 auth_token = homewiki.getAuthToken(NAME, PASSWORD)
114 if auth_token:
115 multicall.applyAuthToken(auth_token)
116 text = file(ATTACHNAME, 'rb+').read()
117 data = xmlrpclib.Binary(text)
118 multicall.putAttachment(PAGENAME, ATTACHNAME, data)
119 result = multicall()
120 print "zip file '%s' on page '%s' saved: %s" % (ATTACHNAME,
121 PAGENAME, tuple(result)[0])
122 else:
123 print 'Wrong name: "%s" or password entered!' % NAME
124 else:
125 print 'Missing name or password'
126
127 def del_attachment():
128 """"
129 this script deletes the ATTACHNAME from the wiki
130 currently the attachment name and the pagename is hard coded
131 username NAME is read from an env var WIKIUSER
132 """
133 if NAME and PASSWORD:
134 xmlrpc_url = "%s?action=xmlrpc2" % WIKIURL
135 homewiki = xmlrpclib.ServerProxy(xmlrpc_url, allow_none=True)
136 multicall = xmlrpclib.MultiCall(homewiki)
137 auth_token = homewiki.getAuthToken(NAME, PASSWORD)
138 if auth_token:
139 multicall.applyAuthToken(auth_token)
140 multicall.delAttachment(PAGENAME, ATTACHNAME)
141 result = multicall()
142 print "file '%s' on page '%s' deleted: %s" % (ATTACHNAME,
143 PAGENAME, tuple(result)[0])
144 else:
145 print 'Wrong name: "%s" or password entered!' % NAME
146 else:
147 print 'Missing name or password'
148
149 if __name__ == "__main__":
150 put_attachment()
151 unzip_attachment()
152 put_page()
153 del_attachment()
154 rename_page()
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.