view engine/python/fife/extensions/pythonize.py @ 697:ecaa4d98f05f tip

Abstracted the GUI code and refactored the GUIChan-specific code into its own module. * Most of the GUIChan code has been refactored into its own gui/guichan module. However, references to the GuiFont class still persist in the Engine and GuiManager code and these will need further refactoring. * GuiManager is now an abstract base class which specific implementations (e.g. GUIChan) should subclass. * The GUIChan GUI code is now a concrete implementation of GuiManager, most of which is in the new GuiChanGuiManager class. * The GUI code in the Console class has been refactored out of the Console and into the GUIChan module as its own GuiChanConsoleWidget class. The rest of the Console class related to executing commands was left largely unchanged. * Existing client code may need to downcast the GuiManager pointer received from FIFE::Engine::getGuiManager() to GuiChanGuiManager, since not all functionality is represented in the GuiManager abstract base class. Python client code can use the new GuiChanGuiManager.castTo static method for this purpose.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 18 Jun 2011 00:28:40 -1000
parents 4f36c890b1dd
children
line wrap: on
line source

# -*- coding: utf-8 -*-

# ####################################################################
#  Copyright (C) 2005-2010 by the FIFE team
#  http://www.fifengine.net
#  This file is part of FIFE.
#
#  FIFE is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the
#  Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
# ####################################################################

"""\
Pythonize FIFE

Import this extension to get a more
pythonic interface to FIFE.

Currently it implements the following
conveniences:

* FIFE Exceptions print their message.
* Automatic property generation for:
  * fife.Engine
  * fife.Instance
  * fife.Image
  * fife.Animation
  * fife.Point
  * fife.Rect

"""

from fife import fife
import re

__all__ = ()

fife.Exception.__str__ = fife.Exception.what
def _Color2Str(c):
	return 'Color(%s)' % ','.join(map(str,(c.r,c.g,c.b,c.a)))
fife.Color.__str__ = _Color2Str

classes = [ fife.Engine, fife.Instance, fife.Point, fife.Rect, fife.Image, fife.Animation,
 fife.RenderBackend, fife.Event, fife.Command, fife.Container ]

def createProperties():
	""" Autocreate properties for getXYZ/setXYZ functions.
	"""
	try:
		import inspect
		getargspec = inspect.getargspec
	except ImportError:
		print "Pythonize: inspect not available - properties are generated with dummy argspec."
		getargspec = lambda func : ([],'args',None,None)

	def isSimpleGetter(func):
		if not callable(func):
			return False
		try:
			argspec = getargspec(func)
			return not (argspec[0] or [s for s in argspec[2:] if s])
		except TypeError, e:
			#print func, e
			return False

	def createNames(name):
		for prefix in ('get', 'is', 'are'):
			if name.startswith(prefix):
				new_name = name[len(prefix):]
				break
		settername   = 'set' + new_name
		propertyname = new_name[0].lower() + new_name[1:]
		return settername, propertyname

	getter = re.compile(r"^(get|are|is)[A-Z]")
	for class_ in classes:
		methods = [(name,attr) for name,attr in class_.__dict__.items()
		                       if isSimpleGetter(attr) ]
		setmethods = [(name,attr) for name,attr in class_.__dict__.items() if callable(attr)]
		getters = []
		for name,method in methods:
			if getter.match(name):
				getters.append((name,method))
				settername, propertyname = createNames(name)
				setter = dict(setmethods).get(settername,None)
				#print name, settername, "--->",propertyname,'(',method,',',setter,')'
				setattr(class_,propertyname,property(method,setter))
		if not getters: continue

		# We need to override the swig setattr function
		# to get properties to work.
		class_._property_names = set([name for name,method in getters])
		def _setattr_wrapper_(self,*args):
			if name in class_._property_names:
				object.__setattr__(self,*args)
			else:
				class_.__setattr__(self,*args)
		class_.__setattr__ = _setattr_wrapper_

createProperties()