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,33 +10,43 @@ 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):
for file in ["Memory.xml", os.path.join("..","..","output","Memory.xml")]: started = None
if os.path.isfile(file):
datafile = file for file in ["Memory.xml", os.path.join("..","..","output","Memory.xml")]:
break if os.path.isfile(file):
else: datafile = file
raise ImportError, "Memory.xml not found." break
else:
raise ImportError, "Memory.xml not found."
def prepare(self): def prepare(self):
""" """
Enforce Attach/Suspend behavior. Enforce Attach/Suspend behavior.
Return True if succeeded, else False Return True if succeeded, else False
""" """
r = True r = True
if not self.is_attached: if not self.is_attached:
r = self.Attach() r = self.Attach()
if r and not self.is_suspended: if r and not self.is_suspended:
r = self.Suspend() r = self.Suspend()
return r return r
def __init__(self, *args, **kwds): def __init__(self, *args, **kwds):
_pydfhack._API.__init__(self, API.datafile) _pydfhack._API.__init__(self, API.datafile)
self._mem_info_mgr_type = MemInfo self._mem_info_mgr_type = MemInfo
self._position_mgr_type = Position self._position_mgr_type = Position
self._material_mgr_type = Materials self._material_mgr_type = Materials
self._creature_mgr_type = Creature self._creature_mgr_type = Creature
self._map_mgr_type = Map self._map_mgr_type = Map
self._translate_mgr_type = Translation self._translate_mgr_type = Translation
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)