Mercurial > hg > martINI
comparison martini/config.py @ 16:8ae3a7fd466a
py3
| author | Jeff Hammel <k0scist@gmail.com> |
|---|---|
| date | Mon, 20 Feb 2017 14:56:46 -0800 |
| parents | 5de4f7d434de |
| children | a42a02bb46ed |
comparison
equal
deleted
inserted
replaced
| 15:5de4f7d434de | 16:8ae3a7fd466a |
|---|---|
| 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 import urllib2 | |
| 6 | 5 |
| 7 from ConfigParser import ConfigParser | 6 |
| 8 from ConfigParser import InterpolationMissingOptionError | |
| 9 from ConfigParser import MissingSectionHeaderError | |
| 10 from ConfigParser import NoOptionError | |
| 11 from StringIO import StringIO | |
| 12 | 7 |
| 13 try: | 8 try: |
| 14 from collections import OrderedDict | 9 from collections import OrderedDict |
| 15 except ImportError: | 10 except ImportError: |
| 16 from odict import OrderedDict | 11 from odict import OrderedDict |
| 12 | |
| 13 try: | |
| 14 # python 2 | |
| 15 from urllib2 import urlopen | |
| 16 from ConfigParser import ConfigParser | |
| 17 from ConfigParser import InterpolationMissingOptionError | |
| 18 from ConfigParser import MissingSectionHeaderError | |
| 19 from ConfigParser import NoOptionError | |
| 20 from StringIO import StringIO | |
| 21 | |
| 22 except ImportError: | |
| 23 # python 3 | |
| 24 from urllib.request import urlopen | |
| 25 from configparser import ConfigParser | |
| 26 from configparser import InterpolationMissingOptionError | |
| 27 from configparser import MissingSectionHeaderError | |
| 28 from configparser import NoOptionError | |
| 29 from io import StringIO | |
| 17 | 30 |
| 18 | 31 |
| 19 def file_pointer(resource): | 32 def file_pointer(resource): |
| 20 """returns a file-like object given a string""" | 33 """returns a file-like object given a string""" |
| 21 # XXX could go in utils.py | 34 # XXX could go in utils.py |
| 26 | 39 |
| 27 if os.path.exists(resource): | 40 if os.path.exists(resource): |
| 28 return file(resource) | 41 return file(resource) |
| 29 if sum([resource.startswith(http) | 42 if sum([resource.startswith(http) |
| 30 for http in ('http://', 'https://')]): | 43 for http in ('http://', 'https://')]): |
| 31 return urllib2.urlopen(resource) | 44 return urlopen(resource) |
| 32 return StringIO(resource) | 45 return StringIO(resource) |
| 33 | 46 |
| 34 | 47 |
| 35 class ConfigMunger(ConfigParser): | 48 class ConfigMunger(ConfigParser): |
| 36 """combine configuration from .ini files""" | 49 """combine configuration from .ini files""" |
