~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to gozerbot/plugs/reload.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2009-09-14 09:00:29 UTC
  • mfrom: (1.1.4 upstream) (3.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20090914090029-uval0ekt72kmklxw
Tags: 0.9.1.3-3
Changed dependency on python-setuptools to python-pkg-resources
(Closes: #546435) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/plugs/reload.py
 
2
#
 
3
#
 
4
 
 
5
__copyright__ = 'this file is in the public domain'
 
6
 
 
7
# gozerbot imports
 
8
from gozerbot.config import config
 
9
from gozerbot.plugins import plugins
 
10
from gozerbot.commands import cmnds
 
11
from gozerbot.examples import examples
 
12
from gozerbot.plughelp import plughelp
 
13
from gozerbot.aliases import aliases
 
14
from gozerbot.gozerimport import gozer_import
 
15
from gozerbot.tests import tests
 
16
 
 
17
# basic imports
 
18
import os, sets
 
19
 
 
20
plughelp.add('reload', 'reload a plugin')
 
21
 
 
22
def handle_reload(bot, ievent):
 
23
 
 
24
    """ reload a plugin. """
 
25
 
 
26
    try:
 
27
        plugs = ievent.args
 
28
    except IndexError:
 
29
        ievent.missing('<list plugins>')
 
30
        return
 
31
 
 
32
    reloaded = []
 
33
    errors = []
 
34
 
 
35
    for plug in plugs:
 
36
        plug = plug.lower()
 
37
 
 
38
        if plug == 'config':
 
39
            config.reload()
 
40
            ievent.reply('config reloaded')
 
41
            continue
 
42
 
 
43
        p = gozer_import('gozerbot.plugs.__init__')
 
44
 
 
45
        if plug in p.__plugs__:
 
46
            reloaded.extend(plugins.reload('gozerbot.plugs', plug))
 
47
            continue
 
48
 
 
49
        try:
 
50
            reloaded.extend(plugins.reload('myplugs', plug))
 
51
        except ImportError, ex:
 
52
            try:
 
53
                reloaded.extend(plugins.reload('gozerplugs', plug))
 
54
            except ImportError, ex:
 
55
                errors.append(str(ex))
 
56
 
 
57
    ievent.reply('reloaded: ', reloaded, dot=True)
 
58
 
 
59
    try:
 
60
       cantreload = list(sets.Set(plugs) - sets.Set(reloaded))
 
61
       if cantreload:
 
62
           ievent.reply("can't reload: " , cantreload, dot=True)
 
63
           if errors:
 
64
               ievent.reply('errors: ', errors)
 
65
    except AttributeError:  # in case of !reload reload
 
66
       pass 
 
67
    
 
68
cmnds.add('reload', handle_reload, 'OPER')
 
69
examples.add('reload', 'reload <plugin>', 'reload core')
 
70
aliases.data['load'] = 'reload'
 
71
tests.add('reload country', 'country').add('unload country')
 
72
 
 
73
def handle_unload(bot, ievent):
 
74
 
 
75
    """ unload a plugin. """
 
76
 
 
77
    try:
 
78
        what = ievent.args[0].lower()
 
79
    except IndexError:
 
80
        ievent.missing('<plugin>')
 
81
        return
 
82
 
 
83
    if not plugins.exist(what):
 
84
        ievent.reply('there is no %s module' % what)
 
85
        return
 
86
 
 
87
    got = plugins.unload(what)
 
88
 
 
89
    for what in got:
 
90
        plugins.disable(what)
 
91
 
 
92
    ievent.reply("unloaded and disabled: ", got, dot=True)
 
93
 
 
94
cmnds.add('unload', handle_unload, 'OPER')
 
95
examples.add('unload', 'unload <plugin>', 'unload relay')
 
96
tests.add('reload country').add('unload country', 'country')