# -*- coding: utf-8 -*- "sets the options of the configuration objects Config object itself" # Copyright (C) 2012 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 group names)""" class GroupError(TypeError): pass def __setattr__(self, name, value): if self.__dict__.has_key(name): raise self.GroupError, "Can't rebind group (%s)"%name self.__dict__[name] = value def __delattr__(self, name): if self.__dict__.has_key(name): raise self.GroupError, "Can't unbind group (%s)"%name raise NameError, name groups = _const() def populate_groups(): "populates the available groups in the appropriate namespaces" _available_group_names = ('default', 'family', 'group') _available_groups_with_a_master = ('group', ) class GroupName(str): """allowed normal group (OptionDescription) names *normal* means : groups that are not master """ pass class MasterGroupName(GroupName): """allowed normal group (OptionDescription) names *master* means : groups that have the 'master' attribute set """ pass groups.GroupName = GroupName groups.MasterGroupName = MasterGroupName # populates normal or master groups for grp in _available_group_names: if grp in _available_groups_with_a_master: setattr(groups, grp, MasterGroupName(grp)) else: setattr(groups, grp, GroupName(grp)) populate_groups() # ____________________________________________________________ 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 # generic owner. 'default' is the general config owner after init time owner = 'user' # ____________________________________________________________ # properties methods def has_properties(self): "has properties means the Config's properties attribute is not empty" return bool(len(self.properties)) 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) def set_permissive(self, permissive): if not isinstance(permissive, list): raise TypeError('permissive must be a list') self.permissive = permissive def read_only(self): "convenience method to freeze, hidde and disable" self.freeze() 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" self.freeze() self.enable_property('hidden') self.enable_property('disabled') self.mandatory = False 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 set_owner(self, owner): ":param owner: sets the default value for owner at the Config level" self.owner = owner # Setting is actually a singleton settings = Setting()