From e360a07a21bce90545ccda5c81a8211fc978baea Mon Sep 17 00:00:00 2001 From: gwen Date: Fri, 1 Mar 2013 13:10:52 +0100 Subject: [PATCH] BaseType refactoring --- tiramisu/basetype.py | 57 +++++++++++++++++++++++++++++++------------- tiramisu/option.py | 9 +++---- tiramisu/setting.py | 2 +- 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/tiramisu/basetype.py b/tiramisu/basetype.py index 02d3550..92d2e71 100644 --- a/tiramisu/basetype.py +++ b/tiramisu/basetype.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- "base 'interface' types for option types" -# Copyright (C) 2012 Team tiramisu (see AUTHORS for all contributors) +# 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 @@ -21,7 +21,7 @@ # the whole pypy projet is under MIT licence # ____________________________________________________________ -class BaseType(object): +class CommonType(object): def has_properties(self): return bool(len(self.properties)) def has_property(self, propname): @@ -34,21 +34,44 @@ class BaseType(object): 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() -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') +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} -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') +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) diff --git a/tiramisu/option.py b/tiramisu/option.py index 88ce51e..386da9c 100644 --- a/tiramisu/option.py +++ b/tiramisu/option.py @@ -22,14 +22,15 @@ # ____________________________________________________________ import re from types import FunctionType -from tiramisu.basetype import HiddenBaseType, DisabledBaseType +from tiramisu.basetype import BaseType from tiramisu.error import (ConfigError, ConflictConfigError, NotFoundError, RequiresError, RequirementRecursionError, MandatoryError, PropertiesOptionError) from tiramisu.autolib import carry_out_calculation from tiramisu.setting import groups, owners -requires_actions = [('hide', 'show'), ('enable', 'disable'), ('freeze', 'unfreeze')] +requires_actions = [('hide', 'show'), ('enable', 'disable'), + ('freeze', 'unfreeze')] available_actions = [] reverse_actions = {} @@ -48,7 +49,7 @@ def valid_name(name): #____________________________________________________________ # -class Option(HiddenBaseType, DisabledBaseType): +class Option(BaseType): """ Abstract base class for configuration option's. @@ -373,7 +374,7 @@ class NetmaskOption(Option): # by now the validation is nothing but a string, use IPy instead return isinstance(value, str) -class OptionDescription(HiddenBaseType, DisabledBaseType): +class OptionDescription(BaseType): """Config's schema (organisation, group) and container of Options""" # the group_type is useful for filtering OptionDescriptions in a config group_type = groups.default diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 23d2157..0e989c6 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- "sets the options of the configuration objects Config object itself" -# Copyright (C) 2012 Team tiramisu (see AUTHORS for all contributors) +# 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