summaryrefslogtreecommitdiff
path: root/Mailman/enum.py
diff options
context:
space:
mode:
authorbwarsaw2007-05-28 20:21:41 +0000
committerbwarsaw2007-05-28 20:21:41 +0000
commitb18f632faa6de17badabb3c6c7ba61752ac84c37 (patch)
tree8b444330b288c5dfc9b25be639d429abfaeb3d3d /Mailman/enum.py
parent5ff792b13599920527b48f92f8bad880668f8f26 (diff)
downloadmailman-b18f632faa6de17badabb3c6c7ba61752ac84c37.tar.gz
mailman-b18f632faa6de17badabb3c6c7ba61752ac84c37.tar.zst
mailman-b18f632faa6de17badabb3c6c7ba61752ac84c37.zip
Merge exp-elixir-branch to trunk. There is enough working to make me feel
confident the Elixir branch is ready to become mainline. Also, fewer branches makes for an easier migration to a dvcs. Don't expect much of the old test suite to work, or even for much of the old functionality to work. The changes here are disruptive enough to break higher level parts of Mailman. But that's okay because I am slowly building up a new and improved test suite, which will lead to a functional system again. For now, only the doctests in Mailman/docs (and their related test harnesses) will pass, but they all do pass. Note that Mailman/docs serve as system documentation first and unit tests second. You should be able to read the doctest files to understand the underlying data model. Other changes included in this merge: - Added the Mailman.ext extension package. - zope.interfaces uses to describe major components - SQLAlchemy/Elixir used as the database model - Top level doinstall target renamed to justinstall - 3rd-party packages are now installed in pythonlib/lib/python to be more compliant with distutils standards. This allows us to use just --home instead of all the --install-* options. - No longer need to include the email package or pysqlite, as Python 2.5 is required (and comes with both packages). - munepy package is included, for Python enums - IRosterSets are added as a way to manage a collection of IRosters. Roster sets are named so that we can maintain the indirection between mailing lists and rosters, where the two are maintained in different storages. - IMailingListRosters: remove_*_roster() -> delete_*_roster() - Remove IMember interface. - Utils.list_names() -> config.list_manager.names - fqdn_listname() takes an optional hostname argument. - Added a bunch of new exceptions used throughout the new interfaces. - Make LockFile a context manager for use with the 'with' statement.
Diffstat (limited to 'Mailman/enum.py')
-rw-r--r--Mailman/enum.py132
1 files changed, 0 insertions, 132 deletions
diff --git a/Mailman/enum.py b/Mailman/enum.py
deleted file mode 100644
index 893e988ba..000000000
--- a/Mailman/enum.py
+++ /dev/null
@@ -1,132 +0,0 @@
-# Copyright (C) 2004-2007 by the Free Software Foundation, Inc.
-#
-# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
-# USA.
-
-"""Enumeration meta class.
-
-To define your own enumeration, do something like:
-
->>> class Colors(Enum):
-... red = 1
-... green = 2
-... blue = 3
-
-Enum subclasses cannot be instantiated, but you can convert them to integers
-and from integers. Returned enumeration attributes are singletons and can be
-compared by identity only.
-"""
-
-COMMASPACE = ', '
-
-# Based on example by Jeremy Hylton
-# Modified and extended by Barry Warsaw
-
-
-
-class EnumMetaclass(type):
- def __init__(cls, name, bases, dict):
- # cls == the class being defined
- # name == the name of the class
- # bases == the class's bases
- # dict == the class attributes
- super(EnumMetaclass, cls).__init__(name, bases, dict)
- # Store EnumValues here for easy access.
- cls._enums = {}
- # Figure out the set of enum values on the base classes, to ensure
- # that we don't get any duplicate values (which would screw up
- # conversion from integer).
- for basecls in cls.__mro__:
- if hasattr(basecls, '_enums'):
- cls._enums.update(basecls._enums)
- # For each class attribute, create an EnumValue and store that back on
- # the class instead of the int. Skip Python reserved names. Also add
- # a mapping from the integer to the instance so we can return the same
- # object on conversion.
- for attr in dict:
- if not (attr.startswith('__') and attr.endswith('__')):
- intval = dict[attr]
- enumval = EnumValue(name, intval, attr)
- if intval in cls._enums:
- raise TypeError('Multiple enum values: %s' % enumval)
- # Store as an attribute on the class, and save the attr name
- setattr(cls, attr, enumval)
- cls._enums[intval] = attr
-
- def __getattr__(cls, name):
- if name == '__members__':
- return cls._enums.values()
- raise AttributeError(name)
-
- def __repr__(cls):
- enums = ['%s: %d' % (cls._enums[k], k) for k in sorted(cls._enums)]
- return '<%s {%s}>' % (cls.__name__, COMMASPACE.join(enums))
-
- def __iter__(cls):
- for i in sorted(cls._enums):
- yield getattr(cls, cls._enums[i])
-
- def __getitem__(cls, i):
- # i can be an integer or a string
- attr = cls._enums.get(i)
- if attr is None:
- # It wasn't an integer -- try attribute name
- try:
- return getattr(cls, i)
- except (AttributeError, TypeError):
- raise ValueError(i)
- return getattr(cls, attr)
-
- # Support both MyEnum[i] and MyEnum(i)
- __call__ = __getitem__
-
-
-
-class EnumValue(object):
- """Class to represent an enumeration value.
-
- EnumValue('Color', 'red', 12) prints as 'Color.red' and can be converted
- to the integer 12.
- """
- def __init__(self, classname, value, enumname):
- self._classname = classname
- self._value = value
- self._enumname = enumname
-
- def __repr__(self):
- return 'EnumValue(%s, %s, %d)' % (
- self._classname, self._enumname, self._value)
-
- def __str__(self):
- return self._enumname
-
- def __int__(self):
- return self._value
-
- # Support only comparison by identity. Yes, really raise
- # NotImplementedError instead of returning NotImplemented.
- def __eq__(self, other):
- raise NotImplementedError
-
- __ne__ = __eq__
- __lt__ = __eq__
- __gt__ = __eq__
- __le__ = __eq__
- __ge__ = __eq__
-
-
-
-class Enum:
- __metaclass__ = EnumMetaclass