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 rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence # the whole pypy projet is under MIT licence
# ____________________________________________________________ # ____________________________________________________________
class _const: class _const:
"""convenient class that emulates a module """convenient class that emulates a module
and builds constants (that is, unique group names)""" and builds constants (that is, unique names)"""
class GroupError(TypeError): pass class ConstError(TypeError): pass
def __setattr__(self, name, value): def __setattr__(self, name, value):
if self.__dict__.has_key(name): 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 self.__dict__[name] = value
def __delattr__(self, name): def __delattr__(self, name):
if self.__dict__.has_key(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 raise NameError, name
# ____________________________________________________________
groups = _const() class GroupModule(_const):
def populate_groups(): "emulates a module to manage unique group (OptionDescription) names"
"populates the available groups in the appropriate namespaces"
_available_group_names = ('default', 'family', 'group')
_available_groups_with_a_master = ('group', )
class GroupName(str): class GroupName(str):
"""allowed normal group (OptionDescription) names """allowed normal group (OptionDescription) names
*normal* means : groups that are not master *normal* means : groups that are not master
""" """
pass pass
class MasterGroupName(GroupName): class MasterGroupName(GroupName):
"""allowed normal group (OptionDescription) names """allowed normal group (OptionDescription) names
*master* means : groups that have the 'master' attribute set *master* means : groups that have the 'master' attribute set
""" """
pass pass
# setting.groups (emulates a module)
groups = GroupModule()
groups.GroupName = GroupName def populate_groups():
groups.MasterGroupName = MasterGroupName "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 # populates normal or master groups
for grp in _available_group_names: for grp in _available_group_names:
if grp in _available_groups_with_a_master: if grp in _available_groups_with_a_master:
setattr(groups, grp, MasterGroupName(grp)) setattr(groups, grp, groups.MasterGroupName(grp))
else: else:
setattr(groups, grp, GroupName(grp)) setattr(groups, grp, groups.GroupName(grp))
# names are in the module now
populate_groups() populate_groups()
# ____________________________________________________________ # ____________________________________________________________