Skip to content

Commit 7552201

Browse files
committed
Module Locking
1 parent fdf0552 commit 7552201

1 file changed

Lines changed: 41 additions & 9 deletions

File tree

philh_myftp_biz/modules.py

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ def Scanner() -> Generator['Module']:
6868
pass
6969

7070
class ModuleDisabledError(Exception):
71-
pass
71+
def __init__(self, module:'Module'):
72+
super().__init__(module.dir.path)
73+
74+
class ModuleLockedError(Exception):
75+
def __init__(self, module:'Module'):
76+
super().__init__(module.dir.path)
7277

7378
class Module:
7479
"""
@@ -115,6 +120,8 @@ def __init__(self,
115120

116121
config = YAML(configFile).read()
117122

123+
self.lock = Lock(self)
124+
118125
self.enabled = config['enabled']
119126

120127
self.packages: list[str] = config['packages']
@@ -133,16 +140,20 @@ def run(self,
133140
"""
134141
Execute a new Process and wait for it to finish
135142
"""
136-
if self.enabled:
143+
if not self.enabled:
144+
raise ModuleDisabledError(self)
145+
146+
elif self.lock.locked():
147+
raise ModuleLockedError(self)
148+
149+
else:
137150
return Process(
138151
module = self,
139152
args = list(args),
140153
hide = hide,
141154
wait = True
142155
)
143-
144-
else:
145-
raise ModuleDisabledError(self.dir.path)
156+
146157

147158
def start(self,
148159
*args,
@@ -151,16 +162,19 @@ def start(self,
151162
"""
152163
Execute a new Process simultaneously with the current execution
153164
"""
154-
if self.enabled:
165+
if not self.enabled:
166+
raise ModuleDisabledError(self)
167+
168+
elif self.lock.locked():
169+
raise ModuleLockedError(self)
170+
171+
else:
155172
return Process(
156173
module = self,
157174
args = list(args),
158175
hide = hide,
159176
wait = False
160177
)
161-
162-
else:
163-
raise ModuleDisabledError(self.dir.path)
164178

165179
def file(self,
166180
*name: str
@@ -297,3 +311,21 @@ def modified(self) -> bool:
297311
"""Check if the file has been modified"""
298312

299313
return (self.__mtime.read() != self.path.mtime.get().unix)
314+
315+
class Lock:
316+
317+
def __init__(self,
318+
module: Module
319+
):
320+
self.__lockfile = module.dir.child('/__lock__.ig')
321+
322+
def lock(self) -> None:
323+
self.__lockfile.open('w')
324+
self.__lockfile.visibility.hide()
325+
326+
def unlock(self) -> None:
327+
if self.__lockfile.exists():
328+
self.__lockfile.delete()
329+
330+
def locked(self) -> bool:
331+
return self.__lockfile.exists()

0 commit comments

Comments
 (0)