Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
refactor tailor class
Browse files Browse the repository at this point in the history
Refactored the `Tailor` class by removing the local store
and utilizing native dict functionality.
  • Loading branch information
bradleygolden authored Oct 4, 2018
1 parent 2c58060 commit 02c1bc0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
2 changes: 1 addition & 1 deletion pytailor/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
# |__| \/ \/


VERSION = (0, 1, 3)
VERSION = (0, 1, 4)

__version__ = ".".join(map(str, VERSION))
32 changes: 5 additions & 27 deletions pytailor/tailor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
class Tailor(dict):
def __init__(self):
self.env_store = dict()
self.store = dict()

def __getitem__(self, key):
rv = None
Expand All @@ -20,38 +19,17 @@ def __getitem__(self, key):
except KeyError:
# system environment variable no longer exists
# does a saved config value exist?
if key in self.store:
rv = self.store[key]
if key in self.keys():
rv = super().__getitem__(key)
else:
# seems the user deleted their environment variable and
# don't have it configured in a file
rv = self.env_store[key]
else:
rv = self.store[key]
rv = super().__getitem__(key)

return rv

def __setitem__(self, key, val):
self.store.__setitem__(key, val)

def __eq__(self, other):
if not isinstance(other, dict):
return False
return (
dict.__eq__(self, other)
and self.env_store == other.env_store
and self.store == other.store
)

def __ne__(self, other):
return not self.__eq__(other)

def __contains__(self, item):
return item in self.store

def __str__(self):
return str(self.store)

def from_object(self, obj: object):
"""Read configuration from an object."""
_store = {
Expand All @@ -60,13 +38,13 @@ def from_object(self, obj: object):
if not key.startswith("__")
}
for name, value in _store.items():
self.store[name] = value
self[name] = value

def from_dotenv(self, path: str):
"""Load configuration from specified .env path."""
_store = dotenv_to_dict(path)
for name, value in _store.items():
self.store[name] = value
self[name] = value

def watch_env_var(self, name: str):
"""Set configuration and watch a system wide environment variable."""
Expand Down

0 comments on commit 02c1bc0

Please sign in to comment.