Package flumotion :: Package common :: Module mimetypes
[hide private]

Source Code for Module flumotion.common.mimetypes

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3   
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L. 
  6  # Copyright (C) 2010,2011 Flumotion Services, S.A. 
  7  # All rights reserved. 
  8  # 
  9  # This file may be distributed and/or modified under the terms of 
 10  # the GNU Lesser General Public License version 2.1 as published by 
 11  # the Free Software Foundation. 
 12  # This file is distributed without any warranty; without even the implied 
 13  # warranty of merchantability or fitness for a particular purpose. 
 14  # See "LICENSE.LGPL" in the source distribution for more information. 
 15  # 
 16  # Headers in this file shall remain intact. 
 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   
39 -def mimeTypeToExtention(mimeType):
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
47 -def launchApplicationByUrl(url, mimeType):
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