Преглед изворни кода

__iter__ for Command+TaskManager instead of get_all()

tags/v0.1^2
Ben Kurtovic пре 12 година
родитељ
комит
60b52f6aeb
4 измењених фајлова са 24 додато и 19 уклоњено
  1. +13
    -4
      earwigbot/commands/__init__.py
  2. +4
    -4
      earwigbot/commands/help.py
  3. +3
    -7
      earwigbot/commands/threads.py
  4. +4
    -4
      earwigbot/tasks/__init__.py

+ 13
- 4
earwigbot/commands/__init__.py Прегледај датотеку

@@ -71,6 +71,8 @@ class BaseCommand(object):

Given a Data() instance, return True if we should respond to this
activity, or False if we should ignore it or it doesn't apply to us.
Be aware that since this is called for each message sent on IRC, it
should not be cheap to execute and unlikely to throw exceptions.

Most commands return True if data.command == self.name, otherwise they
return False. This is the default behavior of check(); you need only
@@ -97,6 +99,10 @@ class CommandManager(object):
self._commands = {}
self._command_access_lock = Lock()

def __iter__(self):
for name in self._commands:
yield name

def _load_command(self, name, path):
"""Load a specific command from a module, identified by name and path.

@@ -148,10 +154,6 @@ class CommandManager(object):
commands = ", ".join(self._commands.keys())
self.logger.info(msg.format(len(self._commands), commands))

def get_all(self):
"""Return our dict of all loaded commands."""
return self._commands

def check(self, hook, data):
"""Given an IRC event, check if there's anything we can respond to."""
with self._command_access_lock:
@@ -164,3 +166,10 @@ class CommandManager(object):
e = "Error executing command '{0}':"
self.logger.exception(e.format(data.command))
break

def get(self, command_name):
"""Return the class instance associated with a certain command name.

Will raise KeyError if the command is not found.
"""
return self._command[command_name]

+ 4
- 4
earwigbot/commands/help.py Прегледај датотеку

@@ -30,7 +30,6 @@ class Command(BaseCommand):
name = "help"

def process(self, data):
self.cmnds = self.bot.commands.get_all()
if not data.args:
self.do_main_help(data)
else:
@@ -39,7 +38,7 @@ class Command(BaseCommand):
def do_main_help(self, data):
"""Give the user a general help message with a list of all commands."""
msg = "Hi, I'm a bot! I have {0} commands loaded: {1}. You can get help for any command with '!help <command>'."
cmnds = sorted(self.cmnds.keys())
cmnds = sorted(self.bot.commands)
msg = msg.format(len(cmnds), ', '.join(cmnds))
self.connection.reply(data, msg)

@@ -53,13 +52,14 @@ class Command(BaseCommand):
dummy.command = command.lower()
dummy.is_command = True

for cmnd in self.cmnds.values():
for cmnd_name in self.bot.commands:
cmnd = self.bot.commands.get(cmnd_name)
if not cmnd.check(dummy):
continue
if cmnd.__doc__:
doc = cmnd.__doc__.replace("\n", "")
doc = re.sub("\s\s+", " ", doc)
msg = "info for command \x0303{0}\x0301: \"{1}\""
msg = "help for command \x0303{0}\x0301: \"{1}\""
self.connection.reply(data, msg.format(command, doc))
return
break


+ 3
- 7
earwigbot/commands/threads.py Прегледај датотеку

@@ -103,13 +103,9 @@ class Command(BaseCommand):
def do_listall(self):
"""With !tasks listall or !tasks all, list all loaded tasks, and report
whether they are currently running or idle."""
all_tasks = self.bot.tasks.get_all().keys()
threads = threading.enumerate()
tasklist = []

all_tasks.sort()

for task in all_tasks:
for task in sorted(self.bot.tasks):
threadlist = [t for t in threads if t.name.startswith(task)]
ids = [str(t.ident) for t in threadlist]
if not ids:
@@ -144,9 +140,9 @@ class Command(BaseCommand):
self.connection.reply(data, msg)
return

if task_name not in self.bot.tasks.get_all().keys():
if task_name not in self.bot.tasks:
# This task does not exist or hasn't been loaded:
msg = "task could not be found; either tasks/{0}.py doesn't exist, or it wasn't loaded correctly."
msg = "task could not be found; either it doesn't exist, or it wasn't loaded correctly."
self.connection.reply(data, msg.format(task_name))
return



+ 4
- 4
earwigbot/tasks/__init__.py Прегледај датотеку

@@ -139,6 +139,10 @@ class TaskManager(object):
self._tasks = {}
self._task_access_lock = Lock()

def __iter__(self):
for name in self._tasks:
yield name

def _wrapper(self, task, **kwargs):
"""Wrapper for task classes: run the task and catch any errors."""
try:
@@ -239,7 +243,3 @@ class TaskManager(object):
Will raise KeyError if the task is not found.
"""
return self._tasks[task_name]

def get_all(self):
"""Return our dict of all loaded tasks."""
return self._tasks

Loading…
Откажи
Сачувај