constants heritage

This commit is contained in:
gwen 2012-12-10 09:53:13 +01:00
parent e118f07539
commit 07d3cb1037
1 changed files with 16 additions and 17 deletions

View File

@ -20,48 +20,47 @@
# 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
and builds constants (that is, unique names)"""
class ConstError(TypeError): pass
def __setattr__(self, name, value):
if self.__dict__.has_key(name):
raise self.GroupError, "Can't rebind group (%s)"%name
raise self.ConstError, "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 self.ConstError, "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 GroupModule(_const):
"emulates a module to manage unique group (OptionDescription) names"
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
# setting.groups (emulates a module)
groups = GroupModule()
groups.GroupName = GroupName
groups.MasterGroupName = MasterGroupName
def populate_groups():
"populates the available groups in the appropriate namespaces"
_available_group_names = ('default', 'family', 'group')
_available_groups_with_a_master = ('group', )
# populates normal or master groups
for grp in _available_group_names:
if grp in _available_groups_with_a_master:
setattr(groups, grp, MasterGroupName(grp))
setattr(groups, grp, groups.MasterGroupName(grp))
else:
setattr(groups, grp, GroupName(grp))
setattr(groups, grp, groups.GroupName(grp))
# names are in the module now
populate_groups()
# ____________________________________________________________