# -*- encoding: UTF-8 -*-
# 
# This a project generated by Pyjama with default layout
# 
# Copyright (C) 2012  maemo
# 
# 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/>
# 

'''
Created on Mar 5, 2012

@author: maemo
'''

import logging
import threading

from ..common import version 

version.getInstance().submitRevision("$Revision: 11 $")

FREE_MOBILE_OP_CODE = [14,15]
FREE_MOBILE_COUNTRY = [208]

ORANGE_OP_CODE = [0,1,2]
ORANGE_COUNTRY = [208]

class AsyncTask(threading.Thread):
    '''
    This is a stopable asynchroneous task.
    '''
    def __init__(self, stop_hook,  method, *argv, **kwarg):
            self.task_name = "Mougeon-AsyncTask"
            threading.Thread.__init__(self,name=self.task_name)
            self.method = method   
            self.argv = argv
            self.kwarg = kwarg                
            self.stop_hook = stop_hook

                     
        
    def run(self):            
            logging.debug("%s thread running...." % self.task_name)                                                  
            self.cancellableAction()            
            logging.debug("%s thread finished" % self.task_name)

    def cancellableAction(self):
            try:
                self.method(*self.argv, **self.kwarg)
            except StopSignalException, sse:
                logging.info("%s aborted by software" % self.task_name)
            except Exception, e:
                logging.error("uncaught exception in %s \n%s" %(self.task_name,str(e)))
                raise
            else:
                logging.debug("%s terminated" % self.task_name)
            
    def send_stop_signal(self):
        if self.stop_hook:
            self.stop_hook()
        
class StopSignalException(Exception):
    pass

