tiramisu/tiramisu/basetype.py
2013-03-01 13:10:52 +01:00

78 lines
2.9 KiB
Python

# -*- coding: utf-8 -*-
"base 'interface' types for option types"
# Copyright (C) 2012-2013 Team tiramisu (see AUTHORS for all contributors)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# The original `Config` design model is unproudly borrowed from
# the rough gus of pypy: pypy: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
class CommonType(object):
def has_properties(self):
return bool(len(self.properties))
def has_property(self, propname):
return propname in self.properties
def get_properties(self):
return self.properties
def add_property(self, propname):
if not propname in self.properties:
self.properties.append(propname)
def del_property(self, propname):
if self.has_property(propname):
self.properties.remove(propname)
#class HiddenBaseType(BaseType):
# def hide(self):
# self.add_property('hidden')
# def show(self):
# self.del_property('hidden')
# def _is_hidden(self):
# # dangerous method: how an Option() can determine its status by itself ?
# return self.has_property('hidden')
#class DisabledBaseType(BaseType):
# def disable(self):
# self.add_property('disabled')
# def enable(self):
# self.del_property('disabled')
# def _is_disabled(self):
# return self.has_property('disabled')
# commonly used properties. Option and OptionDescription will have these methods
common_properties = {'hidden': ('hide', 'show', '_is_hidden'),
'disabled': ('disable', 'enable', '_is_disabled')
}
basetype_methods = dict()
def build_properties(prop, add_prop, del_prop, is_prop):
def add_property(self):
self.add_property(prop)
def del_property(self):
self.del_property(prop)
def is_property(self):
return self.has_property(prop)
return {add_prop:add_property, del_prop:del_property,
is_prop:is_property}
for propname, meth_names in common_properties.items():
basetype_methods.update(build_properties(propname, meth_names[0], meth_names[1],
meth_names[2]))
BaseType = type('BaseType', (CommonType,), basetype_methods)