Skip to content

Commit

Permalink
Add re type
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordi Bagot committed Oct 8, 2019
1 parent c262002 commit ec94fc7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
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:john@admin.com,Jane:jane@admin.com
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=EnvBase.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
7 changes: 6 additions & 1 deletion environ/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,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 +400,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 @@ -22,6 +22,7 @@ EMPTY_LIST=
INT_VAR=42
STR_LIST_WITH_SPACES= foo, bar
STR_VAR=bar
STR_RE_VAR=bar-1.0.0
MULTILINE_STR_VAR=foo\nbar
INT_LIST=42,33
CYRILLIC_VAR=фуубар
Expand Down

0 comments on commit ec94fc7

Please sign in to comment.