Automatically handle Suspend/Resume Start/Finish

develop
RusAnon 2010-05-03 18:45:25 +11:00
parent f678b344d2
commit add3288333
3 changed files with 52 additions and 30 deletions

@ -7,9 +7,14 @@ from decorator import decorator
@decorator
def suspend(func, self, *args, **kw):
"""
This decorator will try to suspend DF and start needed module before running func
This decorator will try to suspend DF and start needed module before running func.
If DF was resumed when decorator was run, it will try to resume back after executing func
"""
susp = not self.api.is_attached or self.api.is_suspended
if self.prepare():
return func(self, *args, **kw)
res = func(self, *args, **kw)
if not susp:
self.api.Resume()
return res
else:
raise Exception(u"Could not suspend/start")

@ -20,8 +20,13 @@ class NeedsStart(object):
return False
def Start(self):
if self.started:
return True
if self.api.prepare():
self.started = self.cls.Start(self)
if self.started:
self.api.started.append(self)
return self.started
else:
return False
@ -32,6 +37,8 @@ class NeedsStart(object):
if self.started:
self.cls.Finish(self)
self.started = False
if self in self.api.started:
self.api.started.remove(self)
finish = Finish

@ -10,6 +10,8 @@ from .construction import Construction
from .vegetation import Vegetation
from .gui import GUI
class API(_pydfhack._API):
started = None
for file in ["Memory.xml", os.path.join("..","..","output","Memory.xml")]:
if os.path.isfile(file):
datafile = file
@ -40,3 +42,11 @@ class API(_pydfhack._API):
self._construction_mgr_type = Construction
self._vegetation_mgr_type = Vegetation
self._gui_mgr_type = GUI
self.started = []
def Resume(self):
# Explicitly Finish() all started modules
for m in self.started[:]:
m.Finish()
self.started = []
_pydfhack._API.Resume(self)