Attachment 'xmlrpc_newedit.py'
Download 1 """
2 This plugin is archieve some xml-rpc api which are not standard, but may be useful.
3
4 Implemented methods:
5 - newedit.editCategory you can edit the existing category, if the category is not existed, it'll create a new one
6 - newedit.putFile put file to blog
7 - newedit.getFileList get file list which you'v uploaded
8 - newedit.getSysFile get system file which you can access
9 - newedit.getSysFileList get system file list which you can access
10 - newedit.putSysFile put system file which you can access
11
12 If you want to use putFile function, you should add parameter as blow:
13
14 newedit_filepath - the path that files will be saved, it's a relative path according to datadir
15
16 getSysFile is used to modify some important files just like links.dat, flavour template files, etc. If you want to use
17 this method, you shoud add parameter in config.py as blow:
18
19 newedit_filelist - is a filename, the content of it is a list of filename that enable accessed via xmlrpc.
20 so if you invoke the method, it'll check the filename in the list first. If existed, you can
21 get the content of the file, and put the new content back via putSysFile. It should be a relative
22 path according to datadir.
23
24 For example:
25
26 config.py
27
28 py['newedit_filepath'] = 'files'
29 py['newedit_filelist'] = 'filelist.dat'
30
31 the structure of directory:
32
33 files/
34 entries/ #datadir
35 |
36 +------ filelist.dat
37 """
38 __version__ = '0.1'
39 __author__ = 'limodou chatme at 263 dot com'
40
41 import os
42 import os.path
43 import xmlrpclib
44 from Pyblosxom import tools, plugin_utils
45
46 class NError(Exception): pass
47
48 def getFilePath(request):
49 """
50 get newedit_filepath value from request
51
52 @param request: the pyblosxom Request instance
53 @type request: Request
54
55 @returns the path string of filepath or raise a exception
56 """
57 config = request.getConfiguration()
58 datadir = config['rootdir']
59 path = os.path.join(datadir, config['newedit_filepath'])
60 if path.startswith(datadir):
61 return path
62 else:
63 raise NError, 'The filepath is not correct.'
64
65 def verify_installation(request):
66 config = request.getConfiguration()
67
68 retval = 1
69
70 if not config.has_key('newedit_filepath'):
71 print 'The "newedit_filepath" is not existing'
72 retval = 0
73
74 try:
75 dir = getFilePath(request)
76 except:
77 print 'The "newedit_filepath" is not correct, it should be a relavite path according to datadir.'
78 retval = 0
79
80 if not os.path.isdir(dir):
81 print 'The "newedit_filepath" property in the config file must refer to a directory'
82 retval = 0
83
84 if os.access(dir, 0777):
85 print 'The rights of the "newedit_filepath" is should be 777'
86 retval = 0
87
88 return retval
89
90 def cb_xmlrpc_register(args):
91 """
92 Binds the methods that we handle with the function instances.
93 Part of the pyblosxom callback api
94
95 @param args: the callback arguments
96 @type args: dict
97 """
98 args["methods"].update(
99 {'newedit.editCategory':newedit_editCategory,
100 'newedit.putFile':newedit_putFile,
101 'newedit.getFileList':newedit_getFileList,
102 'newedit.getSysFile':newedit_getSysFile,
103 'newedit.getSysFileList':newedit_getSysFileList,
104 'newedit.putSysFile':newedit_putSysFile,
105 })
106
107 return args
108
109 def authenticate(request, username, password):
110 """
111 Handles authentication. This works by getting the xmlrpc dispatcher
112 plugin Python module and then calling authenticate on that.
113
114 @param request: the pyblosxom Request instance
115 @type request: Request
116
117 @param username: the username
118 @type username: string
119
120 @param password: the password
121 @type password: string
122 """
123 xmlrpc_plugin = plugin_utils.get_plugin_by_name("xmlrpc")
124 xmlrpc_plugin.authenticate(request, username, password)
125
126 def newedit_editCategory(request, username, password, path, description):
127 """
128 Edit an existing category, but if the category is not existed,
129 it'll create a new one. And the description is must not be existed.
130
131 @param request: the pyblosxom Request instance
132 @type request: Request
133
134 @param username: the username
135 @type username: string
136
137 @param password: the password
138 @type password: string
139
140 @param path: category's path
141 @type path: string
142
143 @param description: category's description
144 @type description: string
145
146 @returns an xmlrpclib boolean -- true if the edit was successful, false otherwise
147 """
148 #tools.log("edit category %s %s" % (path, description))
149 authenticate(request, username, password)
150 config = request.getConfiguration()
151
152 path = path.encode('utf-8')
153 description = description.encode('utf-8')
154
155 #check the path, if not existed, make it
156 datadir = config['datadir']
157 if path and path[0] == '/':
158 path = path[1:]
159 p = os.path.join(datadir, path)
160 if not os.path.exists(p):
161 os.makedirs(p)
162 os.chmod(p, 0777)
163
164 #get the category file, if not existed, create it
165 categoryfile = config.get('categoryfile', os.path.join(config['datadir'], 'categoryfile'))
166 if not os.path.exists(categoryfile):
167 f = file(categoryfile, 'w')
168 f.close()
169
170 #get all categories
171 categories = {}
172 lines = file(categoryfile).readlines()
173 for line in lines:
174 line = line.strip()
175 p, des = line.split('=')
176 categories[p] = des
177
178 #add new description
179 categories[path] = description
180
181 #check if description is existed, if existed, then raise exception
182 if categories.values().count(description) > 1:
183 raise NError, "The description has existed!"
184
185 #save category file
186 file(categoryfile, 'w').write('\n'.join(['%s=%s' % (p, d) for p, d in categories.items()]))
187
188 return xmlrpclib.Boolean(True)
189
190 def newedit_putFile(request, username, password, filename, data):
191 """
192 Upload a file to a specified directory
193
194 @param request: the pyblosxom Request instance
195 @type request: Request
196
197 @param username: the username
198 @type username: string
199
200 @param password: the password
201 @type password: string
202
203 @param filename: filename
204 @type filename: string
205
206 @param data: content of the file
207 @type data: binary string
208
209 @returns an xmlrpclib boolean -- true if the edit was successful, false otherwise
210 """
211 tools.log("putfile %s" % filename)
212 authenticate(request, username, password)
213 config = request.getConfiguration()
214
215 filename = os.path.basename(filename.encode('utf-8'))
216
217 dir = getFilePath(request)
218 if dir:
219 f = file(os.path.join(dir, filename), 'wb')
220 f.write(str(data))
221 f.close()
222
223 return xmlrpclib.Boolean(True)
224
225 def newedit_getFileList(request, username, password):
226 """
227 get the file list in filepath directory
228
229 @param request: the pyblosxom Request instance
230 @type request: Request
231
232 @param username: the username
233 @type username: string
234
235 @param password: the password
236 @type password: string
237
238 @returns an xmlrpclib struct list -- each item is a dict ('filename':filename, 'url':url)
239 """
240 #tools.log("getFileList")
241 authenticate(request, username, password)
242 config = request.getConfiguration()
243
244 dir = getFilePath(request)
245 rootdir = config['rootdir']
246
247 #relative path of filepath
248 rdir = dir[len(rootdir)+1:]
249
250 files = os.listdir(dir)
251
252 url = config['start_url']
253 result = []
254 for f in files:
255 path = url + '/' + rdir + '/' + f
256 result.append({'filename':f, 'url':path})
257
258 return result
259
260 def newedit_getSysFile(request, username, password, filename):
261 """
262 get the content of the system file defined in filelist.dat
263
264 @param request: the pyblosxom Request instance
265 @type request: Request
266
267 @param username: the username
268 @type username: string
269
270 @param password: the password
271 @type password: string
272
273 @param filename: the file which you want to get
274 @type filename: string
275
276 @returns an xmlrpclib struct list -- each item is a tuple (filename, url)
277 """
278 #tools.log("getSysFile %s" % filename)
279 authenticate(request, username, password)
280 config = request.getConfiguration()
281
282 if checkSysFile(request, filename):
283 data = xmlrpclib.Binary(file(os.path.join(config['datadir'], filename), 'rb').read())
284 return data
285 return xmlrpclib.Boolean(False)
286
287 def newedit_putSysFile(request, username, password, filename, data):
288 """
289 change the content of the filename in filelist.dat
290
291 @param request: the pyblosxom Request instance
292 @type request: Request
293
294 @param username: the username
295 @type username: string
296
297 @param password: the password
298 @type password: string
299
300 @param filename: the file which you want to get
301 @type filename: string
302
303 @param data: the content of the file
304 @type data: binary
305
306 @returns True or False
307 """
308 #tools.log("putSysFile %s" % filename)
309 authenticate(request, username, password)
310 config = request.getConfiguration()
311
312 if checkSysFile(request, filename):
313 f = file(os.path.join(config['datadir'], filename), 'wb')
314 f.write(str(data))
315 f.close()
316
317 return xmlrpclib.Boolean(True)
318 else:
319 return xmlrpclib.Boolean(False)
320
321 def checkSysFile(request, filename):
322 """
323 Check if the filename can be accessed
324
325 @param request: the pyblosxom Request instance
326 @type request: Request
327
328 @param filename: the filename which you want to access
329 @type filename: string
330
331 @returns True if available or False if disable
332 """
333 config = request.getConfiguration()
334 datadir = config['datadir']
335 filelist = config['newedit_filelist']
336
337 f = os.path.join(datadir, filelist)
338 lines = file(f).readlines()
339 for line in lines:
340 if filename == line.strip():
341 return True
342 return False
343
344 def newedit_getSysFileList(request, username, password):
345 """
346 Get the file list which can be accessed
347
348 @param request: the pyblosxom Request instance
349 @type request: Request
350
351 @param username: the username
352 @type username: string
353
354 @param password: the password
355 @type password: string
356
357 @returns True if available or False if disable
358 """
359 authenticate(request, username, password)
360 config = request.getConfiguration()
361 datadir = config['datadir']
362 filelist = config['newedit_filelist']
363
364 f = os.path.join(datadir, filelist)
365 lines = file(f).readlines()
366 return [line.strip() for line in lines]
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.