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

WIP feat(cli) add option to disable api in command line #722

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions codecarbon/cli/cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def get_api_endpoint(path: Optional[Path] = None):
return "https://api.codecarbon.io"


def get_api_enabled(path: Optional[Path] = None):
p = path or Path.cwd().resolve() / ".codecarbon.config"
if p.exists():
config = configparser.ConfigParser()
config.read(str(p))
if "codecarbon" in config.sections():
d = dict(config["codecarbon"])
if "api_enabled" in d:
return "1"
else:
with p.open("a") as f:
f.write("api_enabled=0\n")
return "0"


def get_existing_local_exp_id(path: Optional[Path] = None):
p = path or Path.cwd().resolve() / ".codecarbon.config"
if p.exists():
Expand Down
26 changes: 23 additions & 3 deletions codecarbon/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
from pathlib import Path
from typing import Optional
from uuid import uuid4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import seems to not be used, right ?

Copy link
Contributor Author

@prmths128 prmths128 Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's used to set a random experiment id, since I thought the experiment id might be needed even with no api.
Not sure though, I have to check.


import questionary
import requests
Expand All @@ -15,6 +16,7 @@
from codecarbon import __app_name__, __version__
from codecarbon.cli.cli_utils import (
create_new_config_file,
get_api_enabled,
get_api_endpoint,
get_config,
get_existing_local_exp_id,
Expand Down Expand Up @@ -177,13 +179,29 @@ def config():
else:
file_path = create_new_config_file()

api_enabled = get_api_enabled()
api_enabled = typer.prompt(
f"API is currently {'enabled' if api_enabled == '1' else 'disabled'}. Press enter to continue or change to 0/1",
type=str,
default="0",
)
overwrite_local_config("api_enabled", api_enabled, path=file_path)

if api_enabled == "0":
overwrite_local_config("experiment_id", str(uuid4()), path=file_path)
return

api_endpoint = get_api_endpoint(file_path)
api_endpoint = typer.prompt(
f"Current API endpoint is {api_endpoint}. Press enter to continue or input other url",
f"Current API endpoint is at {api_endpoint}. Press enter to continue or input other url",
type=str,
default=api_endpoint,
)
overwrite_local_config("api_endpoint", api_endpoint, path=file_path)

print(f"Logging into the auth server at {AUTH_SERVER_URL}")
fief_auth.authorize()

api = ApiClient(endpoint_url=api_endpoint)
api.set_access_token(_get_access_token())
organizations = api.get_list_organizations()
Expand Down Expand Up @@ -305,8 +323,8 @@ def monitor(
int, typer.Argument(help="Number of measures between API calls.")
] = 30,
api: Annotated[
bool, typer.Option(help="Choose to call Code Carbon API or not")
] = True,
Optional[bool], typer.Option(help="Choose to call Code Carbon API or not")
] = None,
):
"""Monitor your machine's carbon emissions.

Expand All @@ -315,6 +333,8 @@ def monitor(
api_call_interval (Annotated[int, typer.Argument, optional): Number of measures before calling API. Defaults to 30.
api (Annotated[bool, typer.Option, optional): Choose to call Code Carbon API or not. Defaults to True.
"""
if api is None:
api = get_api_enabled() == "1"
experiment_id = get_existing_local_exp_id()
if api and experiment_id is None:
print("ERROR: No experiment id, call 'codecarbon init' first.", err=True)
Expand Down
Loading