Merge lp:~thekorn/zeitgeist/fix-586524-mimetypes-api into lp:zeitgeist/0.1

Proposed by Markus Korn
Status: Merged
Merged at revision: 1567
Proposed branch: lp:~thekorn/zeitgeist/fix-586524-mimetypes-api
Merge into: lp:zeitgeist/0.1
Diff against target: 270 lines (+249/-1)
3 files modified
test/mimetypes-test.py (+47/-0)
zeitgeist/Makefile.am (+2/-1)
zeitgeist/mimetypes.py (+200/-0)
To merge this branch: bzr merge lp:~thekorn/zeitgeist/fix-586524-mimetypes-api
Reviewer Review Type Date Requested Status
Zeitgeist Framework Team Pending
Review via email: mp+34397@code.launchpad.net

Commit message

Added a new helper module called zeitgeist.mimetypes which basically
provides two functions:
  * interpretation_for_mimetype(), which tries to get a suitable
    interpretation for a given mimetype
  * manifestation_for_uri(), which tries to lookup a manifestation for the
    given uri

Description of the change

This branch adds a zeitgeist.mimetypes module as a fix of bug 586524.
I tried to follow the logic of the existing code in libzeitgeist as much as possible, also the testcases do exactly the same checks.
See my proposed commit message for more information.

I added this RegExpr helper class to the module to make Michal's work easier to write a tool which generates the corresponding libzeitgeist code.

To post a comment you must log in.
1565. By Markus Korn

removed whitespace

Revision history for this message
Seif Lotfy (seif) wrote :

Perfectly done. However I have to complain about the naming of the methods
IMHO interpretation_for_mimetype should be get_interpretation_for_mimetype ame goes for manifestation_for_uri :)

Revision history for this message
Siegfried Gevatter (rainct) wrote :

I agree with Seif.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'test/mimetypes-test.py'
2--- test/mimetypes-test.py 1970-01-01 00:00:00 +0000
3+++ test/mimetypes-test.py 2010-09-02 12:55:54 +0000
4@@ -0,0 +1,47 @@
5+#!/usr/bin/python
6+# -.- coding: utf-8 -.-
7+
8+# Update python path to use local zeitgeist module
9+import sys
10+import os
11+import unittest
12+
13+sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
14+from zeitgeist.mimetypes import interpretation_for_mimetype, manifestation_for_uri
15+from zeitgeist.datamodel import Interpretation, Manifestation
16+
17+
18+class MimetypesTest(unittest.TestCase):
19+
20+ def test_textplain(self):
21+ self.assertEquals(
22+ Interpretation.TEXT_DOCUMENT, interpretation_for_mimetype("text/plain")
23+ )
24+
25+ def test_mime_none(self):
26+ self.assertEquals(None, interpretation_for_mimetype("boobarbaz"))
27+
28+ def test_mime_regex(self):
29+ self.assertEquals(
30+ Interpretation.DOCUMENT,
31+ interpretation_for_mimetype("application/x-applix-FOOOOBAR!")
32+ )
33+ self.assertEquals(
34+ Interpretation.SPREADSHEET,
35+ interpretation_for_mimetype("application/x-applix-spreadsheet")
36+ )
37+
38+class SchemeTest(unittest.TestCase):
39+
40+ def test_scheme_file(self):
41+ self.assertEquals(
42+ Manifestation.FILE_DATA_OBJECT,
43+ manifestation_for_uri("file:///tmp/foo.txt")
44+ )
45+
46+ def test_scheme_none(self):
47+ self.assertEquals(None, manifestation_for_uri("boo:///tmp/foo.txt"))
48+
49+
50+if __name__ == '__main__':
51+ unittest.main()
52
53=== modified file 'zeitgeist/Makefile.am'
54--- zeitgeist/Makefile.am 2009-11-27 20:32:54 +0000
55+++ zeitgeist/Makefile.am 2010-09-02 12:55:54 +0000
56@@ -3,7 +3,8 @@
57 app_PYTHON = \
58 __init__.py \
59 datamodel.py \
60- client.py
61+ client.py \
62+ mimetypes.py
63
64 nodist_app_PYTHON = _config.py
65
66
67=== added file 'zeitgeist/mimetypes.py'
68--- zeitgeist/mimetypes.py 1970-01-01 00:00:00 +0000
69+++ zeitgeist/mimetypes.py 2010-09-02 12:55:54 +0000
70@@ -0,0 +1,200 @@
71+# -.- coding: utf-8 -.-
72+
73+# Zeitgeist
74+#
75+# Copyright © 2010 Markus Korn <thekorn@gmx.de>
76+#
77+# This program is free software: you can redistribute it and/or modify
78+# it under the terms of the GNU Lesser General Public License as published by
79+# the Free Software Foundation, either version 3 of the License, or
80+# (at your option) any later version.
81+#
82+# This program is distributed in the hope that it will be useful,
83+# but WITHOUT ANY WARRANTY; without even the implied warranty of
84+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
85+# GNU Lesser General Public License for more details.
86+#
87+# You should have received a copy of the GNU Lesser General Public License
88+# along with this program. If not, see <http://www.gnu.org/licenses/>.
89+
90+import re
91+
92+from datamodel import Interpretation, Manifestation
93+
94+__all__ = [
95+ "interpretation_for_mimetype",
96+ "manifestation_for_uri",
97+]
98+
99+class RegExpr(object):
100+ """ Helper class which holds a compiled regular expression
101+ and its pattern."""
102+
103+ def __init__(self, pattern):
104+ self.pattern = pattern
105+ self.regex = re.compile(self.pattern)
106+
107+ def __str__(self):
108+ return self.pattern
109+
110+ def __getattr__(self, name):
111+ return getattr(self.regex, name)
112+
113+
114+def make_regex_tuple(*items):
115+ return tuple((RegExpr(k), v) for k, v in items)
116+
117+def interpretation_for_mimetype(mimetype):
118+ """ get interpretation for a given mimetype, returns :const:`None`
119+ if none of the predefined interpretations matches
120+ """
121+ interpretation = MIMES.get(mimetype, None)
122+ if interpretation is not None:
123+ return interpretation
124+ for pattern, interpretation in MIMES_REGEX:
125+ if pattern.match(mimetype):
126+ return interpretation
127+ return None
128+
129+def manifestation_for_uri(uri):
130+ """ Lookup Manifestation for a given uri based on the scheme part,
131+ returns :const:`None` if no suitable manifestation is found
132+ """
133+ for scheme, manifestation in SCHEMES:
134+ if uri.startswith(scheme):
135+ return manifestation
136+ return None
137+
138+
139+MIMES = {
140+ # x-applix-*
141+ "application/x-applix-word": Interpretation.PAGINATED_TEXT_DOCUMENT,
142+ "application/x-applix-spreadsheet": Interpretation.SPREADSHEET,
143+ "application/x-applix-presents": Interpretation.PRESENTATION,
144+ # x-kword, x-kspread, x-kpresenter, x-killustrator
145+ "application/x-kword": Interpretation.PAGINATED_TEXT_DOCUMENT,
146+ "application/x-kspread": Interpretation.SPREADSHEET,
147+ "application/x-kpresenter": Interpretation.PRESENTATION,
148+ "application/x-killustrator": Interpretation.VECTOR_IMAGE,
149+ # MS
150+ "application/ms-powerpoint": Interpretation.PRESENTATION,
151+ "application/vnd.ms-powerpoint": Interpretation.PRESENTATION,
152+ "application/msword": Interpretation.PAGINATED_TEXT_DOCUMENT,
153+ "application/msexcel": Interpretation.SPREADSHEET,
154+ "application/ms-excel": Interpretation.SPREADSHEET,
155+ "application/vnd.ms-excel": Interpretation.SPREADSHEET,
156+ # pdf, postscript et al
157+ "application/pdf": Interpretation.PAGINATED_TEXT_DOCUMENT,
158+ "application/postscript": Interpretation.PAGINATED_TEXT_DOCUMENT,
159+ "application/ps": Interpretation.PAGINATED_TEXT_DOCUMENT,
160+ "application/rtf": Interpretation.PAGINATED_TEXT_DOCUMENT,
161+
162+ # Gnome office
163+ "application/x-abiword": Interpretation.PAGINATED_TEXT_DOCUMENT,
164+ "application/x-gnucash": Interpretation.SPREADSHEET,
165+ "application/x-gnumeric": Interpretation.SPREADSHEET,
166+
167+ # TeX stuff
168+ "text/x-tex": Interpretation.SOURCE_CODE,
169+ "text/x-latex": Interpretation.SOURCE_CODE,
170+
171+ # Plain text
172+ "text/plain": Interpretation.TEXT_DOCUMENT,
173+
174+ # HTML files on disk are always HTML_DOCUMENTS while online we should
175+ # assume them to be WEBSITEs. By default we anticipate local files...
176+ "text/html": Interpretation.HTML_DOCUMENT,
177+
178+ # Image types
179+ "application/vnd.corel-draw": Interpretation.VECTOR_IMAGE,
180+ "image/jpeg": Interpretation.RASTER_IMAGE,
181+ "image/png": Interpretation.RASTER_IMAGE,
182+ "image/tiff": Interpretation.RASTER_IMAGE,
183+ "image/gif": Interpretation.RASTER_IMAGE,
184+ "image/x-xcf": Interpretation.RASTER_IMAGE,
185+ "image/svg+xml": Interpretation.VECTOR_IMAGE,
186+
187+ # Audio
188+ "application/ogg": Interpretation.AUDIO,
189+ "audio/x-scpls": Interpretation.MEDIA_LIST,
190+
191+ # Development files
192+ "application/ecmascript": Interpretation.SOURCE_CODE,
193+ "application/javascript": Interpretation.SOURCE_CODE,
194+ "application/x-csh": Interpretation.SOURCE_CODE,
195+ "application/x-designer": Interpretation.SOURCE_CODE,
196+ "application/x-desktop": Interpretation.SOURCE_CODE,
197+ "application/x-dia-diagram": Interpretation.SOURCE_CODE,
198+ "application/x-fluid": Interpretation.SOURCE_CODE,
199+ "application/x-glade": Interpretation.SOURCE_CODE,
200+ "application/xhtml+xml": Interpretation.SOURCE_CODE,
201+ "application/x-java-archive": Interpretation.SOURCE_CODE,
202+ "application/x-m4": Interpretation.SOURCE_CODE,
203+ "application/xml": Interpretation.SOURCE_CODE,
204+ "application/x-object": Interpretation.SOURCE_CODE,
205+ "application/x-perl": Interpretation.SOURCE_CODE,
206+ "application/x-php": Interpretation.SOURCE_CODE,
207+ "application/x-ruby": Interpretation.SOURCE_CODE,
208+ "application/x-shellscript": Interpretation.SOURCE_CODE,
209+ "application/x-sql": Interpretation.SOURCE_CODE,
210+ "text/css": Interpretation.SOURCE_CODE,
211+ "text/x-c": Interpretation.SOURCE_CODE,
212+ "text/x-c++": Interpretation.SOURCE_CODE,
213+ "text/x-chdr": Interpretation.SOURCE_CODE,
214+ "text/x-copying": Interpretation.SOURCE_CODE,
215+ "text/x-credits": Interpretation.SOURCE_CODE,
216+ "text/x-csharp": Interpretation.SOURCE_CODE,
217+ "text/x-c++src": Interpretation.SOURCE_CODE,
218+ "text/x-csrc": Interpretation.SOURCE_CODE,
219+ "text/x-dsrc": Interpretation.SOURCE_CODE,
220+ "text/x-eiffel": Interpretation.SOURCE_CODE,
221+ "text/x-gettext-translation": Interpretation.SOURCE_CODE,
222+ "text/x-gettext-translation-template": Interpretation.SOURCE_CODE,
223+ "text/x-haskell": Interpretation.SOURCE_CODE,
224+ "text/x-idl": Interpretation.SOURCE_CODE,
225+ "text/x-java": Interpretation.SOURCE_CODE,
226+ "text/x-lisp": Interpretation.SOURCE_CODE,
227+ "text/x-lua": Interpretation.SOURCE_CODE,
228+ "text/x-makefile": Interpretation.SOURCE_CODE,
229+ "text/x-objcsrc": Interpretation.SOURCE_CODE,
230+ "text/x-ocaml": Interpretation.SOURCE_CODE,
231+ "text/x-pascal": Interpretation.SOURCE_CODE,
232+ "text/x-patch": Interpretation.SOURCE_CODE,
233+ "text/x-python": Interpretation.SOURCE_CODE,
234+ "text/x-sql": Interpretation.SOURCE_CODE,
235+ "text/x-tcl": Interpretation.SOURCE_CODE,
236+ "text/x-troff": Interpretation.SOURCE_CODE,
237+ "text/x-vala": Interpretation.SOURCE_CODE,
238+ "text/x-vhdl": Interpretation.SOURCE_CODE,
239+ "text/x-m4": Interpretation.SOURCE_CODE,
240+}
241+
242+MIMES_REGEX = make_regex_tuple(
243+ # Star Office and OO.org
244+ ("application/vnd.oasis.opendocument.text.*", Interpretation.PAGINATED_TEXT_DOCUMENT),
245+ ("application/vnd.oasis.opendocument.presentation.*", Interpretation.PRESENTATION),
246+ ("application/vnd.oasis.opendocument.spreadsheet.*", Interpretation.SPREADSHEET),
247+ ("application/vnd.oasis.opendocument.graphics.*", Interpretation.VECTOR_IMAGE),
248+ ("application/vnd\\..*", Interpretation.DOCUMENT),
249+ # x-applix-*
250+ ("application/x-applix-.*", Interpretation.DOCUMENT),
251+ # MS
252+ ("application/vnd.ms-excel.*", Interpretation.SPREADSHEET),
253+ ("application/vnd.ms-powerpoint.*", Interpretation.PRESENTATION),
254+ # TeX stuff
255+ (".*/x-dvi", Interpretation.PAGINATED_TEXT_DOCUMENT),
256+ # Image types
257+ ("image/.*", Interpretation.IMAGE),
258+ # Audio
259+ ("audio/.*", Interpretation.AUDIO),
260+ # Video
261+ ("video/.*", Interpretation.VIDEO),
262+)
263+
264+SCHEMES = tuple((
265+ ("file://", Manifestation.FILE_DATA_OBJECT),
266+ ("http://", Manifestation.FILE_DATA_OBJECT.REMOTE_DATA_OBJECT),
267+ ("https://", Manifestation.FILE_DATA_OBJECT.REMOTE_DATA_OBJECT),
268+ ("ssh://", Manifestation.FILE_DATA_OBJECT.REMOTE_DATA_OBJECT),
269+ ("sftp://", Manifestation.FILE_DATA_OBJECT.REMOTE_DATA_OBJECT),
270+))

Subscribers

People subscribed via source and target branches