Repository: kpn/settings-vial

Issue:
Add as_dict method to make it compatible with settings-vial API
The `class Settings` does not have the method `as_dict` which makes it incompatible with [SimpleSettings](https://github.com/kpn-digital/py-pkgsettings/blob/master/pkgsettings/pkgsettings.py#L9) object. 

If `pkgsettings` is used and the `ENV` variables are loaded using `settings-vial`, calling `settings.as_dict()` will raise `AttributeError`, breaking at line [31](https://github.com/kpn-digital/py-pkgsettings/blob/master/pkgsettings/pkgsettings.py#L31)

```python
settings = Settings() # pkgsettings.Settings instance
env_settings = settings_vial.Settings(env_prefix="MY_PREFIX_")
env_settings.load_env()
settings.configure(env_settings)

settings.as_dict()

File "/Users/marcos/Projects/test-with-streaming/venv/lib/python3.9/site-packages/settings_vial/settings_vial.py", line 77, in __getattr__
    raise AttributeError("{} has no attribute {}".format(self, attr))
AttributeError: <Settings {'env_prefix': 'MY_PREFIX_', 'override_prefix': None, 'override_keys_function': None, 'override_keys_reverse_lookup': False, '_config': {'ENVFILES_SECRETS': '/Users/marcos/Projects/test-with-streaming/params.env', 'ENVIRONMENT': 'localdev'}, '_override_config': {}}> has no attribute as_dict
```

Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/kpn/settings-vial/pull/12?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn) Report
> Merging [#12](https://codecov.io/gh/kpn/settings-vial/pull/12?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn) (6bc8102) into [master](https://codecov.io/gh/kpn/settings-vial/commit/541429020f1c1c89ceef2c99777104faa8f286ad?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn) (5414290) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/kpn/settings-vial/pull/12/graphs/tree.svg?width=650&height=150&src=pr&token=I4lKzeDNvD&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn)](https://codecov.io/gh/kpn/settings-vial/pull/12?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn)

```diff
@@            Coverage Diff            @@
##            master       #12   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            3         3           
  Lines           81        83    +2     
  Branches        12        12           
=========================================
+ Hits            81        83    +2     
```


| [Impacted Files](https://codecov.io/gh/kpn/settings-vial/pull/12?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn) | Coverage Δ | |
|---|---|---|
| [settings\_vial/settings\_vial.py](https://codecov.io/gh/kpn/settings-vial/pull/12/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn#diff-c2V0dGluZ3NfdmlhbC9zZXR0aW5nc192aWFsLnB5) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kpn/settings-vial/pull/12?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kpn/settings-vial/pull/12?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn). Last update [5414290...6bc8102](https://codecov.io/gh/kpn/settings-vial/pull/12?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=kpn).


Patch:
diff --git a/settings_vial/settings_vial.py b/settings_vial/settings_vial.py
index cfa5315..0263871 100755
--- a/settings_vial/settings_vial.py
+++ b/settings_vial/settings_vial.py
@@ -1,3 +1,4 @@
+import copy
 import json
 import warnings
 from os import environ
@@ -90,7 +91,7 @@ class Settings:
         return override_keys
 
     def load_env(self):
-        r""" Loads configuration from environment variables as json encoded.
+        r"""Loads configuration from environment variables as json encoded.
 
         Will load all environment variables containing **env_prefix** as a prefix and strip
         the prefix so you can access it through attributes normally: ``settings.VAR``.
@@ -103,7 +104,7 @@ class Settings:
         self._load_dict(environ)
 
     def load_dotenv_file(self, path):
-        """ Loads configuration from .env files as json encoded values.
+        """Loads configuration from .env files as json encoded values.
 
         Works the same way as ``load_env``, but reads values from .env file instead of environment variables.
         """
@@ -127,7 +128,7 @@ class Settings:
             self._load_overrides()
 
     def _load_overrides(self):
-        r""" Traverses through the already loaded prefixed environment variables and
+        r"""Traverses through the already loaded prefixed environment variables and
         extracts the overridable ones.
 
         If any of the environment variables loaded contains **override_prefix** the prefix will be stripped
@@ -149,3 +150,6 @@ class Settings:
 
         for var in vars_to_delete:
             del self._config[var]
+
+    def as_dict(self):
+        return copy.deepcopy(self._config)


Test patch:
diff --git a/tests/test_dotenv.py b/tests/test_dotenv.py
index 0faab7a..1f2d2c1 100644
--- a/tests/test_dotenv.py
+++ b/tests/test_dotenv.py
@@ -61,3 +61,11 @@ class TestEnvfileSettings:
         settings.load_dotenv_file(mock_envfile)
 
         assert settings.SOME_KEY_PREFIX_REPEATED == "nested"
+
+    def test_as_dict(self, mock_envfile):
+        settings = Settings(env_prefix="PREFIX_")
+        settings.load_dotenv_file(mock_envfile)
+
+        as_dict = settings.as_dict()
+        assert as_dict == settings._config
+        assert isinstance(as_dict, dict)