update format using black

This commit is contained in:
2019-05-08 05:06:58 +02:00
parent ad9eb25df0
commit 693711abc1
12 changed files with 488 additions and 408 deletions
+15 -14
View File
@@ -1,44 +1,46 @@
import os
class flock(object):
'''A simple filelocking mechanism to prevent execution at the same time'''
filename = '.im.lock'
"""A simple filelocking mechanism to prevent execution at the same time"""
filename = ".im.lock"
def __init__(self):
'''Creates an object with the current pid'''
"""Creates an object with the current pid"""
self.pid = os.getpid()
def aquire(self):
'''Try to get the lock, if it fails it returns False'''
"""Try to get the lock, if it fails it returns False"""
if self.is_locked():
return False
with open(self.filename, 'w+') as f:
f.write('{}'.format(self.pid))
with open(self.filename, "w+") as f:
f.write("{}".format(self.pid))
return True
def release(self):
'''Release the lock'''
"""Release the lock"""
if self.own_lock():
os.unlink(self.filename)
def __del__(self):
'''Release on delete'''
"""Release on delete"""
self.release()
def own_lock(self):
'''Check if the lock is assigned to the current pid'''
"""Check if the lock is assigned to the current pid"""
lockinfo = self._get_lockinfo()
return lockinfo == self.pid
def is_locked(self):
'''Check if it is currently locked'''
"""Check if it is currently locked"""
lockinfo = self._get_lockinfo()
if not lockinfo:
return False
return self._is_process_active(lockinfo)
def _is_process_active(self, pid):
'''Check if the processed having the lock is still running'''
"""Check if the processed having the lock is still running"""
try:
os.kill(pid, 0)
return pid != self.pid
@@ -46,12 +48,11 @@ class flock(object):
return False
def _get_lockinfo(self):
'''Retrieve the information about the lock'''
"""Retrieve the information about the lock"""
try:
lock = {}
with open(self.filename, 'r') as f:
with open(self.filename, "r") as f:
pid = int(f.read().strip())
return pid
except Exception as e:
return False