1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
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
154