maintcont.py :  » Network » Python-Wikipedia-Robot-Framework » pywikipedia » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Network » Python Wikipedia Robot Framework 
Python Wikipedia Robot Framework » pywikipedia » maintcont.py
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
The controller bot for maintainer.py
Exactly one instance should be running of it. To check, use /whois maintcont on irc.freenode.net

This script requires the Python IRC library http://python-irclib.sourceforge.net/

Warning: experimental software, use at your own risk
"""
__version__ = '$Id: maintcont.py 7336 2009-09-29 18:27:04Z alexsh $'

# Author: Balasyum
# http://hu.wikipedia.org/wiki/User:Balasyum

from ircbot import SingleServerIRCBot
from irclib import nm_to_n
import threading
import time
import math

tasks = 'rciw|censure'
projtasks = {}
mainters = []
activity = {}

class MaintcontBot(SingleServerIRCBot):
    def __init__(self, nickname, server, port=6667):
        SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)

    def on_nicknameinuse(self, c, e):
        c.nick(c.get_nickname() + "_")

    def on_welcome(self, c, e):
        t = threading.Thread(target=self.lister)
        t.setDaemon(True)
        t.start()

    def on_privmsg(self, c, e):
        nick = nm_to_n(e.source())
        c = self.connection
        cmd = e.arguments()[0]
        do = cmd.split()
        if do[0] == "workerjoin":
            c.privmsg(nick, "accepted")
            mainters.append([nick, do[1]])
            activity[nick] = time.time()
            print "worker got, name:", nick, "job:", do[1]
            self.retasker(do[1])
        elif do[0] == "active":
            activity[nick] = time.time()

    def on_dccmsg(self, c, e):
        pass

    def on_dccchat(self, c, e):
        pass

    def lister(self):
        while True:
            print
            print "worker list:"
            for mainter in mainters:
                if time.time() - activity[mainter[0]] > 30:
                    print "*", mainter[0], "has been removed"
                    mainters.remove(mainter)
                    del activity[mainter[0]]
                    self.retasker(mainter[1])
                    continue
                print "mainter name:", mainter[0], "job:", mainter[1]
            print "--------------------"
            print
            time.sleep(1*60)

    def retasker(self, group, optask = ''):
        ingroup = 0
        for mainter in mainters:
            if mainter[1] == group:
                ingroup += 1
        if ingroup == 0:
            return
        if group in projtasks:
            grt = projtasks[group]
        else:
            grt = tasks
        tpc = grt.split('|')
        tpcn = round(len(tpc) / ingroup)
        i = 0
        for mainter in mainters:
            if mainter[1] != group:
                continue
            tts = '|'.join(tpc[int(round(i * tpcn)):int(round((i + 1) * tpcn))])
            if tts != False:
                self.connection.privmsg(mainter[0], "tasklist " + tts)
            i += 1

def main():
    bot = MaintcontBot("maintcont", "irc.freenode.net")
    bot.start()

if __name__ == "__main__":
    main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.