A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

tasks.py 3.2 KiB

13 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # -*- coding: utf-8 -*-
  2. # Manage wiki tasks from IRC, and check on thread status.
  3. import threading
  4. from irc.base_command import BaseCommand
  5. from wiki import task_manager
  6. from config.main import *
  7. from config.irc import *
  8. class Tasks(BaseCommand):
  9. def get_hooks(self):
  10. return ["msg"]
  11. def get_help(self, command):
  12. return "Manage wiki tasks from IRC, and check on thread status."
  13. def check(self, data):
  14. if data.is_command and data.command in ["tasks", "threads", "tasklist"]:
  15. return True
  16. return False
  17. def process(self, data):
  18. self.data = data
  19. if data.host not in OWNERS:
  20. self.connection.reply(data, "at this time, you must be a bot owner to use this command.")
  21. return
  22. if not data.args:
  23. if data.command == "tasklist":
  24. self.do_list()
  25. else:
  26. self.connection.reply(data, "no arguments provided. Maybe you wanted '!{cmnd} list', '!{cmnd} start', or '!{cmnd} listall'?".format(cmnd=data.command))
  27. return
  28. if data.args[0] == "list":
  29. self.do_list()
  30. elif data.args[0] == "start":
  31. self.do_start()
  32. elif data.args[0] == "listall":
  33. self.do_listall()
  34. else: # they asked us to do something we don't know
  35. self.connection.reply(data, "unknown argument: \x0303{}\x0301.".format(data.args[0]))
  36. def do_list(self):
  37. threads = threading.enumerate()
  38. normal_threads = []
  39. task_threads = []
  40. task_thread_num = 0
  41. for thread in threads:
  42. tname = thread.name
  43. if tname == "MainThread":
  44. tname = self.get_main_thread_name()
  45. normal_threads.append("{} (running as main thread)".format(tname))
  46. elif tname in ["irc-frontend", "irc-watcher", "wiki-scheduler"]:
  47. normal_threads.append(tname)
  48. else:
  49. task_thread_num += 1
  50. task_threads.append(tname)
  51. if task_threads:
  52. msg = "\x0302{}\x0301 threads active: {} and \x0302{}\x0301 tasks: {}.".format(len(threads), ', '.join(normal_threads), task_thread_num, ', '.join(task_threads))
  53. else:
  54. msg = "\x0302{}\x0301 threads active: {} and \x03020\x0301 tasks.".format(len(threads), ', '.join(normal_threads))
  55. self.connection.reply(self.data, msg)
  56. def do_listall(self):
  57. tasks = task_manager.task_list.keys()
  58. self.connection.reply(self.data, ', '.join(tasks))
  59. def do_start(self):
  60. kwargs = {}
  61. try:
  62. task_manager.start_task(self.data.args[1], **kwargs)
  63. except IndexError: # no task name given
  64. self.connection.reply(self.data, "what task do you want me to start?")
  65. else:
  66. self.connection.reply(self.data, "task '{}' started.".format(self.data.args[1]))
  67. def get_main_thread_name(self):
  68. """Return the "proper" name of the MainThread; e.g. "irc-frontend" or "irc-watcher"."""
  69. if enable_irc_frontend:
  70. return "irc-frontend"
  71. elif enable_wiki_schedule:
  72. return "wiki-scheduler"
  73. else:
  74. return "irc-watcher"