diff --git a/CHANGELOG.rst b/CHANGELOG.rst index fe154dbf..5fd8e3db 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -6,6 +6,13 @@ The format is *inspired* by `Keep a Changelog `_. +`unreleased`_ +-------------------------- +Added ++++++ + - New type: re + + `v0.4.5`_ - 25-June-2018 -------------------------- Added diff --git a/README.rst b/README.rst index 871a8225..55e8d2ba 100644 --- a/README.rst +++ b/README.rst @@ -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:// @@ -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 diff --git a/environ/environ.py b/environ/environ.py index ea4978c9..2ae6ddfa 100644 --- a/environ/environ.py +++ b/environ/environ.py @@ -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. @@ -768,7 +782,7 @@ def __unicode__(self): def __getitem__(self, *args, **kwargs): return self.__str__().__getitem__(*args, **kwargs) - + def __fspath__(self): return self.__str__() diff --git a/environ/test.py b/environ/test.py index 2325816f..bf3efc17 100644 --- a/environ/test.py +++ b/environ/test.py @@ -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')) @@ -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: diff --git a/environ/test_env.txt b/environ/test_env.txt index f00ce490..c0d0e8ea 100644 --- a/environ/test_env.txt +++ b/environ/test_env.txt @@ -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=фуубар