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$"
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 'video/webm': 'webm',
40 }
41
42
44 """Converts a mime type to a file extension.
45 @param mimeType: the mime type
46 @returns: file extenion if found or data otherwise
47 """
48 return _EXTENSIONS.get(mimeType, 'data')
49
50
52 """Launches an application in the background for
53 displaying a url which is of a specific mimeType
54 @param url: the url to display
55 @param mimeType: the mime type of the content
56 """
57 try:
58 import gnomevfs
59 except ImportError:
60 gnomevfs = None
61
62 try:
63 from win32com.shell import shell as win32shell
64 except ImportError:
65 win32shell = None
66
67 try:
68 import gio
69 except ImportError:
70 gio = None
71
72 if gio:
73 app = gio.app_info_get_default_for_type(mimeType, True)
74 if not app:
75 return
76 args = '%s %s' % (app.get_executable(), url)
77 executable = None
78 shell = True
79 elif gnomevfs:
80 app = gnomevfs.mime_get_default_application(mimeType)
81 if not app:
82 return
83 args = '%s %s' % (app[2], url)
84 executable = None
85 shell = True
86 elif win32shell:
87 assoc = win32shell.AssocCreate()
88 ext = _EXTENSIONS.get(mimeType)
89 if ext is None:
90 return
91 assoc.Init(0, '.' + ext)
92 args = assoc.GetString(0, _ASSOCSTR_COMMAND)
93 executable = assoc.GetString(0, _ASSOCSTR_EXECUTABLE)
94 args = args.replace("%1", url)
95 args = args.replace("%L", url)
96 shell = False
97 else:
98 return
99
100 import subprocess
101 subprocess.Popen(args, executable=executable,
102 shell=shell)
103