diff MoinMoin/wikiutil.py @ 801:1f8976e01c3a

fix wrong import, make more use of MimeType class
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 19 May 2006 20:18:45 +0200
parents 28de47f4ff1a
children 7b2e550c9660
line wrap: on
line diff
--- a/MoinMoin/wikiutil.py	Fri May 19 19:17:12 2006 +0200
+++ b/MoinMoin/wikiutil.py	Fri May 19 20:18:45 2006 +0200
@@ -24,35 +24,6 @@
 CHILD_PREFIX_LEN = len(CHILD_PREFIX)
 
 #############################################################################
-### mimetype support
-#############################################################################
-import mimetypes
-guess_type = mimetypes.guess_type
-guess_extension = mimetypes.guess_extension
-
-_our_types = {
- # OpenOffice 2.x & others
- '.odt': 'application/vnd.oasis.opendocument.text',
- '.ods': 'application/vnd.oasis.opendocument.spreadsheet',
- '.odp': 'application/vnd.oasis.opendocument.presentation',
- '.odg': 'application/vnd.oasis.opendocument.graphics',
- '.odc': 'application/vnd.oasis.opendocument.chart',
- '.odf': 'application/vnd.oasis.opendocument.formula',
- '.odb': 'application/vnd.oasis.opendocument.database',
- '.odi': 'application/vnd.oasis.opendocument.image',
- '.odm': 'application/vnd.oasis.opendocument.text-master',
- '.ott': 'application/vnd.oasis.opendocument.text-template',
- '.ots': 'application/vnd.oasis.opendocument.spreadsheet-template',
- '.otp': 'application/vnd.oasis.opendocument.presentation-template',
- '.otg': 'application/vnd.oasis.opendocument.graphics-template',
-}
-[mimetypes.add_type(mimetype, ext, True) for ext, mimetype in _our_types.items()]
-
-def mimetype2modulename(mimetype):
-    """ convert an original mimetype to a string usable as python module name """
-    return mimetype.replace("/", "_").replace("-","_").replace(".", "_")
-
-#############################################################################
 ### Getting data from user/Sending data to user
 #############################################################################
 
@@ -790,33 +761,57 @@
     else:
         return u'["%s"]' % pagename
 
+#############################################################################
+### mimetype support
+#############################################################################
+import mimetypes
+
+MIMETYPES_MORE = {
+ # OpenOffice 2.x & other open document stuff
+ '.odt': 'application/vnd.oasis.opendocument.text',
+ '.ods': 'application/vnd.oasis.opendocument.spreadsheet',
+ '.odp': 'application/vnd.oasis.opendocument.presentation',
+ '.odg': 'application/vnd.oasis.opendocument.graphics',
+ '.odc': 'application/vnd.oasis.opendocument.chart',
+ '.odf': 'application/vnd.oasis.opendocument.formula',
+ '.odb': 'application/vnd.oasis.opendocument.database',
+ '.odi': 'application/vnd.oasis.opendocument.image',
+ '.odm': 'application/vnd.oasis.opendocument.text-master',
+ '.ott': 'application/vnd.oasis.opendocument.text-template',
+ '.ots': 'application/vnd.oasis.opendocument.spreadsheet-template',
+ '.otp': 'application/vnd.oasis.opendocument.presentation-template',
+ '.otg': 'application/vnd.oasis.opendocument.graphics-template',
+}
+[mimetypes.add_type(mimetype, ext, True) for ext, mimetype in MIMETYPES_MORE.items()]
+
+MIMETYPES_sanitize_mapping = {
+    # this stuff is text, but got application/* for unknown reasons
+    ('application', 'docbook+xml'): ('text', 'docbook'),
+    ('application', 'x-latex'): ('text', 'latex'),
+    ('application', 'x-tex'): ('text', 'tex'),
+    ('application', 'javascript'): ('text', 'javascript'),
+}
+
+MIMETYPES_spoil_mapping = {} # inverse mapping of above
+for key, value in MIMETYPES_sanitize_mapping.items():
+    MIMETYPES_spoil_mapping[value] = key
+
+
 # mimetype stuff ------------------------------------------------------------
 class MimeType(object):
     """ represents a mimetype like text/plain """
-    sanitize_mapping = {
-        # this stuff is text, but got application/* for unknown reasons
-        ('application', 'docbook+xml'): ('text', 'docbook'),
-        ('application', 'x-latex'): ('text', 'latex'),
-        ('application', 'x-tex'): ('text', 'tex'),
-        ('application', 'javascript'): ('text', 'javascript'),
-    }
-    spoil_mapping = {} # inverse mapping of above
     
     def __init__(self, mimestr=None, filename=None):
         self.major = self.minor = None # sanitized mime type and subtype
         self.params = {} # parameters like "charset" or others
         self.charset = None # this stays None until we know for sure!
 
-        for key, value in self.sanitize_mapping.items():
-            self.spoil_mapping[value] = key
-
         if mimestr:
             self.parse_mimetype(mimestr)
         elif filename:
             self.parse_filename(filename)
     
     def parse_filename(self, filename):
-        import mimetypes
         mtype, encoding = mimetypes.guess_type()
         if mtype is None:
             mtype = 'application/octet-stream'
@@ -873,13 +868,13 @@
             readable text, we will return some text/* mimetype, not application/*,
             because we need text/plain as fallback and not application/octet-stream.
         """
-        self.major, self.minor = self.sanitize_mapping.get((self.major, self.minor), (self.major, self.minor))
+        self.major, self.minor = MIMETYPES_sanitize_mapping.get((self.major, self.minor), (self.major, self.minor))
 
     def spoil(self):
         """ this returns something conformant to /etc/mime.type or IANA as a string,
             kind of inverse operation of sanitize(), but doesn't change self
         """
-        major, minor = self.spoil_mapping.get((self.major, self.minor), (self.major, self.minor))
+        major, minor = MIMETYPES_spoil_mapping.get((self.major, self.minor), (self.major, self.minor))
         return self.content_type(major, minor)
 
     def content_type(self, major=None, minor=None, charset=None, params=None):