grim/envconfig2

Add url methods

2017-08-29, Gary Kramlich
0bd92d883490
Parents 9adbef1d099b
Children 7d652f5b5a92
Add url methods
--- a/envconfig2.py Tue Aug 29 14:44:17 2017 -0500
+++ b/envconfig2.py Tue Aug 29 14:44:25 2017 -0500
@@ -19,6 +19,7 @@
import json as realjson
import os
+import urlparse
__version__ = '0.2'
@@ -90,3 +91,14 @@
return realjson.loads(os.environ[var])
+
+def url(name, default=None, required=False):
+ var = find_variable(name, required)
+ if var is None:
+ if default is not None:
+ return urlparse.urlparse(default)
+
+ return default
+
+ return urlparse.urlparse(os.environ[var])
+
--- a/tests/test_envconfig2.py Tue Aug 29 14:44:17 2017 -0500
+++ b/tests/test_envconfig2.py Tue Aug 29 14:44:25 2017 -0500
@@ -18,6 +18,7 @@
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os
+import urlparse
import pytest
@@ -185,6 +186,35 @@
assert envconfig2.json('JSON') == {'foo': 'bar'}
+def test_url_required_not_set():
+ os.environ = {}
+
+ with pytest.raises(ValueError):
+ envconfig2.url('URL', required=True)
+
+
+def test_url_required_set():
+ os.environ = {
+ 'URL': 'https://example.com',
+ }
+
+ assert envconfig2.url('URL', required=True) == urlparse.urlparse('https://example.com')
+
+
+def test_url_default_not_set():
+ os.environ = {}
+
+ assert envconfig2.url('URL', default='https://example.com') == urlparse.urlparse('https://example.com')
+
+
+def test_url():
+ os.environ = {
+ 'URL': 'https://example.com',
+ }
+
+ assert envconfig2.url('URL') == urlparse.urlparse('https://example.com')
+
+
def test_prefix():
try:
os.environ = {