From 3ccb7783c90e0c23b1fef52ab74f02df9a710fd6 Mon Sep 17 00:00:00 2001 From: RusAnon Date: Mon, 3 May 2010 17:13:35 +1100 Subject: [PATCH] Added subclasses for all exported classes, and linked them to API --- dfhack/python/pydfhack/blocks.py | 6 ++++ dfhack/python/pydfhack/construction.py | 25 ++++++++++++++++ dfhack/python/pydfhack/creature.py | 25 ++++++++++++++++ dfhack/python/pydfhack/gui.py | 25 ++++++++++++++++ dfhack/python/pydfhack/map.py | 25 ++++++++++++++++ dfhack/python/pydfhack/materials.py | 25 ++++++++++++++++ dfhack/python/pydfhack/meminfo.py | 25 ++++++++++++++++ dfhack/python/pydfhack/position.py | 23 +++++++++++++++ dfhack/python/pydfhack/pydfapi.py | 41 ++++++++++++++++++-------- dfhack/python/pydfhack/translation.py | 25 ++++++++++++++++ dfhack/python/pydfhack/vegetation.py | 25 ++++++++++++++++ 11 files changed, 257 insertions(+), 13 deletions(-) create mode 100644 dfhack/python/pydfhack/construction.py create mode 100644 dfhack/python/pydfhack/creature.py create mode 100644 dfhack/python/pydfhack/gui.py create mode 100644 dfhack/python/pydfhack/map.py create mode 100644 dfhack/python/pydfhack/materials.py create mode 100644 dfhack/python/pydfhack/meminfo.py create mode 100644 dfhack/python/pydfhack/position.py create mode 100644 dfhack/python/pydfhack/translation.py create mode 100644 dfhack/python/pydfhack/vegetation.py diff --git a/dfhack/python/pydfhack/blocks.py b/dfhack/python/pydfhack/blocks.py index 91b2277ba..6e02d3066 100644 --- a/dfhack/python/pydfhack/blocks.py +++ b/dfhack/python/pydfhack/blocks.py @@ -11,6 +11,12 @@ class Point(object): def get_block(self): return Point(self.x/16, self.y/16, self.z, True) + + def __repr__(self): + b = '' + if self.block: + b = ', Block' + return "".format(self, b) class Block(object): """ diff --git a/dfhack/python/pydfhack/construction.py b/dfhack/python/pydfhack/construction.py new file mode 100644 index 000000000..d0876c5d9 --- /dev/null +++ b/dfhack/python/pydfhack/construction.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +""" +Python class for DF_Hack::Construction +""" +from ._pydfhack import _ConstructionManager +class Construction(_ConstructionManager): + api = None + started = False + def __init__(self, api, *args, **kwds): + _ConstructionManager.__init__(self, args, kwds) + self.api = api + + def prepare(self): + """ + Enforce Suspend/Start + """ + if self.api.prepare(): + if not self.started: + self.started = self.Start() + return self.started + else: + return False + + + diff --git a/dfhack/python/pydfhack/creature.py b/dfhack/python/pydfhack/creature.py new file mode 100644 index 000000000..8857e9ef1 --- /dev/null +++ b/dfhack/python/pydfhack/creature.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +""" +Python class for DF_Hack::Creature +""" +from ._pydfhack import _CreatureManager +class Creature(_CreatureManager): + api = None + started = False + def __init__(self, api, *args, **kwds): + _CreatureManager.__init__(self, args, kwds) + self.api = api + + def prepare(self): + """ + Enforce Suspend/Start + """ + if self.api.prepare(): + if not self.started: + self.started = self.Start() + return self.started + else: + return False + + + diff --git a/dfhack/python/pydfhack/gui.py b/dfhack/python/pydfhack/gui.py new file mode 100644 index 000000000..07192f67c --- /dev/null +++ b/dfhack/python/pydfhack/gui.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +""" +Python class for DF_Hack::GUI +""" +from ._pydfhack import _GUIManager +class GUI(_GUIManager): + api = None + started = False + def __init__(self, api, *args, **kwds): + _GUIManager.__init__(self, args, kwds) + self.api = api + + def prepare(self): + """ + Enforce Suspend/Start + """ + if self.api.prepare(): + if not self.started: + self.started = self.Start() + return self.started + else: + return False + + + diff --git a/dfhack/python/pydfhack/map.py b/dfhack/python/pydfhack/map.py new file mode 100644 index 000000000..944aa1fe0 --- /dev/null +++ b/dfhack/python/pydfhack/map.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +""" +Python class for DF_Hack::Maps +""" +from ._pydfhack import _MapManager +class Map(_MapManager): + api = None + started = False + def __init__(self, api, *args, **kwds): + _MapManager.__init__(self, args, kwds) + self.api = api + + def prepare(self): + """ + Enforce Suspend/Start + """ + if self.api.prepare(): + if not self.started: + self.started = self.Start() + return self.started + else: + return False + + + diff --git a/dfhack/python/pydfhack/materials.py b/dfhack/python/pydfhack/materials.py new file mode 100644 index 000000000..5cd3de191 --- /dev/null +++ b/dfhack/python/pydfhack/materials.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +""" +Python class for DF_Hack::Materials +""" +from ._pydfhack import _MaterialsManager +class Materials(_MaterialsManager): + api = None + started = False + def __init__(self, api, *args, **kwds): + _MaterialsManager.__init__(self, args, kwds) + self.api = api + + def prepare(self): + """ + Enforce Suspend/Start + """ + if self.api.prepare(): + if not self.started: + self.started = self.Start() + return self.started + else: + return False + + + diff --git a/dfhack/python/pydfhack/meminfo.py b/dfhack/python/pydfhack/meminfo.py new file mode 100644 index 000000000..2a4b155aa --- /dev/null +++ b/dfhack/python/pydfhack/meminfo.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +""" +Python class for DF_Hack::MemInfo +""" +from ._pydfhack import _MemInfo +class MemInfo(_MemInfo): + api = None + started = False + def __init__(self, api, *args, **kwds): + _MemInfo.__init__(self, args, kwds) + self.api = api + + def prepare(self): + """ + Enforce Suspend/Start + """ + if self.api.prepare(): + if not self.started: + self.started = self.Start() + return self.started + else: + return False + + + diff --git a/dfhack/python/pydfhack/position.py b/dfhack/python/pydfhack/position.py new file mode 100644 index 000000000..f46f5cf80 --- /dev/null +++ b/dfhack/python/pydfhack/position.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +""" +Python class for DF_Hack::Position +""" +from ._pydfhack import _PositionManager +from .blocks import Point, Block +class Position(_PositionManager): + api = None + def __init__(self, api, *args, **kwds): + _PositionManager.__init__(self, args, kwds) + self.api = api + + def prepare(self): + """ + Enforce Suspend/Start + """ + return self.api.prepare() + + def get_cursor(self): + self.prepare() + coords = self.cursor_coords + return Point(*coords) + diff --git a/dfhack/python/pydfhack/pydfapi.py b/dfhack/python/pydfhack/pydfapi.py index 5b7f7ae97..c172bc05f 100644 --- a/dfhack/python/pydfhack/pydfapi.py +++ b/dfhack/python/pydfhack/pydfapi.py @@ -1,4 +1,14 @@ import _pydfhack, os +from .blocks import Point, Block +from .meminfo import MemInfo +from .position import Position +from .materials import Materials +from .creature import Creature +from .map import Map +from .translation import Translation +from .construction import Construction +from .vegetation import Vegetation +from .gui import GUI class API(_pydfhack._API): for file in ["Memory.xml", os.path.join("..","..","output","Memory.xml")]: if os.path.isfile(file): @@ -7,21 +17,26 @@ class API(_pydfhack._API): else: raise ImportError, "Memory.xml not found." + def prepare(self): + """ + Enforce Attach/Suspend behavior. + Return True if succeeded, else False + """ + r = True + if not self.is_attached: + r = self.Attach() + if r and not self.is_suspended: + r = self.Suspend() + return r + def __init__(self, *args, **kwds): _pydfhack._API.__init__(self, API.datafile) - + self._mem_info_mgr_type = MemInfo + self._position_mgr_type = Position + self._material_mgr_type = Materials + self._creature_mgr_type = Creature self._map_mgr_type = Map + self._translate_mgr_type = Translation + self._construction_mgr_type = Construction self._vegetation_mgr_type = Vegetation self._gui_mgr_type = GUI - -class Map(_pydfhack._MapManager): - def __init__(self, *args, **kwds): - _pydfhack._MapManager.__init__(self, args, kwds) - -class Vegetation(_pydfhack._VegetationManager): - def __init__(self, *args, **kwds): - _pydfhack._VegetationManager.__init__(self, args, kwds) - -class GUI(_pydfhack._GUIManager): - def __init__(self, *args, **kwds): - _pydfhack._GUIManager.__init__(self, args, kwds) diff --git a/dfhack/python/pydfhack/translation.py b/dfhack/python/pydfhack/translation.py new file mode 100644 index 000000000..0179e9db0 --- /dev/null +++ b/dfhack/python/pydfhack/translation.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +""" +Python class for DF_Hack::Translation +""" +from ._pydfhack import _TranslationManager +class Translation(_TranslationManager): + api = None + started = False + def __init__(self, api, *args, **kwds): + _TranslationManager.__init__(self, args, kwds) + self.api = api + + def prepare(self): + """ + Enforce Suspend/Start + """ + if self.api.prepare(): + if not self.started: + self.started = self.Start() + return self.started + else: + return False + + + diff --git a/dfhack/python/pydfhack/vegetation.py b/dfhack/python/pydfhack/vegetation.py new file mode 100644 index 000000000..b80291d66 --- /dev/null +++ b/dfhack/python/pydfhack/vegetation.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +""" +Python class for DF_Hack::Vegetation +""" +from ._pydfhack import _VegetationManager +class Vegetation(_VegetationManager): + api = None + started = False + def __init__(self, api, *args, **kwds): + _VegetationManager.__init__(self, args, kwds) + self.api = api + + def prepare(self): + """ + Enforce Suspend/Start + """ + if self.api.prepare(): + if not self.started: + self.started = self.Start() + return self.started + else: + return False + + +