Skip to content

Commit 81d44d0

Browse files
authored
Merge pull request #121 from GreenScheduler/aturner_pr
PR 110 redux
2 parents 99737e8 + 68e45ee commit 81d44d0

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

cats/configure.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import logging
1414
import sys
15+
import os
1516
from collections.abc import Mapping
1617
from typing import Any, Optional
1718

@@ -76,7 +77,16 @@ def config_from_file(configpath="") -> Mapping[str, Any]:
7677
return yaml.safe_load(f)
7778
logging.info(f"Using provided config file: {configpath}\n")
7879
else:
79-
# if no path provided, look for `config.yml` in current directory
80+
# if no path provided, try to use config environment variable
81+
cfile = os.getenv("CATS_CONFIG_FILE")
82+
if cfile is not None:
83+
try:
84+
with open(cfile, "r") as f:
85+
return yaml.safe_load(f)
86+
logging.info("Using config.yml found in CATS_CONFIG_FILE\n")
87+
except FileNotFoundError:
88+
logging.warning("CATS_CONFIG_FILE config file not found")
89+
# if no path provided and no env variable, look for `config.yml` in current directory
8090
try:
8191
with open("config.yml", "r") as f:
8292
return yaml.safe_load(f)

docs/source/quickstart.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ Use the ``--config`` option to specify a path to the configuration
4545
file, relative to the current directory.
4646

4747
In case of a missing location command line argument, ``cats`` looks
48-
for a file named ``config.yaml`` or ``config.yml`` in the current directory.
48+
first in the ``CATS_CONFIG_FILE`` environment variable and if that
49+
is not set it looks for a file named ``config.yml``
50+
in the current directory.
4951

5052
.. code-block:: shell
5153
@@ -67,7 +69,16 @@ machine IP address:
6769
Carbon intensity if job started now = 117.95 gCO2eq/kWh
6870
Carbon intensity at optimal time = 60.93 gCO2eq/kWh
6971
70-
Use --format=json to get this in machine readable format
72+
Use --format=json to get this in machine readable format
73+
74+
.. code-block::console
75+
76+
# location information is provided by the file
77+
# specified in $CATS_CONFIG_FILE
78+
# If not, it looks for ./config.yml
79+
# otherwise 'cats' errors out.
80+
export CATS_CONFIG_FILE=/path/to/config.yml
81+
cats --duration 480
7182
7283
7384
Displaying carbon footprint estimates

tests/test_configure.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ def test_config_from_file_default(local_config_file):
5050
assert configmapping == CATS_CONFIG
5151

5252

53+
def test_config_from_env(local_config_file):
54+
os.environ["CATS_CONFIG_FILE"] = str(local_config_file / "config.yml")
55+
configmapping = config_from_file()
56+
assert configmapping == CATS_CONFIG
57+
58+
5359
@patch("cats.configure.requests")
5460
def test_get_location_from_config_or_args(mock_requests):
5561
expected_location = "SW7"

0 commit comments

Comments
 (0)