tiramisu/tiramisu/setting.py

238 lines
8.3 KiB
Python
Raw Normal View History

2012-11-19 10:45:03 +01:00
# -*- coding: utf-8 -*-
"sets the options of the configuration objects Config object itself"
2013-03-01 13:10:52 +01:00
# Copyright (C) 2012-2013 Team tiramisu (see AUTHORS for all contributors)
2012-11-19 10:45:03 +01:00
#
# 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 pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
2012-12-06 18:14:57 +01:00
class _const:
"""convenient class that emulates a module
2012-12-10 09:53:13 +01:00
and builds constants (that is, unique names)"""
class ConstError(TypeError): pass
2012-12-06 18:14:57 +01:00
def __setattr__(self, name, value):
if self.__dict__.has_key(name):
2012-12-10 09:53:13 +01:00
raise self.ConstError, "Can't rebind group (%s)"%name
2012-12-06 18:14:57 +01:00
self.__dict__[name] = value
def __delattr__(self, name):
if self.__dict__.has_key(name):
2012-12-10 09:53:13 +01:00
raise self.ConstError, "Can't unbind group (%s)"%name
2012-12-06 18:14:57 +01:00
raise NameError, name
2012-12-10 09:53:13 +01:00
# ____________________________________________________________
class GroupModule(_const):
"emulates a module to manage unique group (OptionDescription) names"
2012-12-10 14:38:25 +01:00
class GroupType(str):
2012-12-06 18:14:57 +01:00
"""allowed normal group (OptionDescription) names
*normal* means : groups that are not master
"""
pass
2012-12-10 14:38:25 +01:00
class DefaultGroupType(GroupType):
2012-12-10 14:10:05 +01:00
"""groups that are default (typically 'default')"""
pass
2012-12-10 14:38:25 +01:00
class MasterGroupType(GroupType):
2012-12-06 18:14:57 +01:00
"""allowed normal group (OptionDescription) names
*master* means : groups that have the 'master' attribute set
"""
pass
2012-12-10 09:53:13 +01:00
# setting.groups (emulates a module)
groups = GroupModule()
2012-12-06 18:14:57 +01:00
2012-12-10 09:53:13 +01:00
def populate_groups():
"populates the available groups in the appropriate namespaces"
groups.master = groups.MasterGroupType('master')
groups.default = groups.DefaultGroupType('default')
groups.family = groups.GroupType('family')
2012-12-10 09:53:13 +01:00
# names are in the module now
2012-12-06 18:14:57 +01:00
populate_groups()
# ____________________________________________________________
2012-12-10 14:10:05 +01:00
class OwnerModule(_const):
"""emulates a module to manage unique owner names.
owners are living in `Config._cfgimpl_value_owners`
"""
class Owner(str):
"""allowed owner names
"""
pass
class DefaultOwner(Owner):
"""groups that are default (typically 'default')"""
pass
# setting.owners (emulates a module)
owners = OwnerModule()
2012-12-10 14:10:05 +01:00
def populate_owners():
"""populates the available owners in the appropriate namespaces
- 'user' is the generic is the generic owner.
- 'default' is the config owner after init time
"""
setattr(owners, 'default', owners.DefaultOwner('default'))
setattr(owners,'user', owners.Owner('user'))
2012-12-10 14:38:25 +01:00
def add_owner(name):
"""
:param name: the name of the new owner
"""
setattr(owners, name, owners.Owner(name))
setattr(owners, 'add_owner', add_owner)
2012-12-10 14:10:05 +01:00
# names are in the module now
populate_owners()
2013-02-21 17:07:00 +01:00
class MultiTypeModule(_const):
class MultiType(str):
pass
class DefaultMultiType(MultiType):
pass
class MasterMultiType(MultiType):
pass
class SlaveMultiType(MultiType):
pass
multitypes = MultiTypeModule()
def populate_multitypes():
setattr(multitypes, 'default', multitypes.DefaultMultiType('default'))
setattr(multitypes, 'master', multitypes.MasterMultiType('master'))
setattr(multitypes, 'slave', multitypes.SlaveMultiType('slave'))
populate_multitypes()
2012-12-10 14:10:05 +01:00
#____________________________________________________________
2012-11-19 10:45:03 +01:00
class Setting():
"``Config()``'s configuration options"
# properties attribute: the name of a property enables this property
properties = ['hidden', 'disabled']
# overrides the validations in the acces of the option values
permissive = []
# a mandatory option must have a value that is not None
mandatory = True
frozen = True
# enables validation function for options if set
validator = False
2013-02-06 14:59:24 +01:00
# generic owner
2012-12-11 11:18:53 +01:00
owner = owners.user
2013-02-06 14:59:24 +01:00
# in order to freeze everything, not **only** the frozen options
everything_frozen = False
2013-03-26 10:29:49 +01:00
# enables at build time to raise an exception if the option's name
# has the name of a config's method
valid_opt_names = True
2012-12-11 11:18:53 +01:00
#____________________________________________________________
2012-11-19 10:45:03 +01:00
# properties methods
def has_properties(self):
"has properties means the Config's properties attribute is not empty"
return bool(len(self.properties))
2013-03-14 11:31:44 +01:00
def get_properties(self):
return self.properties
2012-11-19 10:45:03 +01:00
def has_property(self, propname):
"""has property propname in the Config's properties attribute
:param property: string wich is the name of the property"""
return propname in self.properties
def enable_property(self, propname):
"puts property propname in the Config's properties attribute"
if propname not in self.properties:
self.properties.append(propname)
def disable_property(self, propname):
"deletes property propname in the Config's properties attribute"
if self.has_property(propname):
self.properties.remove(propname)
2012-12-11 11:18:53 +01:00
#____________________________________________________________
2013-03-14 11:31:44 +01:00
def get_permissive(self):
return self.permissive
2012-11-19 10:45:03 +01:00
def set_permissive(self, permissive):
if not isinstance(permissive, list):
raise TypeError('permissive must be a list')
self.permissive = permissive
2013-02-06 14:59:24 +01:00
#____________________________________________________________
# complete freeze methods
def freeze_everything(self):
"""everything is frozen, not only the option that are tagged "frozen"
"""
self.everything_frozen = True
2012-11-19 10:45:03 +01:00
2013-02-06 14:59:24 +01:00
def un_freeze_everything(self):
"""everything is frozen, not only the option that are tagged "frozen"
"""
self.everything_frozen = False
def is_frozen_for_everything(self):
"""frozen for a whole config (not only the options
that have been set to frozen)"""
return self.everything_frozen
#____________________________________________________________
2012-11-19 10:45:03 +01:00
def read_only(self):
"convenience method to freeze, hidde and disable"
2013-02-06 14:59:24 +01:00
self.freeze_everything()
self.freeze() # can be usefull...
2012-11-19 10:45:03 +01:00
self.disable_property('hidden')
self.enable_property('disabled')
self.mandatory = True
self.validator = True
def read_write(self):
"convenience method to freeze, hidde and disable"
2013-02-06 14:59:24 +01:00
self.un_freeze_everything()
2012-11-19 10:45:03 +01:00
self.freeze()
self.enable_property('hidden')
self.enable_property('disabled')
self.mandatory = False
self.validator = False
2012-11-19 10:45:03 +01:00
def non_mandatory(self):
"""mandatory at the Config level means that the Config raises an error
if a mandatory option is found"""
self.mandatory = False
def mandatory(self):
"""mandatory at the Config level means that the Config raises an error
if a mandatory option is found"""
self.mandatory = True
def is_mandatory(self):
"all mandatory Options shall have a value"
return self.mandatory
def freeze(self):
"cannot modify the frozen `Option`'s"
self.frozen = True
def unfreeze(self):
"can modify the Options that are frozen"
self.frozen = False
def is_frozen(self):
"freeze flag at Config level"
return self.frozen
def setowner(self, owner):
2012-11-19 10:45:03 +01:00
":param owner: sets the default value for owner at the Config level"
2012-12-11 11:18:53 +01:00
if not isinstance(owner, owners.Owner):
raise TypeError("invalid generic owner {0}".format(str(owner)))
2012-11-19 10:45:03 +01:00
self.owner = owner
def getowner(self):
2012-12-11 11:18:53 +01:00
return self.owner