annotate MoinMoin/util/mimetype.py @ 2657:080673c3e60a

Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
author Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
date Sun, 06 Jul 2014 09:25:31 +0530
parents 8300781142e5
children
rev   line source
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 # Copyright: 2005-2011 MoinMoin:ThomasWaldmann
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4 """
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 MoinMoin - mimetype support
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 """
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 from __future__ import absolute_import, division
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 import mimetypes
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12
1973
286e315935ae refactor usage of constants
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
13 from MoinMoin.constants.contenttypes import PARSER_TEXT_MIMETYPE
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14
1104
a4db041df27a fixes mimetypes read from registry issue on windows, thanks to Nick Rioux
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 969
diff changeset
15 # prevents unexpected results on Windows
a4db041df27a fixes mimetypes read from registry issue on windows, thanks to Nick Rioux
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 969
diff changeset
16 # see http://bugs.python.org/issue10551
a4db041df27a fixes mimetypes read from registry issue on windows, thanks to Nick Rioux
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 969
diff changeset
17 mimetypes.init(mimetypes.knownfiles)
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19 MIMETYPES_MORE = {
2011
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
20 # OpenOffice 2.x & other open document stuff
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
21 '.odt': 'application/vnd.oasis.opendocument.text',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
22 '.ods': 'application/vnd.oasis.opendocument.spreadsheet',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
23 '.odp': 'application/vnd.oasis.opendocument.presentation',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
24 '.odg': 'application/vnd.oasis.opendocument.graphics',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
25 '.odc': 'application/vnd.oasis.opendocument.chart',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
26 '.odf': 'application/vnd.oasis.opendocument.formula',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
27 '.odb': 'application/vnd.oasis.opendocument.database',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
28 '.odi': 'application/vnd.oasis.opendocument.image',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
29 '.odm': 'application/vnd.oasis.opendocument.text-master',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
30 '.ott': 'application/vnd.oasis.opendocument.text-template',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
31 '.ots': 'application/vnd.oasis.opendocument.spreadsheet-template',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
32 '.otp': 'application/vnd.oasis.opendocument.presentation-template',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
33 '.otg': 'application/vnd.oasis.opendocument.graphics-template',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
34 # some systems (like Mac OS X) don't have some of these:
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
35 '.patch': 'text/x-diff',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
36 '.diff': 'text/x-diff',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
37 '.py': 'text/x-python',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
38 '.cfg': 'text/plain',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
39 '.conf': 'text/plain',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
40 '.irc': 'text/plain',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
41 '.md5': 'text/plain',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
42 '.csv': 'text/csv',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
43 '.rst': 'text/x-rst',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
44 '.flv': 'video/x-flv',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
45 '.wmv': 'video/x-ms-wmv',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
46 '.wma': 'audio/x-ms-wma',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
47 '.swf': 'application/x-shockwave-flash',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
48 '.awd': 'application/x-anywikidraw',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
49 '.twd': 'application/x-twikidraw',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
50 '.swd': 'application/x-svgdraw',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
51 '.dbx': 'application/docbook+xml',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
52 '.moin': 'text/x.moin.wiki',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
53 '.creole': 'text/x.moin.creole',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
54 '.mediawiki': 'text/x-mediawiki',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
55 '.ico': 'image/x-icon',
791bdedb0c20 fix lots of pep8 issues, mainly E12x
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
56 '.svg': 'image/svg+xml',
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
57 }
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59 # add all mimetype patterns of pygments
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 import pygments.lexers
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62 for name, short, patterns, mime in pygments.lexers.get_all_lexers():
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 for pattern in patterns:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 if pattern.startswith('*.') and mime:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 MIMETYPES_MORE[pattern[1:]] = mime[0]
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 [mimetypes.add_type(mimetype, ext, True) for ext, mimetype in MIMETYPES_MORE.items()]
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 MIMETYPES_sanitize_mapping = {
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 # this stuff is text, but got application/* for unknown reasons
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 ('application', 'docbook+xml'): ('text', 'docbook'),
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 ('application', 'x-latex'): ('text', 'latex'),
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 ('application', 'x-tex'): ('text', 'tex'),
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 ('application', 'javascript'): ('text', 'javascript'),
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 }
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
77 MIMETYPES_spoil_mapping = {} # inverse mapping of above
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78 for _key, _value in MIMETYPES_sanitize_mapping.items():
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79 MIMETYPES_spoil_mapping[_value] = _key
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 class MimeType(object):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83 """ represents a mimetype like text/plain """
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
84
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
85 def __init__(self, mimestr=None, filename=None):
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
86 self.major = self.minor = None # sanitized mime type and subtype
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
87 self.params = {} # parameters like "charset" or others
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
88 self.charset = None # this stays None until we know for sure!
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89 self.raw_mimestr = mimestr
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
90 self.filename = filename
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
91 if mimestr:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
92 self.parse_mimetype(mimestr)
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
93 elif filename:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
94 self.parse_filename(filename)
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
95
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96 def parse_filename(self, filename):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97 mtype, encoding = mimetypes.guess_type(filename)
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98 if mtype is None:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
99 mtype = 'application/octet-stream'
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
100 self.parse_mimetype(mtype)
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
101
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
102 def parse_mimetype(self, mimestr):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
103 """ take a string like used in content-type and parse it into components,
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
104 alternatively it also can process some abbreviated string like "wiki"
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
105 """
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
106 parameters = mimestr.split(";")
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
107 parameters = [p.strip() for p in parameters]
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
108 mimetype, parameters = parameters[0], parameters[1:]
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
109 mimetype = mimetype.split('/')
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
110 if len(mimetype) >= 2:
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
111 major, minor = mimetype[:2] # we just ignore more than 2 parts
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
112 else:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
113 major, minor = self.parse_format(mimetype[0])
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
114 self.major = major.lower()
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
115 self.minor = minor.lower()
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
116 for param in parameters:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
117 key, value = param.split('=')
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
118 if value[0] == '"' and value[-1] == '"': # remove quotes
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
119 value = value[1:-1]
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
120 self.params[key.lower()] = value
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
121 if 'charset' in self.params:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
122 self.charset = self.params['charset'].lower()
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
123 self.sanitize()
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
124
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
125 def parse_format(self, format):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
126 """ maps from what we currently use on-page in a #format xxx processing
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
127 instruction to a sanitized mimetype major, minor tuple.
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
128 can also be user later for easier entry by the user, so he can just
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
129 type "wiki" instead of "text/x.moin.wiki".
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
130 """
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
131 format = format.lower()
1973
286e315935ae refactor usage of constants
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1397
diff changeset
132 if format in PARSER_TEXT_MIMETYPE:
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
133 mimetype = 'text', format
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
134 else:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
135 mapping = {
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
136 'wiki': ('text', 'x.moin.wiki'),
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
137 'irc': ('text', 'irssi'),
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
138 }
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
139 try:
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
140 mimetype = mapping[format]
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
141 except KeyError:
969
5bf6d7a2ffcf Convert all %r, %s, %x, %d, %i, %o to new Format String Syntax
Vedran Mileti? <rivanvx@gmail.com>
parents: 959
diff changeset
142 mimetype = 'text', 'x-{0}'.format(format)
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
143 return mimetype
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
144
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
145 def sanitize(self):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
146 """ convert to some representation that makes sense - this is not necessarily
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
147 conformant to /etc/mime.types or IANA listing, but if something is
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
148 readable text, we will return some ``text/*`` mimetype, not ``application/*``,
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
149 because we need text/plain as fallback and not application/octet-stream.
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
150 """
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
151 self.major, self.minor = MIMETYPES_sanitize_mapping.get((self.major, self.minor), (self.major, self.minor))
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
152
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
153 def spoil(self):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
154 """ this returns something conformant to /etc/mime.type or IANA as a string,
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
155 kind of inverse operation of sanitize(), but doesn't change self
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
156 """
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
157 major, minor = MIMETYPES_spoil_mapping.get((self.major, self.minor), (self.major, self.minor))
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
158 return self.content_type(major, minor)
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
159
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
160 def content_type(self, major=None, minor=None, charset=None, params=None):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
161 """ return a string suitable for Content-Type header
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
162 """
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
163 major = major or self.major
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
164 minor = minor or self.minor
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
165 params = params or self.params or {}
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
166 if major == 'text':
946
001077ec4540 MimeType: do not make potentially wrong assumptions that charset is utf-8
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 255
diff changeset
167 charset = charset or self.charset
001077ec4540 MimeType: do not make potentially wrong assumptions that charset is utf-8
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 255
diff changeset
168 if charset:
001077ec4540 MimeType: do not make potentially wrong assumptions that charset is utf-8
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 255
diff changeset
169 params['charset'] = charset
969
5bf6d7a2ffcf Convert all %r, %s, %x, %d, %i, %o to new Format String Syntax
Vedran Mileti? <rivanvx@gmail.com>
parents: 959
diff changeset
170 mimestr = "{0}/{1}".format(major, minor)
5bf6d7a2ffcf Convert all %r, %s, %x, %d, %i, %o to new Format String Syntax
Vedran Mileti? <rivanvx@gmail.com>
parents: 959
diff changeset
171 params = ['{0}="{1}"'.format(key.lower(), value) for key, value in params.items()]
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
172 params.insert(0, mimestr)
2023
8300781142e5 make contenttype values pass the validator (add charset for text), fix namespace and name validators
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 2011
diff changeset
173 return ";".join(params)
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
174
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
175 def mime_type(self):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
176 """ return a string major/minor only, no params """
969
5bf6d7a2ffcf Convert all %r, %s, %x, %d, %i, %o to new Format String Syntax
Vedran Mileti? <rivanvx@gmail.com>
parents: 959
diff changeset
177 return "{0}/{1}".format(self.major, self.minor)
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
178
255
76a4e3cc8705 add +download view for just downloading a revision with forced content-disposition: attachment
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 173
diff changeset
179 def as_attachment(self, cfg):
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
180 # for dangerous files (like .html), when we are in danger of cross-site-scripting attacks,
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
181 # we just let the user store them to disk ('attachment').
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
182 # For safe files, we directly show them inline (this also works better for IE).
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
183 mime_type = self.mime_type()
255
76a4e3cc8705 add +download view for just downloading a revision with forced content-disposition: attachment
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 173
diff changeset
184 return mime_type in cfg.mimetypes_xss_protect
173
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
185
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
186 def module_name(self):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
187 """ convert this mimetype to a string useable as python module name,
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
188 we yield the exact module name first and then proceed to shorter
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
189 module names (useful for falling back to them, if the more special
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
190 module is not found) - e.g. first "text_python", next "text".
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
191 Finally, we yield "application_octet_stream" as the most general
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
192 mimetype we have.
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
193
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
194 Hint: the fallback handler module for text/* should be implemented
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
195 in module "text" (not "text_plain")
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
196 """
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
197 mimetype = self.mime_type()
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
198 modname = mimetype.replace("/", "_").replace("-", "_").replace(".", "_")
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
199 fragments = modname.split('_')
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
200 for length in range(len(fragments), 1, -1):
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
201 yield "_".join(fragments[:length])
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
202 yield self.raw_mimestr
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
203 yield fragments[0]
89f50aed143f move mimetype related code from wikiutil to util.mimetype
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
204 yield "application_octet_stream"