power: set path to file once

This commit is contained in:
James Cameron 2015-05-13 09:02:10 +10:00 committed by Sam Parkinson
parent a56282991f
commit 561271bad1

View File

@ -38,6 +38,7 @@ class PowerManager():
def __init__(self): def __init__(self):
self._suspend_inhibit_counter = 0 self._suspend_inhibit_counter = 0
self._path = os.path.join(_POWERD_INHIBIT_DIR, str(os.getpid()))
def __del__(self): def __del__(self):
self._remove_flag_file() self._remove_flag_file()
@ -50,13 +51,12 @@ class PowerManager():
return return
if self._suspend_inhibit_counter == 0: if self._suspend_inhibit_counter == 0:
path = os.path.join(_POWERD_INHIBIT_DIR, str(os.getpid()))
try: try:
with open(path, 'w') as flag_file: with open(self._path, 'w') as flag_file:
flag_file.write('') flag_file.write('')
except IOError: except IOError:
logging.error("Inhibit Suspend: Could not create file %s", logging.error("Inhibit Suspend: Could not create file %s",
path) self._path)
self._suspend_inhibit_counter += 1 self._suspend_inhibit_counter += 1
@ -77,9 +77,9 @@ class PowerManager():
self._remove_flag_file() self._remove_flag_file()
def _remove_flag_file(self): def _remove_flag_file(self):
path = os.path.join(_POWERD_INHIBIT_DIR, str(os.getpid()))
try: try:
os.unlink(path) os.unlink(self._path)
except OSError: except OSError:
logging.error("Inhibit Suspend: Could not delete file %s", path) logging.error("Inhibit Suspend: Could not delete file %s",
self._path)
self._suspend_inhibit_counter = 0 self._suspend_inhibit_counter = 0