tiramisu/tiramisu/setting.py

245 lines
8.2 KiB
Python

# -*- coding: utf-8 -*-
"sets the options of the configuration objects Config object itself"
# 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 pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
class _const:
"""convenient class that emulates a module
and builds constants (that is, unique names)"""
class ConstError(TypeError):
pass
def __setattr__(self, name, value):
if name in self.__dict__:
raise self.ConstError, "Can't rebind group ({})".format(name)
self.__dict__[name] = value
def __delattr__(self, name):
if name in self.__dict__:
raise self.ConstError, "Can't unbind group ({})".format(name)
raise NameError(name)
# ____________________________________________________________
class GroupModule(_const):
"emulates a module to manage unique group (OptionDescription) names"
class GroupType(str):
"""allowed normal group (OptionDescription) names
*normal* means : groups that are not master
"""
pass
class DefaultGroupType(GroupType):
"""groups that are default (typically 'default')"""
pass
class MasterGroupType(GroupType):
"""allowed normal group (OptionDescription) names
*master* means : groups that have the 'master' attribute set
"""
pass
# setting.groups (emulates a module)
groups = GroupModule()
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')
# names are in the module now
populate_groups()
# ____________________________________________________________
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()
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'))
def add_owner(name):
"""
:param name: the name of the new owner
"""
setattr(owners, name, owners.Owner(name))
setattr(owners, 'add_owner', add_owner)
# names are in the module now
populate_owners()
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()
#____________________________________________________________
class Setting(object):
"``Config()``'s configuration options"
__slots__ = ('properties', 'permissives', 'owner')
def __init__(self):
# properties attribute: the name of a property enables this property
# key is None for global properties
self.properties = {None: []} # ['hidden', 'disabled', 'mandatory', 'frozen', 'validator']}
# permissive properties
self.permissives = {}
# generic owner
self.owner = owners.user
#____________________________________________________________
# properties methods
def has_properties(self, opt=None):
"has properties means the Config's properties attribute is not empty"
return bool(len(self.get_properties(opt)))
def get_properties(self, opt=None):
if opt is None:
default = []
else:
default = list(opt._properties)
return self.properties.get(opt, default)
def has_property(self, propname, opt=None):
"""has property propname in the Config's properties attribute
:param property: string wich is the name of the property"""
return propname in self.get_properties(opt)
def enable_property(self, propname):
"puts property propname in the Config's properties attribute"
props = self.get_properties()
if propname not in props:
props.append(propname)
self.set_properties(props)
def disable_property(self, propname):
"deletes property propname in the Config's properties attribute"
props = self.get_properties()
if propname in props:
props.remove(propname)
self.set_properties(props)
def set_properties(self, properties, opt=None):
"""save properties for specified opt
(never save properties if same has option properties)
"""
if opt is None:
self.properties[opt] = properties
else:
if opt._properties == properties:
if opt in self.properties:
del(self.properties[opt])
else:
self.properties[opt] = properties
def add_property(self, propname, opt):
properties = self.get_properties(opt)
if not propname in properties:
properties.append(propname)
self.set_properties(properties, opt)
def del_property(self, propname, opt):
properties = self.get_properties(opt)
if propname in properties:
properties.remove(propname)
self.set_properties(properties, opt)
#____________________________________________________________
def get_permissive(self, config=None):
return self.permissives.get(config, [])
def set_permissive(self, permissive, config=None):
if not isinstance(permissive, list):
raise TypeError('permissive must be a list')
self.permissives[config] = permissive
#____________________________________________________________
def setowner(self, owner):
":param owner: sets the default value for owner at the Config level"
if not isinstance(owner, owners.Owner):
raise TypeError("invalid generic owner {0}".format(str(owner)))
self.owner = owner
def getowner(self):
return self.owner
#____________________________________________________________
def read_only(self):
"convenience method to freeze, hidde and disable"
self.enable_property('everything_frozen')
self.enable_property('frozen') # can be usefull...
self.disable_property('hidden')
self.enable_property('disabled')
self.enable_property('mandatory')
self.enable_property('validator')
self.disable_property('permissive')
def read_write(self):
"convenience method to freeze, hidde and disable"
self.disable_property('everything_frozen')
self.enable_property('frozen') # can be usefull...
self.enable_property('hidden')
self.enable_property('disabled')
self.disable_property('mandatory')
self.disable_property('validator')
self.disable_property('permissive')