Package fife :: Package extensions :: Module pythonize
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.pythonize

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2019 by the FIFE team 
  5  #  http://www.fifengine.net 
  6  #  This file is part of FIFE. 
  7  # 
  8  #  FIFE is free software; you can redistribute it and/or 
  9  #  modify it under the terms of the GNU Lesser General Public 
 10  #  License as published by the Free Software Foundation; either 
 11  #  version 2.1 of the License, or (at your option) any later version. 
 12  # 
 13  #  This library is distributed in the hope that it will be useful, 
 14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 16  #  Lesser General Public License for more details. 
 17  # 
 18  #  You should have received a copy of the GNU Lesser General Public 
 19  #  License along with this library; if not, write to the 
 20  #  Free Software Foundation, Inc., 
 21  #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 22  # #################################################################### 
 23   
 24  """\ 
 25  Pythonize FIFE 
 26   
 27  Import this extension to get a more 
 28  pythonic interface to FIFE. 
 29   
 30  Currently it implements the following 
 31  conveniences: 
 32   
 33    - FIFE Exceptions print their message. 
 34    - Automatic property generation for: 
 35      - fife.Engine 
 36      - fife.Instance 
 37      - fife.Image 
 38      - fife.Animation 
 39      - fife.Point 
 40      - fife.Rect 
 41  """ 
 42  from __future__ import print_function 
 43   
 44  from builtins import map 
 45  from fife import fife 
 46  from fife import fifechan 
 47  import re 
 48   
 49  __all__ = () 
 50   
 51  fife.Exception.__str__ = fife.Exception.what 
52 -def _Color2Str(c):
53 return 'Color(%s)' % ','.join(map(str,(c.r,c.g,c.b,c.a)))
54 fifechan.Color.__str__ = _Color2Str 55 56 classes = [ fife.Engine, fife.Instance, fife.Point, fife.Rect, fife.Image, fife.Animation, 57 fife.RenderBackend, fife.Event, fife.Command, fifechan.Container ] 58
59 -def createProperties():
60 """ Autocreate properties for getXYZ/setXYZ functions. 61 """ 62 try: 63 import inspect 64 getargspec = inspect.getargspec 65 except ImportError: 66 print("Pythonize: inspect not available - properties are generated with dummy argspec.") 67 getargspec = lambda func : ([],'args',None,None) 68 69 def isSimpleGetter(func): 70 if not callable(func): 71 return False 72 try: 73 argspec = getargspec(func) 74 return not (argspec[0] or [s for s in argspec[2:] if s]) 75 except TypeError as e: 76 #print func, e 77 return False
78 79 def createNames(name): 80 for prefix in ('get', 'is', 'are'): 81 if name.startswith(prefix): 82 new_name = name[len(prefix):] 83 break 84 settername = 'set' + new_name 85 propertyname = new_name[0].lower() + new_name[1:] 86 return settername, propertyname 87 88 getter = re.compile(r"^(get|are|is)[A-Z]") 89 for class_ in classes: 90 methods = [(name,attr) for name,attr in list(class_.__dict__.items()) 91 if isSimpleGetter(attr) ] 92 setmethods = [(name,attr) for name,attr in list(class_.__dict__.items()) if callable(attr)] 93 getters = [] 94 for name,method in methods: 95 if getter.match(name): 96 getters.append((name,method)) 97 settername, propertyname = createNames(name) 98 setter = dict(setmethods).get(settername,None) 99 #print name, settername, "--->",propertyname,'(',method,',',setter,')' 100 setattr(class_,propertyname,property(method,setter)) 101 if not getters: continue 102 103 # We need to override the swig setattr function 104 # to get properties to work. 105 class_._property_names = set([name for name,method in getters]) 106 def _setattr_wrapper_(self,*args): 107 if name in class_._property_names: 108 object.__setattr__(self,*args) 109 else: 110 class_.__setattr__(self,*args) 111 class_.__setattr__ = _setattr_wrapper_ 112 113 createProperties() 114