Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR 110 redux #121

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cats/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import logging
import sys
import os
from collections.abc import Mapping
from typing import Any, Optional

Expand Down Expand Up @@ -76,7 +77,16 @@
return yaml.safe_load(f)
logging.info(f"Using provided config file: {configpath}\n")
else:
# if no path provided, look for `config.yml` in current directory
# if no path provided, try to use config environment variable
cfile = os.getenv("CATS_CONFIG_FILE")
if cfile is not None:
try:
with open(cfile, "r") as f:
return yaml.safe_load(f)
logging.info("Using config.yml found in CATS_CONFIG_FILE\n")
except FileNotFoundError:
logging.warning("CATS_CONFIG_FILE config file not found")

Check warning on line 88 in cats/configure.py

View check run for this annotation

Codecov / codecov/patch

cats/configure.py#L86-L88

Added lines #L86 - L88 were not covered by tests
# if no path provided and no env variable, look for `config.yml` in current directory
try:
with open("config.yml", "r") as f:
return yaml.safe_load(f)
Expand Down
15 changes: 13 additions & 2 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ Use the ``--config`` option to specify a path to the configuration
file, relative to the current directory.

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

.. code-block:: shell

Expand All @@ -67,7 +69,16 @@ machine IP address:
Carbon intensity if job started now = 117.95 gCO2eq/kWh
Carbon intensity at optimal time = 60.93 gCO2eq/kWh

Use --format=json to get this in machine readable format
Use --format=json to get this in machine readable format

.. code-block::console

# location information is provided by the file
# specified in $CATS_CONFIG_FILE
# If not, it looks for ./config.yaml
# otherwise 'cats' errors out.
export CATS_CONFIG_FILE=/path/to/config.yaml
cats --duration 480


Displaying carbon footprint estimates
Expand Down
6 changes: 6 additions & 0 deletions tests/test_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_config_from_file_default(local_config_file):
assert configmapping == CATS_CONFIG


def test_config_from_env(local_config_file):
os.environ["CATS_CONFIG_FILE"] = str(local_config_file / "config.yml")
configmapping = config_from_file()
assert configmapping == CATS_CONFIG


@patch("cats.configure.requests")
def test_get_location_from_config_or_args(mock_requests):
expected_location = "SW7"
Expand Down
Loading