Commit 51bc42684794319cf0feb7ae850a31d5e093a0d3

  • avatar
  • Krister Svanlund <adsum @BIG…SH.(none)>
  • Fri Jul 03 01:17:43 CEST 2009
  • Tree SHA1: f7e91c8
  • Parent SHA1: 3980459 (MediaWiki: Removed old debug commands and implemented real commands for getting tables and lists from arbitrary wiki-pages. Also rewrote some functions to avoid code duplication.)
  • raw diff | raw patch
MediaWiki: Added 'add' command to wiki for adding to list, tables and sections. Also for adding tables and lists.
ChangeLog
(4 / 0)
  
112009-07-02 Krister Svanlund <krister.svanlund@gmail.com>
22
33 * kofoo_mediawiki.py (do_wiki_get_list): Added command for retrieving lists to IRC.
4 (do_wiki_get_section): Added command for retrieving sections to IRC.
5 (mw_add_to_page): Rewrote mw_add_to_page because earlier version did not add text
6 correctly.
7 (do_wiki): Implemented proper commands for adding things to lists and sections.
48
59 * kofoo.py (KoFoo.handle_command): Fixed bug causing crash on empty string passed
610 to handle_command.
  
8686 if args:
8787 if args[0] == 'get' and args[1:]:
8888 ### wiki get table|list|section "itemname" on "pagename" ###
89 get_pat = re.compile(r'("?)(.*?) on ("?)(\S+)\3
89 get_pat = re.compile(r'([\"\']?)(.*?) (on|from) ([\"\']?)(\S+)\4
9090 if args[1] == 'table':
9191 m = get_pat.match(' '.join(args[2:]))
9292 if m:
9393 tablename = m.group(2)
94 pagename = m.group(4)
94 pagename = m.group(5)
9595 return do_wiki_get_table(bot, server, sender, target, pagename, tablename)
9696 elif args[1] == 'list':
9797 m = get_pat.match(' '.join(args[2:]))
9898 if m:
9999 listname = m.group(2)
100 pagename = m.group(4)
100 pagename = m.group(5)
101101 return do_wiki_get_list(bot, server, sender, target, pagename, listname)
102102 elif args[1] == 'section':
103103 m = get_pat.match(' '.join(args[2:]))
104104 if m:
105105 sectionname = m.group(2)
106 pagename = m.group(4)
106 pagename = m.group(5)
107 return do_wiki_get_section(bot, server, sender, target, pagename, sectionname)
107108 else:
108109 bot.respond(server, sender, target, "\x02%s\x02 is not a valid argument." % args[1])
109110 return False
111 elif args[0] == 'add' and args[1:]:
112 if args[1] == 'section':
113 return do_wiki_add_section(bot, server, sender, target, args[2:])
114 elif args[1] == 'table' and args[2:]:
115 return do_wiki_add_table(bot, server, sender, target, args[2:])
116 elif args[1] == 'to' and args[2:]:
117 if args[2] == 'list' and args[3:]:
118 return do_wiki_add_to_list(bot, server, sender, target, args[3:])
119 elif args[2] == 'table' and args[3:]:
120 return do_wiki_add_to_table(bot, server, sender, target, args[3:])
121 elif args[2] == 'section' and args[3:]:
122 return do_wiki_add_to_section(bot, server, sender, target, args[3:])
123 elif args[2] == 'page' and args[3:]:
124 return do_wiki_add_to_page(bot, server, sender, target, args[3:])
125 else:
126 bot.respond(server, sender, target, "\x02%s\x02 is not a valid argument or not enough arguments." % args[2])
127 return False
128 else:
129 bot.respond(server, sender, target, "\x02%s\x02 is not a valid argument or not enough arguments." % args[1])
130 return False
110131 else:
111132 bot.respond(server, sender, target, "\x02%s\x02 is not a valid argument or not enough arguments." % args[0])
112133 return False
139139#
140140####
141141
142def do_wiki_add_table(bot, server, sender, target, args):
143 add_table_pat = re.compile(r'([\"\']?)(.*?)\1 in ([\"\']?)(.*?)\3 on ([\"\']?)(\S+)\5[:;] ([\"\']?)(.+)\7(\sat top|)')
144 args = ' '.join(args)
145 print args
146 m = add_table_pat.match(args)
147 if m:
148 print m.groups()
149 tablename = m.group(2)
150 sectionname = m.group(4)
151 pagename = m.group(6)
152 header_row = '"%s"' % m.group(8)
153 at_top = (len(m.group(9)) > 0)
154 print " + At top: %s %s" % (str(at_top), m.group(8))
155 headers = []
156 print " + Create table '%s' on '%s'." % (tablename, pagename)
157 for m in re.finditer(r'\s*"(.*?)"', header_row):
158 headers.append(m.group(1))
159 if headers:
160 if mw_add_table(pagename, tablename, sectionname, headers, not at_top):
161 bot.respond(server, sender, target, "Successfully added table.")
162 else:
163 bot.respond(server, sender, target, "Could not add table to page.")
164 return True
165 else:
166 bot.respond(server, sender, target, "No headers specified.")
167 return False
168 else:
169 bot.respond(server, sender, target, "Could not parse arguments.")
170 return False
171
172def do_wiki_add_to_table(bot, server, sender, target, args):
173 add_to_table_pat = re.compile(r'([\"\']?)(.*?)\1 on ([\"\']?)(\S+)\3[:;] (.+)(\sat top|)')
174 m = add_to_table_pat.match(' '.join(args))
175 if m:
176 tablename = m.group(2)
177 pagename = m.group(4)
178 new_row = m.group(5)
179 at_top = len(m.group(6)) > 0
180 row = []
181 print " + To table '%s' on '%s'." % (tablename, pagename)
182 for m in re.finditer(r'\s*"(.*?)"', new_row):
183 row.append(m.group(1))
184 if row:
185 print " + Add: \"%s\"" % '", "'.join(row)
186 if mw_add_to_table(pagename, tablename, [row], not at_top):
187 print " + Added row to table."
188 else:
189 print " - Failed to add row."
190 return True
191 else:
192 bot.respond(server, sender, target, "Nothing to add.")
193 return False
194 else:
195 bot.respond(server, sender, target, "Could not parse arguments.")
196 return False
197
198def do_wiki_add_to_list(bot, server, sender, target, args):
199 add_to_list_pat = re.compile(r'([\"\']?)(.*?)\1 on ([\"\']?)(\S+)\3[:;] ([\"\']?)(.*?)\5(\sat top|)')
200 m = add_to_list_pat.match(' '.join(args))
201 if m:
202 listname = m.group(2)
203 pagename = m.group(4)
204 new_item = m.group(6)
205 at_top = len(m.group(7)) > 0
206 level = 1
207 return mw_add_to_list(pagename, listname, new_item, level, not at_top)
208 else:
209 bot.respond(server, sender, target, "Could not parse arguments.")
210 return False
211
212def do_wiki_add_to_page(bot, server, sender, target, args):
213 add_to_page_pat = re.compile(r'([\"\']?)(\S+)\1[:;] ([\"\']?)(.*?)\3(\sat top|)')
214 m = add_to_page_pat.match(' '.join(args))
215 if m:
216 pagename = m.group(2)
217 text_to_add = m.group(4)
218 at_top = len(m.group(5)) > 0
219 return mw_add_to_page(pagename, "", text_to_add, "Text added by bot.", not at_top)
220 else:
221 bot.respond(server, sender, target, "Could not parse arguments.")
222 return False
223
224def do_wiki_add_to_section(bot, server, sender, target, args):
225 add_to_section_pat = re.compile(r'([\"\']?)(.*?)\1 on ([\"\']?)(\S+)\3[:;] ([\"\']?)(.*?)\5(\sat top|)')
226 m = add_to_section_pat.match(' '.join(args))
227 if m:
228 sectionname = m.group(2)
229 pagename = m.group(4)
230 text_to_add = m.group(6)
231 at_top = len(m.group(7)) > 0
232 return mw_add_to_page(pagename, sectionname, text_to_add, "Text added by bot.", not at_top)
233 else:
234 bot.respond(server, sender, target, "Could not parse arguments.")
235 return False
236
237def do_wiki_add_section(bot, server, sender, target, args):
238 add_section_pat = re.compile(r'([\"\']?)(.*?)\1 (to|on) ([\"\']?)(\S+)\4(\sat top|)')
239 add_section_level_pat = re.compile(r'([\"\']?)(.*?)\1 level ([0-9]+) (to|on) ([\"\']?)(\S+)\5(\sat top|)')
240 add_section_parent_pat = re.compile(r'([\"\']?)(.*?)\1 below ([\"\']?)(.*?)\3 (to|on) ([\"\']?)(\S+)\6(\sat top|)')
241 m = add_section_parent_pat.match(' '.join(args))
242 if m:
243 sectionname = m.group(2)
244 sectionlevel = 1
245 sectionparent = m.group(4)
246 pagename = m.group(7)
247 at_top = (len(m.group(8)) > 0)
248 else:
249 m = add_section_level_pat.match(' '.join(args))
250 if m:
251 sectionname = m.group(2)
252 try:
253 sectionlevel = int(m.group(3))
254 except TypeError:
255 sectionlevel = 1
256 sectionparent = ""
257 pagename = m.group(6)
258 at_top = (len(m.group(7)) > 0)
259 else:
260 m = add_section_pat.match(' '.join(args))
261 if m:
262 sectionname = m.group(2)
263 sectionlevel = 1
264 sectionparent = ""
265 pagename = m.group(5)
266 at_top = (len(m.group(6)) > 0)
267 print " + At top: %s" % at_top
268 else:
269 bot.respond(server, sender, target, "Could not parse arguments.")
270 return False
271 if sectionparent:
272 print " + Add section '%s' below %s on '%s'." % (sectionname, sectionparent, pagename)
273 else:
274 print " + Add section '%s' with level %d on '%s'." % (sectionname, sectionlevel, pagename)
275 success = mw_add_section(pagename, sectionname, sectionlevel, sectionparent, not at_top)
276 if success:
277 bot.respond(server, sender, target, "New section created on '%s'." % pagename)
278 else:
279 bot.respond(server, sender, target, "Could not create section.")
280 return True
281
142282def do_wiki_get_table(bot, server, sender, target, pagename, tablename):
143283 print "Get '%s' from '%s'." % (tablename, pagename)
144284 table = mw_get_table(pagename, tablename)
287287 table['title'] = mw_rewrite_wiki_links(table.get('title', ""))
288288 table['content'] = [[mw_rewrite_wiki_links(s) for s in r] for r in table.get('content', [])]
289289 table['headers'] = [mw_rewrite_wiki_links(s) for s in table.get('headers', [])]
290 ret_list = ["\x02\x1F%s\x1F\x02" % (table.get('title', ''))]
290 ret_list = ['\x02\x1F%s\x1F\x02' % table.get('title', '')]
291291 width = {}
292292 for i in xrange(len(table.get('headers', []))):
293293 width[i] = len(table['headers'][i])
297297 #width = max([max([len(s) for s in r]) for r in table.get('content', [])])
298298 headers = ""
299299 for i, header in zip(xrange(len(table.get('headers', []))), table.get('headers', [])):
300 headers += " \x02{0:{1}}\x02 ".format(header.encode("latin-1"), width.get(i, 0))
300 headers += " \x02{0:^{1}}\x02 ".format(header.encode("latin-1"), width.get(i, 0))
301301 ret_list.append(headers.decode("latin-1"))
302302 for row in table.get('content', []):
303303 new_row = ""
305305 new_row += " {0:{1}} ".format(item.encode("latin-1"), width.get(i, 0))
306306 ret_list.append(new_row.decode("latin-1"))
307307 for s in ret_list:
308 bot.respond(server, sender, target, bot.convert_to_irc_string(s.strip()))
308 bot.respond(server, sender, target, bot.convert_to_irc_string(s))
309309 bot.respond(server, sender, target, "End of table.")
310310 print " + Printed all rows to IRC."
311311 return True
329329 bot.respond(server, sender, target, "No list or empty list found.")
330330 return True
331331
332def do_wiki_get_section(bot, server, sender, target, pagename, sectionname):
333 print "Get '%s' from '%s'." % (sectionname, pagename)
334 section = mw_get_section(pagename, sectionname)
335 if section:
336 print " + Got section."
337 bot.respond(server, sender, target, "Section \x02'%s\x02:" % sectionname)
338 for s in section.splitlines():
339 s = ' '+mw_rewrite_wiki_links(s)
340 bot.respond(server, sender, target, bot.convert_to_irc_string(s))
341 bot.respond(server, sender, target, "End of section.")
342 return True
343 else:
344 bot.respond(server, sender, target, "No section with that name.")
345 return True
346
332347#####
333348# Helper functions after this point
334349##
351351exp_end_of_section = r'(\n\s*?=+\s*%s\s*=+\s*\n.*)(\n+\s*=+)'
352352exp_start_of_section = r'(\n\s*?=+\s*%s\s*=+\s*)(\n.*?\n)'
353353
354def mw_add_to_page(pagename, section, add_text, summary, at_end = True):
355 print "Add '%s' to page %s below '%s'." % (add_text, pagename, section)
356 if pagename and add_text:
357 page = wiki_site.Pages[pagename]
358 text = page.edit()
359 new_text, subcount = re.subn((exp_start_of_section, exp_end_of_section)[at_end] % section, r'\1\n%s\2' % add_text, text)
360 if subcount == 0:
361 print "Failed to substitute."
354def mw_add_to_page(pagename, parentsection, text_to_add, summary, at_end):
355 print "Add text to '%s'." % text_to_add
356 page = wiki_site.Pages[pagename]
357 text = page.edit()
358 if parentsection:
359 ### Get the section, add the text and replace the old section with the new section. ###
360 section = mw_get_section(pagename, parentsection, text)
361 if section:
362362 if at_end:
363 new_text, subcount = re.subn(r'(\n+\s*=+)', r'\n%s\1' % add_text, text, 1)
364 if subcount > 0:
365 print "Before first title."
366 else:
367 if at_end:
368 new_text = text + "\n%s" % add_text
369 else:
370 new_text = "%s\n%s" % (add_text, text)
371 print "Added after text."
363 print " + Add text at end of section '%s'." % section
364 new_section = section+'\n'+text_to_add
372365 else:
373 new_text = "%s\n%s" % (add_text, text)
374 try:
375 page.save(new_text, summary=summary)
376 print " + Added text to %s." % pagename
377 return True
378 except Exception, e:
379 print " - Failed to save new wiki page: %s" % str(e)
380 return False
366 print " + Add text at start of section '%s'." % section
367 new_section = []
368 added_lines = False
369 for line in section.splitlines():
370 if not added_lines and line:
371 m = re.match(r'\s*(=+)(.*?)\1', line)
372 if not m:
373 print " + Add text."
374 new_section.append(text_to_add)
375 added_lines = True
376 else:
377 print " + Found a title: %s" % m.group(2)
378 new_section.append(line)
379 new_section = '\n'.join(new_section)
380 if new_section:
381 text = text.replace(section, new_section)
382 print " + Replace the old section with the new section."
381383 else:
382 print " - Not enough arguments."
384 if at_end:
385 print " + Add text to end of page."
386 new_text = text+'\n'+text_to_add
387 else:
388 print " + Add text before first header in text."
389 new_text = []
390 added_lines = False
391 for line in text.splitlines():
392 if not added_lines and line:
393 m = re.match(r'\s*(=+)(.*?)\1', line)
394 if m:
395 print " + Found a title: '%s'" % m.group(2)
396 new_text.append(text_to_add)
397 added_lines = True
398 new_text.append(line)
399 if not added_lines:
400 print " + Text has not been added so add at end of page."
401 new_text.append(text_to_add)
402 new_text = '\n'.join(new_text)
403 if new_text:
404 text = new_text
405 try:
406 page.save(text, summary=summary)
407 print " + Added text to '%s'." % pagename
408 return True
409 except Exception, e:
410 print " - Failed to save new wiki page: %s" % str(e)
383411 return False
384412
385def mw_add_section(pagename, section, parent = ""):
413def mw_add_section(pagename, section, sectionlevel = 1, parent = "", to_bottom = True):
386414 print "Add section '%s' to page '%s'." % (section, pagename)
387415 if pagename and section:
388416 if parent:
420420 if parent and not parent_level:
421421 print " - Failed to find parent level."
422422 return False
423 section_level = parent_level + 1
424 if mw_add_to_page(pagename, parent, "%s %s %s" % ('='*section_level, section, '='*section_level), "Section added by bot.", True):
423 section_level = max(parent_level + 1, sectionlevel)
424 if mw_add_to_page(pagename, parent, "%s %s %s" % ('='*section_level, section, '='*section_level), "Section added by bot.", to_bottom):
425425 print " + Section added."
426426 return True
427427 else:
440440 if type(new_rows) is not list:
441441 new_rows = [new_rows]
442442 for row in new_rows:
443 if type(row) is not tuple:
444 row = (row,)
443 if type(row) is not list:
444 row = [row]
445445 rows += '| %s \n' % ' || '.join(row)
446446 rows = '\n|-\n'.join(rows.splitlines())
447447 for table in re.finditer(r'\{\|.*?\|\}', text, re.S):
450450 print " + Has right name."
451451 else:
452452 print " - Not the right name."
453 return False
453 continue
454454 new_table = []
455455 row_added = False
456456 has_header = False
490490 print " - Not enough arguments."
491491 return False
492492
493def create_table(tablename, headers):
494 table = """\
495{| style="width:100%%"
496|+ '''%s'''
497! %s
498|-
499|}""" % (tablename, ' || '.join(headers))
500 return table
501
502def mw_add_table(pagename, tablename, sectionname, headers, to_bottom):
503 table = create_table(tablename, headers)
504 print table
505 print " + At end: %s" % str(to_bottom)
506 return mw_add_to_page(pagename, sectionname, table, "Table '%s' added by bot." % tablename, to_bottom)
507
493508def mw_add_to_list(pagename, listname, new_points, level = 1, to_bottom = True):
494509 print "Add point to list '%s' on page '%s'." % (listname, pagename)
495510 if pagename and listname:
537537 inside_list = False
538538 print " + Add point to bottom of list."
539539 print " + Add: '%s'." % ', '.join(new_points)
540 new_section.append(line)
541540 new_section.extend([' '*indent+p for p in points])
541 new_section.append(line)
542542 added_points = True
543543 else:
544544 new_section.append(line)
717717 text = text.replace(outer.group(0), ' '.join(matches[1:]))
718718 else:
719719 text = text.replace(outer.group(0), matches[0])
720 print outer.group(1)
721720 return text