Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add re type #234

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ The format is *inspired* by `Keep a Changelog <http://keepachangelog.com/en/1.0.
and this project adheres to `Semantic Versioning <http://semver.org/spec/v2.0.0.html>`_.


`unreleased`_
--------------------------
Added
+++++
- New type: re


`v0.4.5`_ - 25-June-2018
--------------------------
Added
Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Supported types
- dict (BAR=key=val,foo=bar) #environ.Env(BAR=(dict, {}))
- dict (BAR=key=val;foo=1.1;baz=True) #environ.Env(BAR=(dict(value=unicode, cast=dict(foo=float,baz=bool)), {}))
- url
- re
- path (environ.Path)
- db_url
- PostgreSQL: postgres://, pgsql://, psql:// or postgresql://
Expand Down Expand Up @@ -207,7 +208,7 @@ Some settings such as Django's ``ADMINS`` make use of nested lists. You can use
.. code-block:: python

# DJANGO_ADMINS=John:[email protected],Jane:[email protected]
ADMINS = [x.split(':') for x in env.list('DJANGO_ADMINS')]
ADMINS = [x.split(':') for x in env.list('DJANGO_ADMINS')]

# or use more specific function

Expand Down
16 changes: 15 additions & 1 deletion environ/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ def path(self, var, default=NOTSET, **kwargs):
"""
return Path(self.get_value(var, default=default), **kwargs)

def re(self, var, regex, default=NOTSET):
"""Returns the string that satisfies the given regex.
If it's not satisfied returns the var as string

:param var: Name of variable.
:param regex: Regular expression to apply to var.
:param default: If var not present in environ, return this instead.

:returns: str
"""
str_var = self.str(var, default)
re_obj = re.search(regex, str_var)
return re_obj.group(0) if re_obj else str_var

def get_value(self, var, cast=None, default=NOTSET, parse_default=False):
"""Return value for given environment variable.

Expand Down Expand Up @@ -768,7 +782,7 @@ def __unicode__(self):

def __getitem__(self, *args, **kwargs):
return self.__str__().__getitem__(*args, **kwargs)

def __fspath__(self):
return self.__str__()

Expand Down
8 changes: 7 additions & 1 deletion environ/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class BaseTests(unittest.TestCase):
def generateData(cls):
return dict(STR_VAR='bar',
MULTILINE_STR_VAR='foo\\nbar',
STR_RE_VAR='bar-1.0.0',
INT_VAR='42',
FLOAT_VAR='33.3',
FLOAT_COMMA_VAR='33,3',
Expand Down Expand Up @@ -97,6 +98,11 @@ def test_str(self):
self.assertTypeAndValue(str, 'foo\\nbar', self.env.str('MULTILINE_STR_VAR'))
self.assertTypeAndValue(str, 'foo\nbar', self.env.str('MULTILINE_STR_VAR', multiline=True))

def test_re(self):
self.assertTypeAndValue(str, '1.0.0', self.env.re('STR_RE_VAR', r'\d+.\d+.\d+'))
self.assertTypeAndValue(str, 'foo', self.env.re('MULTILINE_STR_VAR', r'\w+'))
self.assertTypeAndValue(str, 'bar', self.env.re('STR_VAR', r'\d+'))

def test_bytes(self):
self.assertTypeAndValue(bytes, b'bar', self.env.bytes('STR_VAR'))

Expand Down Expand Up @@ -395,7 +401,7 @@ def test_memory_sqlite_url(self):

self.assertEqual(url['ENGINE'], 'django.db.backends.sqlite3')
self.assertEqual(url['NAME'], ':memory:')

def test_memory_sqlite_url_warns_about_netloc(self):
url = 'sqlite://missing-slash-path'
with warnings.catch_warnings(record=True) as w:
Expand Down
1 change: 1 addition & 0 deletions environ/test_env.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ INT_VAR=42
STR_LIST_WITH_SPACES= foo, bar
STR_VAR=bar
MULTILINE_STR_VAR=foo\nbar
STR_RE_VAR=bar-1.0.0
INT_LIST=42,33
CYRILLIC_VAR=фуубар
INT_TUPLE=(42,33)
Expand Down