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 5.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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", "task", "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. """With !tasks list (or abbreviation !tasklist), list all running
  38. threads. This includes the main threads, like the irc frontend and the
  39. watcher, and task threads."""
  40. threads = threading.enumerate()
  41. normal_threads = []
  42. task_threads = []
  43. for thread in threads:
  44. tname = thread.name
  45. if tname == "MainThread":
  46. tname = self.get_main_thread_name()
  47. normal_threads.append("\x0302{0}\x0301 (as main thread, id {1})".format(tname, thread.ident))
  48. elif tname in ["irc-frontend", "irc-watcher", "wiki-scheduler"]:
  49. normal_threads.append("\x0302{0}\x0301 (id {1})".format(tname, thread.ident))
  50. elif tname.startswith("reminder"):
  51. normal_threads.append("\x0302reminder\x0301 (until {0})".format(tname.replace("reminder ", "")))
  52. else:
  53. tname, start_time = re.findall("^(.*?) \((.*?)\)$", tname)[0]
  54. task_threads.append("\x0302{0}\x0301 (id {1}, since {2})".format(tname, thread.ident, start_time))
  55. if task_threads:
  56. 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))
  57. else:
  58. msg = "\x02{0}\x0F threads active: {1}, and \x020\x0F task threads.".format(len(threads), ', '.join(normal_threads))
  59. self.connection.reply(self.data, msg)
  60. def do_listall(self):
  61. """With !tasks listall or !tasks all, list all loaded tasks, and report
  62. whether they are currently running or idle."""
  63. tasks = task_manager.task_list.keys()
  64. threads = threading.enumerate()
  65. tasklist = []
  66. tasks.sort()
  67. for task in tasks:
  68. threads_running_task = [t for t in threads if t.name.startswith(task)]
  69. ids = map(lambda t: str(t.ident), threads_running_task)
  70. if not ids:
  71. tasklist.append("\x0302{0}\x0301 (idle)".format(task))
  72. elif len(ids) == 1:
  73. tasklist.append("\x0302{0}\x0301 (\x02active\x0F as id {1})".format(task, ids[0]))
  74. else:
  75. tasklist.append("\x0302{0}\x0301 (\x02active\x0F as ids {1})".format(task, ', '.join(ids)))
  76. tasklist = ", ".join(tasklist)
  77. msg = "{0} tasks loaded: {1}.".format(len(tasks), tasklist)
  78. self.connection.reply(self.data, msg)
  79. def do_start(self):
  80. """With !tasks start, start any loaded task by name with or without
  81. kwargs."""
  82. data = self.data
  83. try:
  84. task_name = data.args[1]
  85. except IndexError: # no task name given
  86. self.connection.reply(data, "what task do you want me to start?")
  87. return
  88. try:
  89. data.parse_kwargs()
  90. except KwargParseException, arg:
  91. self.connection.reply(data, "error parsing argument: \x0303{0}\x0301.".format(arg))
  92. return
  93. if task_name not in task_manager.task_list.keys(): # this task does not exist or hasn't been loaded
  94. 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))
  95. return
  96. task_manager.start_task(task_name, **data.kwargs)
  97. self.connection.reply(data, "task \x0302{0}\x0301 started.".format(task_name))
  98. def get_main_thread_name(self):
  99. """Return the "proper" name of the MainThread; e.g. "irc-frontend" or
  100. "irc-watcher"."""
  101. if "irc_frontend" in config.components:
  102. return "irc-frontend"
  103. elif "wiki_schedule" in config.components:
  104. return "wiki-scheduler"
  105. else:
  106. return "irc-watcher"