changeset 1928:b97f0a329819

base MoinMoin.util.mime.Type on collections.namedtuple Besides reducing code, this also gives Type a total order.
author Cheer Xiao <xiaqqaix@gmail.com>
date Thu, 31 Jan 2013 14:30:15 +0800
parents f0b346ac6338
children 95101fe50e49
files MoinMoin/converter/_wiki_macro.py MoinMoin/items/content.py MoinMoin/util/mime.py
diffstat 3 files changed, 25 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/converter/_wiki_macro.py	Sun Jan 20 23:36:37 2013 +0100
+++ b/MoinMoin/converter/_wiki_macro.py	Thu Jan 31 14:30:15 2013 +0800
@@ -150,7 +150,7 @@
             type = Type(name)
         else:
             type = Type(type='x-moin', subtype='format', parameters={'name': name})
-        logging.debug("parser type: %r" % type)
+        logging.debug("parser type: %r" % (type, ))
 
         elem = moin_page.part(attrib={moin_page.content_type: type})
 
--- a/MoinMoin/items/content.py	Sun Jan 20 23:36:37 2013 +0100
+++ b/MoinMoin/items/content.py	Thu Jan 31 14:30:15 2013 +0800
@@ -82,11 +82,7 @@
 
         def __lt__(self, other):
             if isinstance(other, self.__class__):
-                if self.content_type != other.content_type:
-                    return other.content_type.issupertype(self.content_type)
-                if self.priority != other.priority:
-                    return self.priority < other.priority
-                return False
+                return (self.content_type, self.priority) < (other.content_type, other.priority)
             return NotImplemented
 
     def __init__(self, group_names):
--- a/MoinMoin/util/mime.py	Sun Jan 20 23:36:37 2013 +0100
+++ b/MoinMoin/util/mime.py	Thu Jan 31 14:30:15 2013 +0800
@@ -6,7 +6,9 @@
 """
 
 
-class Type(object):
+from collections import namedtuple
+
+class Type(namedtuple('Type', 'type subtype parameters')):
     """
     :ivar type: Type part
     :type type: unicode
@@ -18,7 +20,7 @@
 
     __token_allowed = s = frozenset(r"""!#$%&'*+-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~""")
 
-    def __init__(self, _type=None, type=None, subtype=None, parameters=None):
+    def __new__(cls, _type=None, type=None, subtype=None, parameters=None):
         """
         :param _type: Type object or string representation
         :keyword type: Type part
@@ -26,36 +28,28 @@
         :keyword parameters: Parameters part
         """
         if isinstance(_type, Type):
-            self.type = _type.type
-            self.subtype = _type.subtype
-            self.parameters = _type.parameters.copy()
-
+            new_type, new_subtype, new_parameters = _type.type, _type.subtype, _type.parameters.copy()
+        elif _type:
+            new_type, new_subtype, new_parameters = cls._parse(_type)
         else:
-            self.type = self.subtype = None
-            self.parameters = {}
-
-            if _type:
-                self._parse(_type)
+            new_type = new_subtype = None
+            new_parameters = {}
 
         if type is not None:
-            self.type = type
+            new_type = type
         if subtype is not None:
-            self.subtype = subtype
+            new_subtype = subtype
         if parameters is not None:
-            self.parameters.update(parameters)
+            new_parameters.update(parameters)
+
+        return super(Type, cls).__new__(cls, new_type, new_subtype, new_parameters)
 
     def __eq__(self, other):
         if isinstance(other, basestring):
             return self.__eq__(self.__class__(other))
 
         if isinstance(other, Type):
-            if self.type != other.type:
-                return False
-            if self.subtype != other.subtype:
-                return False
-            if self.parameters != other.parameters:
-                return False
-            return True
+            return super(Type, self).__eq__(other)
 
         return NotImplemented
 
@@ -65,14 +59,6 @@
             return ret
         return not ret
 
-    def __repr__(self):
-        return '<{0} object: type: {1!r}; subtype: {2!r}; parameters: {3!r}>'.format(
-                self.__class__.__name__,
-                self.type,
-                self.subtype,
-                self.parameters,
-                )
-
     def __unicode__(self):
         ret = [u'{0}/{1}'.format(self.type or '*', self.subtype or '*')]
 
@@ -92,20 +78,24 @@
                 return False
         return True
 
-    def _parse(self, type):
+    @classmethod
+    def _parse(cls, type):
         parts = type.split(';')
 
         type, subtype = parts[0].strip().lower().split('/', 1)
 
-        self.type = type != '*' and type or None
-        self.subtype = subtype != '*' and subtype or None
+        type = type != '*' and type or None
+        subtype = subtype != '*' and subtype or None
+        parameters = {}
 
         for param in parts[1:]:
             key, value = param.strip().split('=', 1)
             # remove quotes
             if value[0] == '"' and value[-1] == '"':
                 value = value[1:-1]
-            self.parameters[key.lower()] = value
+            parameters[key.lower()] = value
+
+        return type, subtype, parameters
 
     def issupertype(self, other):
         """