~freshapplepy/caffeine/fullscreen

« back to all changes in this revision

Viewing changes to caffeine/applicationinstance.py

  • Committer: Isaiah Heyer
  • Date: 2009-08-21 04:52:41 UTC
  • Revision ID: freshapplepy@gmail.com-20090821045241-jezoc4ifq6xeuezp
Added command line options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
 
25
25
#class used to handle one application instance mechanism
26
 
class ApplicationInstance:
27
 
 
28
 
        #specify the file used to save the application instance pid
29
 
        def __init__( self, pid_file ):
30
 
                self.pid_file = pid_file
31
 
                self.check()
32
 
                self.startApplication()
33
 
 
34
 
        #check if the current application is already running
35
 
        def check( self ):
36
 
                #check if the pidfile exists
37
 
                if not os.path.isfile( self.pid_file ):
38
 
                        return
39
 
 
40
 
                #read the pid from the file
41
 
                pid = 0
42
 
                try:
43
 
                        file = open( self.pid_file, 'rt' )
44
 
                        data = file.read()
45
 
                        file.close()
46
 
                        pid = int( data )
47
 
                except:
48
 
                        pass
49
 
 
50
 
                #check if the process with specified by pid exists
51
 
                if 0 == pid:
52
 
                        return
53
 
 
54
 
                try:
55
 
                        os.kill( pid, 0 )       #this will raise an exception if the pid is not valid
56
 
                except:
57
 
                        return
58
 
 
59
 
                #exit the application
60
 
                print "The application is already running !"
61
 
                exit(0) #exit raise an exception so don't put it in a try/except block
62
 
 
63
 
        #called when the single instance starts to save it's pid
64
 
        def startApplication( self ):
65
 
                file = open( self.pid_file, 'wt' )
66
 
                file.write( str( os.getpid() ) )
67
 
                file.close()
68
 
 
69
 
        #called when the single instance exit ( remove pid file )
70
 
        def exitApplication( self ):
71
 
                try:
72
 
                        os.remove( self.pid_file )
73
 
                except:
74
 
                        pass
 
26
class ApplicationInstance(object):
 
27
 
 
28
    #specify the file used to save the application instance pid
 
29
    def __init__(self, pid_file):
 
30
        self.pid_file = pid_file
 
31
    
 
32
    #check if the current application is already running
 
33
    def isAnother(self):
 
34
        #check if the pidfile exists
 
35
        if not os.path.isfile( self.pid_file ):
 
36
            return False
 
37
 
 
38
        #read the pid from the file
 
39
        pid = 0
 
40
        try:
 
41
            file = open(self.pid_file, 'rt')
 
42
            data = file.read()
 
43
            file.close()
 
44
            pid = int( data )
 
45
            self.pid = pid
 
46
        except:
 
47
            pass
 
48
 
 
49
        #check if the process with specified by pid exists
 
50
        if 0 == pid:
 
51
            return False
 
52
    
 
53
        #this will raise an exception if the pid is not valid
 
54
        try:
 
55
            os.kill(pid, 0)
 
56
        except:
 
57
            return False
 
58
 
 
59
        return True
 
60
 
 
61
    def killOther(self):
 
62
        os.kill(self.pid, 9)
 
63
 
 
64
    #called when the single instance starts to save it's pid
 
65
    def startApplication(self):
 
66
        file = open(self.pid_file, 'wt')
 
67
        file.write(str(os.getpid()))
 
68
        file.close()
 
69
 
 
70
    #called when the single instance exit ( remove pid file )
 
71
    def exitApplication(self):
 
72
        try:
 
73
            os.remove(self.pid_file)
 
74
        except:
 
75
            pass