~ubuntu-branches/ubuntu/quantal/gozerbot/quantal

« back to all changes in this revision

Viewing changes to build/lib/gozerbot/plugs/plug.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2010-09-29 18:20:02 UTC
  • mfrom: (3.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100929182002-gi532gnem1vlu6jy
Tags: 0.9.1.3-5
Added python2.5 build dependency. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# gozerbot/plugs/plug.py
 
2
#
 
3
#
 
4
 
 
5
# gozerbot imports
 
6
from gozerbot.commands import cmnds
 
7
from gozerbot.examples import examples
 
8
from gozerbot.plugins import plugins
 
9
from gozerbot.utils.exception import exceptionmsg, handle_exception
 
10
from gozerbot.gozerimport import gozer_import 
 
11
from gozerbot.tests import tests
 
12
 
 
13
def handle_plugenable(bot, ievent):
 
14
 
 
15
    """ enable and reload a plugin. """
 
16
 
 
17
    doall = False
 
18
 
 
19
    if '-a' in ievent.optionset:
 
20
        doall = True
 
21
 
 
22
    if not ievent.rest and not doall:
 
23
        ievent.missing('<plugname>')
 
24
        return
 
25
 
 
26
    try:
 
27
        plugs = gozer_import('gozerplugs').__plugs__
 
28
    except ImportError:
 
29
        ievent.reply("no gozerplugs package detected")
 
30
        return
 
31
 
 
32
    if not doall:
 
33
        plugs = ievent.rest.split()
 
34
 
 
35
    ievent.reply("trying to reload: ", plugs, dot=True)
 
36
    errors = []
 
37
    reloaded = []
 
38
    failed = []
 
39
 
 
40
    for plug in plugs:
 
41
        try:
 
42
            reloaded.extend(plugins.reload('gozerplugs', plug))
 
43
        except ImportError, ex:
 
44
            errors.append(str(ex))
 
45
            failed.append(plug)
 
46
        except Exception, ex:
 
47
            handle_exception()
 
48
            errors.append(exceptionmsg())
 
49
            failed.append(plug)
 
50
 
 
51
    ievent.reply('enabled plugins: ', reloaded, dot=True)
 
52
 
 
53
    if failed:
 
54
        ievent.reply('failed to reload: ', failed, dot=True)
 
55
 
 
56
    if errors:
 
57
        ievent.reply('errors: ', errors, dot=True)
 
58
  
 
59
cmnds.add('plug-enable', handle_plugenable, 'OPER', options={'-a': ''}, threaded=True)
 
60
examples.add('plug-enable', 'enable a plugin', 'plug-enable karma')
 
61
tests.add('plug-enable country', 'country')
 
62
 
 
63
def handle_plugdisable(bot, ievent):
 
64
 
 
65
    """ disable and unload a plugin. """
 
66
 
 
67
    if not ievent.rest:
 
68
        ievent.missing('<plugname>')
 
69
        return
 
70
 
 
71
    plugs = ievent.rest.split()
 
72
    disabled = []
 
73
 
 
74
    for plug in plugs:
 
75
        plugins.unload(plug)
 
76
        plugins.disable(plug)
 
77
        disabled.append(plug)
 
78
 
 
79
    ievent.reply('disabled plugins: ', disabled)
 
80
 
 
81
cmnds.add('plug-disable', handle_plugdisable, 'OPER')
 
82
examples.add('plug-disable', 'disable a plugin', 'plug-disable karma')
 
83
tests.add('plug-disable country', 'country')
 
84
 
 
85
def handle_plugupgrade(bot, ievent):
 
86
 
 
87
    """ upgrade a plugin(s). """
 
88
 
 
89
    alreadygot = []
 
90
    upgraded = []
 
91
    plugs = []
 
92
 
 
93
    if ievent.rest:
 
94
        plugs.extend(ievent.rest.split())
 
95
    else:
 
96
        for name, plug in plugins.plugs.iteritems():
 
97
            if hasattr(plug, 'upgrade'):
 
98
                plugs.append(name)    
 
99
 
 
100
    ievent.reply('starting plugin upgrade for plugins: ', plugs, dot=True)
 
101
    errors = []
 
102
 
 
103
    for plug in plugs:
 
104
        if not plugins.plugs.has_key(plug):
 
105
            plugins.reload('gozerplugs', plug, enable=False)
 
106
        
 
107
        try:
 
108
            s = plugins.plugs[plug].size()
 
109
        except AttributeError:
 
110
            s = 0
 
111
 
 
112
        if s and not '-f' in ievent.optionset:
 
113
            alreadygot.append(plug)
 
114
            continue
 
115
 
 
116
        try:
 
117
            plugins.plugs[plug].upgrade()
 
118
            plugins.reload('gozerplugs', plug)
 
119
            ievent.reply('upgraded %s' % plug)
 
120
            upgraded.append(plug)
 
121
        except AttributeError:
 
122
            continue
 
123
        except Exception, ex:
 
124
            handle_exception()
 
125
            errors.append(exceptionmsg())
 
126
 
 
127
    if upgraded:
 
128
        ievent.reply('upgraded the following plugins: ', upgraded, dot=True)
 
129
 
 
130
    if alreadygot:
 
131
        ievent.reply("%s plugins already have data .. use -f to force upgrade" % ' .. '.join(alreadygot))
 
132
 
 
133
    if errors:
 
134
        ievent.reply("errors: ", errors, dot=True)
 
135
 
 
136
cmnds.add('plug-upgrade', handle_plugupgrade, 'OPER', options={'-f': ''}, threaded=True)
 
137
examples.add('plug-upgrade', 'plug-upgrade {<list of plugs>] .. upgrade all plugins or a specified plugin', 'plug-upgrade url')
 
138
tests.add('plug-upgrade url', 'url')