Package elisa :: Package core :: Package utils :: Module misc
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.utils.misc

  1  # -*- coding: utf-8 -*- 
  2  # Elisa - Home multimedia server 
  3  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  4  # All rights reserved. 
  5  # 
  6  # This file is available under one of two license agreements. 
  7  # 
  8  # This file is licensed under the GPL version 3. 
  9  # See "LICENSE.GPL" in the root of this distribution including a special 
 10  # exception to use Elisa with Fluendo's plugins. 
 11  # 
 12  # The GPL part of Elisa is also available under a commercial licensing 
 13  # agreement from Fluendo. 
 14  # See "LICENSE.Elisa" in the root directory of this distribution package 
 15  # for details on that license. 
 16   
 17  """ 
 18  Miscellaneaous utilities that don't need their own module because they are 
 19  reasonnably small. 
 20  """ 
 21   
 22  __maintainer__ = 'Philippe Normand <philippe@fluendo.com>' 
 23   
 24  import mimetypes 
 25  import sys, platform 
 26  import os, re 
 27   
28 -def env_var_expand(var_name):
29 """ 30 Expand the given environment variable content. If it contains 31 other references to environment variables, they are expanded too. 32 33 Supported platforms are win32 and linux. 34 35 Example of use:: 36 37 >>> env_var_expand('$HOME') 38 >>> '/home/phil' 39 40 @raises ValueError: if current system's platform is not windows or linux 41 @param var_name: environment variable 42 @type var_name: string 43 @rtype: string 44 """ 45 platform_type = platform.system().lower() 46 if platform_type == 'windows': 47 re_env = re.compile(r'%(\w+)%') 48 def expander(mo): 49 return os.environ.get(mo.group()[1:-1], 'UNKNOWN')
50 51 elif platform_type == 'linux': 52 re_env = re.compile(r'\$(\w+)') 53 54 def expander(mo): 55 xpanded = os.environ.get(mo.group()[1:], 'UNKNOWN') 56 if xpanded.find('$') > -1: 57 xpanded = re_env.sub(expander, xpanded) 58 return xpanded 59 60 else: 61 raise ValueError("Unsupported platform") 62 63 expanded = re_env.sub(expander, var_name) 64 return expanded 65
66 -def env_var_explode_list(var_name, default=''):
67 """ 68 Explode a list of values stored in an environment variable as a 69 single string. On win32 the item separator is ';' and on other 70 platforms it is ':'. 71 72 Example of use:: 73 74 >>> env_var_explode_list('$PATH') 75 >>> ['/usr/bin','/bin'] 76 77 @param var_name: environment variable 78 @type var_name: string 79 @keyword default: value to use if environment variable not found 80 @type default: string 81 @rtype: list of strings 82 """ 83 value = os.environ.get(var_name, default) 84 platform_type = platform.system().lower() 85 if platform_type == 'windows': 86 separator = ';' 87 else: 88 separator = ':' 89 exploded = value.split(separator) 90 if exploded == ['']: 91 exploded = [] 92 return exploded
93 94
95 -def un_camelify(camel_string):
96 """ 97 Convert CamelCase styled strings to lower_cased style. 98 99 @param camel_string: CamelStyled string to convert 100 @type camel_string: string 101 @rtype: string 102 """ 103 if len(camel_string) < 2: 104 un_cameled = camel_string.lower() 105 else: 106 camel_string = camel_string.replace(' ', '') 107 un_cameled = camel_string[0].lower() 108 for letter in camel_string[1:]: 109 if letter.isupper(): 110 un_cameled += '_%s' % letter.lower() 111 else: 112 un_cameled += letter 113 return un_cameled
114
115 -def get_media_infos_from_mime(uri):
116 """ 117 Retrieve mime-type and media type of the given URI, based on its 118 extension. 119 120 @param uri: the media resource address to analyze 121 @type uri: L{elisa.core.media_uri.MediaUri} 122 @rtype: 2-tuple: (mime_type: string, file_type: string) 123 """ 124 # TODO: remove this function when mime_getter works 125 mime_type = mimetypes.guess_type(uri.path) 126 if mime_type[0]: 127 parts = mime_type[0].split('/') 128 mime_type = mime_type[0] 129 # Special cases handling 130 if parts[0] == 'application': 131 ext = uri.extension 132 custom_extensions = {'ogg': 'audio', 133 } 134 file_type = custom_extensions.get(ext, '') 135 else: 136 file_type = parts[0] 137 138 blacklist = ['x-mpegurl',] 139 if parts[1] in blacklist: 140 mime_type = '' 141 file_type = '' 142 else: 143 # in case mimetypes did not return anything, filter on extension 144 custom_extensions = {'flv': 'video', 145 'ogm': 'video', 146 'mkv': 'video', 147 'mod': 'video', 148 'nef': 'image' 149 } 150 file_type = custom_extensions.get(uri.extension, '') 151 if mime_type == (None, None): 152 mime_type = '' 153 return mime_type, file_type
154