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