Mercurial > hg > martINI
comparison martini/config.py @ 17:a42a02bb46ed
py3
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 20 Feb 2017 15:11:21 -0800 |
parents | 8ae3a7fd466a |
children | 81e0743dbe53 |
comparison
equal
deleted
inserted
replaced
16:8ae3a7fd466a | 17:a42a02bb46ed |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import os | 3 import os |
4 import sys | 4 import sys |
5 | |
6 | 5 |
7 | 6 |
8 try: | 7 try: |
9 from collections import OrderedDict | 8 from collections import OrderedDict |
10 except ImportError: | 9 except ImportError: |
27 from configparser import MissingSectionHeaderError | 26 from configparser import MissingSectionHeaderError |
28 from configparser import NoOptionError | 27 from configparser import NoOptionError |
29 from io import StringIO | 28 from io import StringIO |
30 | 29 |
31 | 30 |
31 try: | |
32 # python 2 | |
33 string = (str, unicode) | |
34 except: | |
35 # python 3 | |
36 string = (str,) | |
37 | |
38 | |
32 def file_pointer(resource): | 39 def file_pointer(resource): |
33 """returns a file-like object given a string""" | 40 """returns a file-like object given a string""" |
34 # XXX could go in utils.py | 41 # XXX could go in utils.py |
35 | 42 |
36 if not isinstance(resource, basestring): | 43 if not isinstance(resource, string): |
37 # assume resource is already a file-like object | 44 # assume resource is already a file-like object |
38 return resource | 45 return resource |
39 | 46 |
40 if os.path.exists(resource): | 47 if os.path.exists(resource): |
41 return file(resource) | 48 return open(resource) |
42 if sum([resource.startswith(http) | 49 if sum([resource.startswith(http) |
43 for http in ('http://', 'https://')]): | 50 for http in ('http://', 'https://')]): |
44 return urlopen(resource) | 51 return urlopen(resource) |
45 return StringIO(resource) | 52 return StringIO(resource) |
46 | 53 |