~ubuntu-branches/ubuntu/precise/gozerbot/precise

« back to all changes in this revision

Viewing changes to build/lib/gozerplugs/plugs/job.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2008-06-02 19:26:39 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080602192639-3rn65nx4q1sgd6sy
Tags: 0.8.1-1
New upstream release.

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