00001 __copyright__ = """ 00002 Copyright 2008 Sean Ross-Ross 00003 """ 00004 __license__ = """ 00005 This file is part of SLIMpy . 00006 00007 SLIMpy is free software: you can redistribute it and/or modify 00008 it under the terms of the GNU Lesser General Public License as published by 00009 the Free Software Foundation, either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 SLIMpy is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>. 00019 """ 00020 00021 00022 class InterfaceType( type ): 00023 00024 interface_methods = {} 00025 00026 def __new__( cls, *p, **kw ): 00027 00028 implementation = type.__new__( cls, *p, **kw ) 00029 00030 cls.check_implements( implementation ) 00031 00032 return implementation 00033 00034 @classmethod 00035 def check_implements( cls , implementation ): 00036 00037 for key, check in cls.interface_methods.items(): 00038 00039 if not hasattr( implementation, key ): 00040 raise InterfaceMustImplement( key ) 00041 00042 attr = getattr( implementation, key ) 00043 00044 if check is not None: 00045 check_result = check( attr ) 00046 if not check_result: 00047 raise InterfaceMustImplement( "%s is not correctly implemented" %key ) 00048 00049 return 00050 00051 class InterfaceMustImplement( Exception ): pass 00052