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

« back to all changes in this revision

Viewing changes to debian/gozerbot/usr/lib/python2.5/site-packages/gozerplugs/plugs/job.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
 
# plugs/job.py
2
 
#
3
 
# (c) Wijnand 'tehmaze' Modderman - http://tehmaze.com
4
 
# BSD License
5
 
 
6
 
__author__ = "Wijnand 'tehmaze' Modderman - http://tehmaze.com"
7
 
__license__ = "BSD"
8
 
 
9
 
from gozerbot.aliases import aliases
10
 
from gozerbot.commands import cmnds
11
 
from gozerbot.periodical import periodical, at
12
 
from gozerbot.plugins import plugins
13
 
from gozerbot.examples import examples
14
 
from gozerbot.plughelp import plughelp
15
 
from gozerbot.generic import uniqlist
16
 
import copy
17
 
import datetime
18
 
import time
19
 
import types
20
 
 
21
 
plughelp.add('job', 'job management')
22
 
 
23
 
def size():
24
 
    return len(periodical.jobs)
25
 
 
26
 
def handle_job(bot, ievent):
27
 
    """ show data of <jobid> """
28
 
    if not ievent.args or not ievent.args[0].isdigit():
29
 
        ievent.reply('<job id>')
30
 
        return
31
 
    for job in periodical.jobs:
32
 
        if job.id() == int(ievent.args[0]):
33
 
            next = job.next
34
 
            if type(next) in [types.FloatType, types.IntType]:
35
 
                next = datetime.datetime(*time.localtime(next)[:7])
36
 
            ievent.reply('%s, fires at %s' % (job.__repr__(), str(next)))
37
 
            return
38
 
    ievent.reply('job not found')
39
 
 
40
 
cmnds.add('job', handle_job, 'USER')
41
 
examples.add('job', 'show job data of <jobid> ', 'job 1')
42
 
 
43
 
def handle_joblist(bot, ievent):
44
 
    """ show job list """
45
 
    try:
46
 
        group = ievent.args[0]
47
 
    except IndexError:
48
 
        group = None
49
 
    result = []
50
 
    for job in periodical.jobs:
51
 
        if group and not job.group == group:
52
 
            continue
53
 
        if job.description:
54
 
            result.append('%d (%s)' % (job.id(), job.description))
55
 
        else:
56
 
            result.append('%d (%s)' % (job.id(), str(job.func.func_name)))
57
 
    if result:
58
 
        ievent.reply('jobs scheduled: ', result, dot=True)
59
 
    else:
60
 
        ievent.reply('no jobs')
61
 
 
62
 
cmnds.add('job-list', handle_joblist, 'OPER')
63
 
examples.add('job-list', 'show all waiting jobs or all jobs belonging to \
64
 
[group]', '1) job-list 2) job-list rss')
65
 
aliases.data['jobs'] = 'job-list'
66
 
 
67
 
def handle_jobgroups(bot, ievent):
68
 
    """ show job groups """
69
 
    result = [job.group for job in periodical.jobs]
70
 
    if result:
71
 
        ievent.reply('job groups: ', uniqlist(result), dot=True)
72
 
    else:
73
 
        ievent.reply('no jobs')
74
 
 
75
 
cmnds.add('job-groups', handle_jobgroups, 'OPER')
76
 
examples.add('job-groups', 'show all job groups', 'job-groups')
77
 
 
78
 
def handle_jobkill(bot, ievent):
79
 
    """ kill a job """
80
 
    if not ievent.args or not ievent.args[0].isdigit():
81
 
        ievent.missing('<job id> [<job id> ...]')
82
 
        return
83
 
    try:
84
 
        ids = [int(jid) for jid in ievent.args]
85
 
    except ValueError:
86
 
        ievent.missing('<job id> [<job id> ...]')
87
 
        return
88
 
    for jid in ids:
89
 
        periodical.killjob(int(ievent.args[0]))
90
 
    ievent.reply('killed %d jobs' % len(ids))
91
 
 
92
 
cmnds.add('job-kill', handle_jobkill, 'OPER')
93
 
examples.add('job-kill', 'kill job with <jobid>', 'job-kill 100000')