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