A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

tasks.py 4.8 KiB

há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
há 13 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # -*- coding: utf-8 -*-
  2. # Manage wiki tasks from IRC, and check on thread status.
  3. import threading
  4. import re
  5. from irc.classes import BaseCommand, Data, KwargParseException
  6. from wiki import task_manager
  7. from core import config
  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 config.irc["permissions"]["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] in ["listall", "all"]:
  33. self.do_listall()
  34. else: # they asked us to do something we don't know
  35. self.connection.reply(data, "unknown argument: \x0303{0}\x0301.".format(data.args[0]))
  36. def do_list(self):
  37. threads = threading.enumerate()
  38. normal_threads = []
  39. task_threads = []
  40. for thread in threads:
  41. tname = thread.name
  42. if tname == "MainThread":
  43. tname = self.get_main_thread_name()
  44. normal_threads.append("\x0302{0}\x0301 (as main thread, id {1})".format(tname, thread.ident))
  45. elif tname in ["irc-frontend", "irc-watcher", "wiki-scheduler"]:
  46. normal_threads.append("\x0302{0}\x0301 (id {1})".format(tname, thread.ident))
  47. elif tname.startswith("reminder"):
  48. normal_threads.append("\x0302reminder\x0301 (until {0})".format(tname.replace("reminder ", "")))
  49. else:
  50. tname, start_time = re.findall("^(.*?) \((.*?)\)$", tname)[0]
  51. task_threads.append("\x0302{0}\x0301 (id {1}, since {2})".format(tname, thread.ident, start_time))
  52. if task_threads:
  53. msg = "\x02{0}\x0F threads active: {1}, and \x02{2}\x0F task threads: {3}.".format(len(threads), ', '.join(normal_threads), len(task_threads), ', '.join(task_threads))
  54. else:
  55. msg = "\x02{0}\x0F threads active: {1}, and \x020\x0F task threads.".format(len(threads), ', '.join(normal_threads))
  56. self.connection.reply(self.data, msg)
  57. def do_listall(self):
  58. tasks = task_manager.task_list.keys()
  59. threads = threading.enumerate()
  60. tasklist = []
  61. tasks.sort()
  62. for task in tasks:
  63. threads_running_task = [t for t in threads if t.name.startswith(task)]
  64. ids = map(lambda t: str(t.ident), threads_running_task)
  65. if not ids:
  66. tasklist.append("\x0302{0}\x0301 (idle)".format(task))
  67. elif len(ids) == 1:
  68. tasklist.append("\x0302{0}\x0301 (\x02active\x0F as id {1})".format(task, ids[0]))
  69. else:
  70. tasklist.append("\x0302{0}\x0301 (\x02active\x0F as ids {1})".format(task, ', '.join(ids)))
  71. tasklist = ", ".join(tasklist)
  72. msg = "{0} tasks loaded: {1}.".format(len(tasks), tasklist)
  73. self.connection.reply(self.data, msg)
  74. def do_start(self):
  75. data = self.data
  76. try:
  77. task_name = data.args[1]
  78. except IndexError: # no task name given
  79. self.connection.reply(data, "what task do you want me to start?")
  80. return
  81. try:
  82. data.parse_kwargs()
  83. except KwargParseException, arg:
  84. self.connection.reply(data, "error parsing argument: \x0303{0}\x0301.".format(arg))
  85. return
  86. if task_name not in task_manager.task_list.keys(): # this task does not exist or hasn't been loaded
  87. self.connection.reply(data, "task could not be found; either wiki/tasks/{0}.py doesn't exist, or it wasn't loaded correctly.".format(task_name))
  88. return
  89. task_manager.start_task(task_name, **data.kwargs)
  90. self.connection.reply(data, "task \x0302{0}\x0301 started.".format(task_name))
  91. def get_main_thread_name(self):
  92. """Return the "proper" name of the MainThread; e.g. "irc-frontend" or "irc-watcher"."""
  93. if "irc_frontend" in config.components:
  94. return "irc-frontend"
  95. elif "wiki_schedule" in config.components:
  96. return "wiki-scheduler"
  97. else:
  98. return "irc-watcher"