iro/joblist.py
author Sandro Knauß <knauss@netzguerilla.net>
Sun, 08 Nov 2009 15:50:29 +0100
changeset 13 2da41f4598e1
parent 11 f25033cf93e0
child 55 ae3669fef3b0
permissions -rw-r--r--
content nun beim anbieter

# -*- coding: utf-8 -*-
#Copyright (C) 2009  Sandro Knauß <bugs@sandroknauss.de>

#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 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/>.

from anbieter import content
import md5, os,  time


class Joblist:
    '''
    Providing an list of jobs; each new job gets a hash id
    '''
    def __init__(self,manager, queue,providerlist):
        self.jobs={}
        self.manager=manager
        self.queue=queue
        self.providerlist=providerlist
        

    def __getitem__(self,key):
        return self.jobs[key]

    def __registerJob__(self, job):
        id = self._createID()
        self.jobs[id]=job
        self.queue.put(job)
        return id

    def newSMS(self,message,recipients,provider="default"):
        '''
        creates a new SMS
        '''
        job=self.manager.SMSJob(self.providerlist, provider,message, content.SMS(message),recipients)
        return self.__registerJob__(job)

    def newFAX(self,subject, fax,recipients,provider="default"):
        '''
        creates a new Fax
        '''
        job=self.manager.FaxJob(self.providerlist, provider,subject, content.FAX(subject, fax),recipients)
        return self.__registerJob__(job)

    def newMail(self,subject, body,recipients,provider="default"):
        '''
        creates a new Mail
        '''
        job=self.manager.MailJob(self.providerlist, provider,subject, content.Mail(subject, body),recipients)
        return self.__registerJob__(job) 

    def _createID(self):
        '''
        creats a random hash id
        '''
        while True:
            m = md5.new()
            m.update(str(time.time()))
            m.update(os.urandom(10))
            if not self.jobs.has_key(m.hexdigest):
                self.jobs[m.hexdigest()]=None
                break
        return m.hexdigest()