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 @decorator
def suspend(func, self, *args, **kw): 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(): if self.prepare():
return func(self, *args, **kw) res = func(self, *args, **kw)
if not susp:
self.api.Resume()
return res
else: else:
raise Exception(u"Could not suspend/start") raise Exception(u"Could not suspend/start")

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

@ -10,6 +10,8 @@ from .construction import Construction
from .vegetation import Vegetation from .vegetation import Vegetation
from .gui import GUI from .gui import GUI
class API(_pydfhack._API): class API(_pydfhack._API):
started = None
for file in ["Memory.xml", os.path.join("..","..","output","Memory.xml")]: for file in ["Memory.xml", os.path.join("..","..","output","Memory.xml")]:
if os.path.isfile(file): if os.path.isfile(file):
datafile = file datafile = file
@ -40,3 +42,11 @@ class API(_pydfhack._API):
self._construction_mgr_type = Construction self._construction_mgr_type = Construction
self._vegetation_mgr_type = Vegetation self._vegetation_mgr_type = Vegetation
self._gui_mgr_type = GUI 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)