Repository: dlr-eoc/ukis-pysat

Issue:
env_get with boolean
**Is your feature request related to a problem? Please describe.**
`env_get` does not do very much at the moment, it would be quite useful if it could return booleans.

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
We could also use another package which does that, but I think it's very easy to implement.


Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91?src=pr&el=h1) Report
> Merging [#91](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91?src=pr&el=desc) into [master](https://codecov.io/gh/dlr-eoc/ukis-pysat/commit/1c374ac101aa0922e0637854462083b4e501cea6?el=desc) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91/graphs/tree.svg?width=650&height=150&src=pr&token=6LYPGRGMXV)](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master      #91   +/-   ##
=======================================
  Coverage   93.51%   93.52%           
=======================================
  Files           8        8           
  Lines         802      803    +1     
=======================================
+ Hits          750      751    +1     
  Misses         52       52           
```

| Flag | Coverage Δ | |
|---|---|---|
| #unittests | `93.52% <100.00%> (+<0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tests/test\_file.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9maWxlLnB5) | `98.21% <100.00%> (+0.13%)` | :arrow_up: |
| [tests/test\_raster.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9yYXN0ZXIucHk=) | `99.38% <100.00%> (-0.02%)` | :arrow_down: |
| [ukis\_pysat/file.py](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91/diff?src=pr&el=tree#diff-dWtpc19weXNhdC9maWxlLnB5) | `92.98% <100.00%> (+0.12%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91?src=pr&el=footer). Last update [1c374ac...391de15](https://codecov.io/gh/dlr-eoc/ukis-pysat/pull/91?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index e14adb0..101a8b4 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -18,6 +18,7 @@ something was changed
 
 Added
 *****
+- ``file``: read and return booleans from env #90
 - ``raster``: possibility to init with 2D array #88
 
 [0.6.3]  (2020-09-02)
diff --git a/ukis_pysat/file.py b/ukis_pysat/file.py
index 8b90acc..09354e3 100644
--- a/ukis_pysat/file.py
+++ b/ukis_pysat/file.py
@@ -11,9 +11,16 @@ from re import compile
 from typing import List, Union, Dict, Iterator, Tuple, Any, Pattern
 
 
-def env_get(key: str) -> str:
-    """get an environment variable or fail with a meaningful error message"""
+def env_get(key: str, boolean: bool = False) -> Union[str, bool]:
+    """get an environment variable or fail with a meaningful error message
+
+    :param key: name of environment variable
+    :param boolean: bool (default: False), optional. Returns true if key in ["true", "y", "yes", "1"].
+    :return: str or boolean
+    """
     try:
+        if boolean:
+            return os.environ[key].lower() in ["true", "y", "yes", "1"]
         return os.environ[key]
     except KeyError:
         raise KeyError(f"No environment variable {key} found")


Test patch:
diff --git a/tests/test_file.py b/tests/test_file.py
index 2db0674..0f5598d 100644
--- a/tests/test_file.py
+++ b/tests/test_file.py
@@ -14,6 +14,11 @@ class FileTest(unittest.TestCase):
         with self.assertRaises(KeyError, msg=f"No environment variable Key found"):
             psf.env_get("Key")
 
+        os.environ["FUN"] = "True"
+        self.assertTrue(psf.env_get("FUN", boolean=True))
+        os.environ["ISITWEEKENDYET"] = "0"
+        self.assertFalse(psf.env_get("ISITWEEKENDYET", boolean=True))
+
     def test_get_sentinel_scene_from_dir(self):
         with psf.get_sentinel_scene_from_dir(path_testfiles) as (full_path, ident):
             self.assertEqual("S1M_hello_from_inside", ident)
diff --git a/tests/test_raster.py b/tests/test_raster.py
index edf8d1e..c29989b 100644
--- a/tests/test_raster.py
+++ b/tests/test_raster.py
@@ -274,34 +274,31 @@ class DataTest(unittest.TestCase):
 
     def test_write_to_file(self):
         self.img.write_to_file(r"result.tif", np.uint16)
-        img2 = Image("result.tif")
-        self.assertTrue(np.array_equal(img2.arr, self.img.arr))
+        with Image("result.tif") as img2:
+            self.assertTrue(np.array_equal(img2.arr, self.img.arr))
 
-        img2.close()
         os.remove(r"result.tif")
 
         self.img.write_to_file(r"result.tif", "min", compress="lzw")
-        img2 = Image("result.tif")
-        self.assertEqual(img2.arr.dtype, "uint8")
-        self.assertEqual(img2.dataset.profile["compress"], "lzw")
+        with Image("result.tif") as img2:
+            self.assertEqual(img2.arr.dtype, "uint8")
+            self.assertEqual(img2.dataset.profile["compress"], "lzw")
 
-        img2.close()
         os.remove(r"result.tif")
 
         self.img.write_to_file(r"result.tif", np.uint8, compress="packbits", kwargs={"tiled": True})
-        img2 = Image("result.tif")
-        self.assertEqual(img2.arr.dtype, "uint8")
-        self.assertEqual(img2.dataset.profile["tiled"], True)
+        with Image("result.tif") as img2:
+            self.assertEqual(img2.arr.dtype, "uint8")
+            self.assertEqual(img2.dataset.profile["tiled"], True)
 
-        img2.close()
         os.remove(r"result.tif")
 
-        img3 = Image(self.img.arr, crs=self.img.dataset.crs, transform=self.img.dataset.transform)
-        img3.write_to_file(r"result.tif", np.uint16)
-        img3.close()
-        img4 = Image("result.tif")
-        self.assertTrue(np.array_equal(img4.arr, self.img.arr))
-        img4.close()
+        with Image(self.img.arr, crs=self.img.dataset.crs, transform=self.img.dataset.transform) as img3:
+            img3.write_to_file(r"result.tif", np.uint16)
+
+        with Image("result.tif") as img4:
+            self.assertTrue(np.array_equal(img4.arr, self.img.arr))
+
         os.remove(r"result.tif")