Subversion Repositories Scribus

Rev

Blame | Last modification | View Log | RSS feed

###
# Copyright (c) 2006, 2007, Oleksandr Moskalenko
# All rights reserved.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
###

import supybot.conf as conf
import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.ircmsgs as ircmsgs
import supybot.callbacks as callbacks
import urllib2
import re
from BeautifulSoup import BeautifulSoup
#from BeautifulSoup import Null

class Utils:
    """This class contains utils used by several commands."""
    def __init__(self):
        pass
    def _makeurl(self, server, bugno):
        self.bugnumber = str(bugno)
        self.server = server
        self.url = 'http://'+self.server+'/view.php?id='+self.bugnumber
        return self.url
    def _bugdata(self, url):
        self.url = url
        try:
            self.srchtml = urllib2.urlopen(self.url).read()
        except urllib2.URLError:
            print "Cannot fetch the bug page."
        self.soup = BeautifulSoup(self.srchtml)
        return self.soup

class Mantis(callbacks.PluginRegexp):
    """This plugin is non-interactive. No help is needed."""
    regexps = ['bugUrl'] #,'titleSnarfer']
    def __init__(self, irc):
        self.__parent = super(Mantis, self)
        self.__parent.__init__(irc)
        if self.checkbts() == True:
            self.btsstatus = 'Mantis BTS is accessible'
        else:
            self.btsstatus = 'Cannot access the BTS front page'
        self.mantisutils = Utils()

    def bugtitle(self, irc, msg, args, number):
        """<number>

        Returns the Title of the bug number <number>. Only positive integers
        are accepted as bug numbers.
        """
        self.bugno = number
        self.url = self.mantisutils._makeurl(self.server, self.bugno)
        try:
            self.soup = self.mantisutils._bugdata(self.url)
            self.title = self.soup.html.head.title.contents
            if self.title[0] == 'Mantis Issue Tracker for Scribus':
                irc.reply("Sorry, it appears that this bug doesn't exist")
            elif self.title == None:
                irc.reply("Sorry, this bug is not accessible")
            else:
                self.shorttitle = self.title[0].strip().split(" - Mantis Issue Tracker")[0]
                irc.reply(self.shorttitle)
        except:
            irc.reply("Sorry, an unknown error has occured. I'll go cry for a while")

    bugtitle = wrap(bugtitle, ['int'])

    def bugstatus(self, irc, msg, args, number):
        """<number>

        Returns the status of the bug number <number>. Only positive integers
        are accepted as bug numbers.
        """
        self.bugno = number
        self.url = self.mantisutils._makeurl(self.server, self.bugno)
        try:
            self.soup = self.mantisutils._bugdata(self.url)
            self.title = self.soup.html.head.title.contents
            if self.title[0] == 'Mantis Issue Tracker for Scribus':
                irc.reply("Sorry, it appears that this bug doesn't exist")
            elif self.title == None:
                irc.reply("Sorry, this bug is not accessible")
            else:
                self.status = self.soup.firstText(re.compile('--\ Status')).findNext('td').findNext('td').contents[0].strip()
                irc.reply(self.status)
        except URLError, e:
            irc.reply(str(e))

    bugstatus = wrap(bugstatus, ['int'])

    def bugreporter(self, irc, msg, args, number):
        """<number>

        Returns the reporter of the bug number <number>. Only positive integers
        are accepted as bug numbers.
        """
        self.bugno = number
        self.url = self.mantisutils._makeurl(self.server, self.bugno)
        try:
            self.soup = self.mantisutils._bugdata(self.url)
            self.title = self.soup.html.head.title.contents
            if self.title[0] == 'Mantis Issue Tracker for Scribus':
                irc.reply("Sorry, it appears that this bug doesn't exist")
            elif self.title == None:
                irc.reply("Sorry, this bug is not accessible")
            else:
                self.reporter = self.soup.firstText(re.compile('Reporter')).findNext('td').findNext('td').contents[0].strip()
                irc.reply(self.reporter)
        except Error, e:
            irc.reply(str(e))

    bugreporter = wrap(bugreporter, ['int'])

    def assignedto(self, irc, msg, args, number):
        """<number>

        Returns login name of the developer the bug number <number> is assigned
        to. Only positive integers are accepted as bug numbers.
        """
        self.bugno = number
        self.url = self.mantisutils._makeurl(self.server, self.bugno)
        try:
            self.soup = self.mantisutils._bugdata(self.url)
            self.title = self.soup.html.head.title.contents
            if self.title[0] == 'Mantis Issue Tracker for Scribus':
                irc.reply("Sorry, it appears that this bug doesn't exist")
            elif self.title == None:
                irc.reply("Sorry, this bug is not accessible")
            else:
                self.assignee = self.soup.firstText(re.compile('Assigned\ To')).findNext('td').contents[0].strip()
                irc.reply(self.assignee)
        except Error, e:
            irc.reply(str(e))

    assignedto = wrap(assignedto, ['int'])

    def checkbts(self):
        """ Example: http://bugs.scribus.net/main_page.php"""
        self.defaultsrv='bugs.scribus.net'
        self.confsrv = conf.supybot.plugins.Mantis.server()
        if not self.confsrv:
            self.server = self.defaultsrv
        else:
            self.server = self.confsrv
        self.btsurl = 'http://'+self.server+'/main_page.php'
        #self.btsurl = 'http://'+self.defaultsrv+'/main_page.php'
        try:
            self.mainpage = urllib2.urlopen(self.btsurl)
            self.log.debug('Checking the bts at %u',  self.btsurl)
            return True
        except URLError:
            print "Cannot open the BTS main page. Unloading plugin."

    def bugUrl(self, irc, msg, m):
        r"^#\d{4,9}"
        """Returns the URL for the bug number passed into the channel."""
        self.msg = msg
        self.channel = self.msg.args[0]
        if irc.isChannel(self.channel):
            if ircmsgs.isAction(self.msg):
                self.text = ircmsgs.unAction(self.msg)
            else:
                self.text = self.msg.args[1]
            self.buglist = re.findall(r"^#\d{4,9}", self.text)
            for self.bugid in self.buglist:
                self.bugno = self.bugid.rstrip()[1:]
                self.url = self.mantisutils._makeurl(self.server,self.bugno)
                try:
                    self.soup = self.mantisutils._bugdata(self.url)
                    self.title = self.soup.html.head.title.contents
                    if self.title[0] == 'Mantis Issue Tracker for Scribus':
                        irc.reply("Sorry, it appears that this bug doesn't exist")
                    elif self.title == None:
                        irc.reply("Sorry, this bug is not accessible")
                    else:
                        self.shorttitle = self.title[0].strip().split(" - Mantis Issue Tracker")[0]
                        irc.reply(self.shorttitle)
                except:
                    irc.reply("Sorry, an unknown error has occured. I'll go cry for a while")
        else:
            self.log.debug("We are not in a channel")

Class = Mantis

# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: