]> git.neil.brown.name Git - susman.git/commitdiff
suspend.py: make a 'blocker' class.
authorNeilBrown <neilb@suse.de>
Sun, 22 Apr 2012 04:22:53 +0000 (14:22 +1000)
committerNeilBrown <neilb@suse.de>
Sun, 22 Apr 2012 04:22:53 +0000 (14:22 +1000)
Rather than using a global variable to hold the file handle on the
'disabled' file, create a class so a class-object can hold it.

suspend.py

index f0703343c657c7d16a1634b5c4f7f1a799bf4836..842b9a566fb6eb336ccc2c98e1115c0a4bac4b54 100644 (file)
@@ -101,22 +101,21 @@ class monitor:
             self.immediate_fd.close()
             self.immediate_fd = None
 
-blockfd = None
-def block():
-    global blockfd
-    if blockfd:
-        return
-    try:
-        blockfd = open('/var/run/suspend/disabled')
-        fcntl.flock(blockfd, fcntl.LOCK_SH)
-    except:
-        pass
+class blocker:
+    def __init__(self, blocked = True):
+        self.blockfd = open('/var/run/suspend/disabled')
+        if blocked:
+            self.block()
+    def block(self):
+        fcntl.flock(self.blockfd, fcntl.LOCK_SH)
+    def unblock(self):
+        fcntl.flock(self.blockfd, fcntl.LOCK_UN)
+    def close(self):
+        self.blockfd.close()
+        self.blockfd = None
+    def abort(self):
+        self.blockfd.read(1)
 
-def unblock():
-    global blockfd
-    if blockfd:
-        blockfd.close()
-        blockfd = None
 
 def abort_cycle():
     fd = open('/var/run/suspend/disabled')