48 string = options.get(optname, default) |
48 string = options.get(optname, default) |
49 if isinstance(string, bool): |
49 if isinstance(string, bool): |
50 return string |
50 return string |
51 elif isinstance(string, int): |
51 elif isinstance(string, int): |
52 return bool(string) |
52 return bool(string) |
53 elif not isinstance(string, basestring): |
53 elif not isinstance(string, str): |
54 raise OptionError('Invalid type %r for option %s; use ' |
54 raise OptionError('Invalid type %r for option %s; use ' |
55 '1/0, yes/no, true/false, on/off' % ( |
55 '1/0, yes/no, true/false, on/off' % ( |
56 string, optname)) |
56 string, optname)) |
57 elif string.lower() in ('1', 'yes', 'true', 'on'): |
57 elif string.lower() in ('1', 'yes', 'true', 'on'): |
58 return True |
58 return True |
78 string, optname)) |
78 string, optname)) |
79 |
79 |
80 |
80 |
81 def get_list_opt(options, optname, default=None): |
81 def get_list_opt(options, optname, default=None): |
82 val = options.get(optname, default) |
82 val = options.get(optname, default) |
83 if isinstance(val, basestring): |
83 if isinstance(val, str): |
84 return val.split() |
84 return val.split() |
85 elif isinstance(val, (list, tuple)): |
85 elif isinstance(val, (list, tuple)): |
86 return list(val) |
86 return list(val) |
87 else: |
87 else: |
88 raise OptionError('Invalid type %r for option %s; you ' |
88 raise OptionError('Invalid type %r for option %s; you ' |
201 # Python 2/3 compatibility |
201 # Python 2/3 compatibility |
202 |
202 |
203 if sys.version_info < (3,0): |
203 if sys.version_info < (3,0): |
204 b = bytes = str |
204 b = bytes = str |
205 u_prefix = 'u' |
205 u_prefix = 'u' |
206 import StringIO, cStringIO |
206 import io, io |
207 BytesIO = cStringIO.StringIO |
207 BytesIO = io.StringIO |
208 StringIO = StringIO.StringIO |
208 StringIO = io.StringIO |
209 else: |
209 else: |
210 import builtins |
210 import builtins |
211 bytes = builtins.bytes |
211 bytes = builtins.bytes |
212 u_prefix = '' |
212 u_prefix = '' |
213 def b(s): |
213 def b(s): |
214 if isinstance(s, str): |
214 if isinstance(s, str): |
215 return bytes(map(ord, s)) |
215 return bytes(list(map(ord, s))) |
216 elif isinstance(s, bytes): |
216 elif isinstance(s, bytes): |
217 return s |
217 return s |
218 else: |
218 else: |
219 raise TypeError("Invalid argument %r for b()" % (s,)) |
219 raise TypeError("Invalid argument %r for b()" % (s,)) |
220 import io |
220 import io |