Skip to content

Commit

Permalink
Add Check class.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed May 29, 2023
1 parent 7394d83 commit b099cc2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
v1.0.0
======

Added ``Check`` class.
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@
.. image:: https://img.shields.io/badge/skeleton-2023-informational
:target: https://blog.jaraco.com/skeleton


This library facilitates handling of environment variables.


Check
=====

Use ``Check`` to act as a boolean gate on an environment variable.
34 changes: 34 additions & 0 deletions jaraco/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os


class Check:
"""
Check if an environment variable meets a certain expectation.
>>> setenv = getfixture('monkeypatch').setenv
>>> check = Check('SOME_VAR', expect='setting1')
>>> bool(check)
False
>>> setenv('SOME_VAR', 'setting1')
>>> bool(check)
True
>>> setenv('SOME_VAR', 'setting2')
>>> bool(check)
False
>>> check = Check('OTHER_VAR', expect='setting1', default='setting1')
>>> bool(check)
True
>>> setenv('OTHER_VAR', 'setting2')
>>> bool(check)
False
"""

def __init__(self, key, expect, *, default=None, _lookup=os.environ):
self.key = key
self.expect = expect
self.default = default
self.lookup = _lookup

def __bool__(self):
return self.lookup.get(self.key, self.default) == self.expect

0 comments on commit b099cc2

Please sign in to comment.