1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """convert mimetypes or launch an application based on one"""
19
20 __version__ = "$Rev$"
21 _ASSOCSTR_COMMAND = 1
22 _ASSOCSTR_EXECUTABLE = 2
23 _EXTENSIONS = {
24 'application/ogg': 'ogg',
25 'audio/mpeg': 'mp3',
26 'audio/x-flac': 'flac',
27 'audio/x-wav': 'wav',
28 'multipart/x-mixed-replace': 'multipart',
29 'video/mpegts': 'ts',
30 'video/x-dv': 'dv',
31 'video/x-flv': 'flv',
32 'video/x-matroska': 'mkv',
33 'video/x-ms-asf': 'asf',
34 'video/x-msvideo': 'avi',
35 'video/webm': 'webm',
36 }
37
38
40 """Converts a mime type to a file extension.
41 @param mimeType: the mime type
42 @returns: file extenion if found or data otherwise
43 """
44 return _EXTENSIONS.get(mimeType, 'data')
45
46
48 """Launches an application in the background for
49 displaying a url which is of a specific mimeType
50 @param url: the url to display
51 @param mimeType: the mime type of the content
52 """
53 try:
54 import gnomevfs
55 except ImportError:
56 gnomevfs = None
57
58 try:
59 from win32com.shell import shell as win32shell
60 except ImportError:
61 win32shell = None
62
63 try:
64 import gio
65 except ImportError:
66 gio = None
67
68 if gio:
69 app = gio.app_info_get_default_for_type(mimeType, True)
70 if not app:
71 return
72 args = '%s %s' % (app.get_executable(), url)
73 executable = None
74 shell = True
75 elif gnomevfs:
76 app = gnomevfs.mime_get_default_application(mimeType)
77 if not app:
78 return
79 args = '%s %s' % (app[2], url)
80 executable = None
81 shell = True
82 elif win32shell:
83 assoc = win32shell.AssocCreate()
84 ext = _EXTENSIONS.get(mimeType)
85 if ext is None:
86 return
87 assoc.Init(0, '.' + ext)
88 args = assoc.GetString(0, _ASSOCSTR_COMMAND)
89 executable = assoc.GetString(0, _ASSOCSTR_EXECUTABLE)
90 args = args.replace("%1", url)
91 args = args.replace("%L", url)
92 shell = False
93 else:
94 return
95
96 import subprocess
97 subprocess.Popen(args, executable=executable,
98 shell=shell)
99