Skip to content

Commit 6c6adcd

Browse files
prmths128benoit-cty
authored andcommitted
WIP feat(cli) add option to disable api in command line
1 parent 4b382d3 commit 6c6adcd

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

codecarbon/cli/cli_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ def get_api_endpoint(path: Optional[Path] = None):
3737
return "https://api.codecarbon.io"
3838

3939

40+
def get_api_enabled(path: Optional[Path] = None):
41+
p = path or Path.cwd().resolve() / ".codecarbon.config"
42+
if p.exists():
43+
config = configparser.ConfigParser()
44+
config.read(str(p))
45+
if "codecarbon" in config.sections():
46+
d = dict(config["codecarbon"])
47+
if "api_enabled" in d:
48+
return "1"
49+
else:
50+
with p.open("a") as f:
51+
f.write("api_enabled=0\n")
52+
return "0"
53+
54+
4055
def get_existing_local_exp_id(path: Optional[Path] = None):
4156
p = path or Path.cwd().resolve() / ".codecarbon.config"
4257
if p.exists():

codecarbon/cli/main.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
from pathlib import Path
55
from typing import Optional
6+
from uuid import uuid4
67

78
import questionary
89
import requests
@@ -16,6 +17,7 @@
1617
from codecarbon import __app_name__, __version__
1718
from codecarbon.cli.cli_utils import (
1819
create_new_config_file,
20+
get_api_enabled,
1921
get_api_endpoint,
2022
get_config,
2123
get_existing_local_exp_id,
@@ -201,13 +203,29 @@ def config():
201203
else:
202204
file_path = create_new_config_file()
203205

206+
api_enabled = get_api_enabled()
207+
api_enabled = typer.prompt(
208+
f"API is currently {'enabled' if api_enabled == '1' else 'disabled'}. Press enter to continue or change to 0/1",
209+
type=str,
210+
default="0",
211+
)
212+
overwrite_local_config("api_enabled", api_enabled, path=file_path)
213+
214+
if api_enabled == "0":
215+
overwrite_local_config("experiment_id", str(uuid4()), path=file_path)
216+
return
217+
204218
api_endpoint = get_api_endpoint(file_path)
205219
api_endpoint = typer.prompt(
206-
f"Current API endpoint is {api_endpoint}. Press enter to continue or input other url",
220+
f"Current API endpoint is at {api_endpoint}. Press enter to continue or input other url",
207221
type=str,
208222
default=api_endpoint,
209223
)
210224
overwrite_local_config("api_endpoint", api_endpoint, path=file_path)
225+
226+
print(f"Logging into the auth server at {AUTH_SERVER_URL}")
227+
fief_auth.authorize()
228+
211229
api = ApiClient(endpoint_url=api_endpoint)
212230
api.set_access_token(_get_access_token())
213231
organizations = api.get_list_organizations()
@@ -334,8 +352,8 @@ def monitor(
334352
int, typer.Argument(help="Number of measures between API calls.")
335353
] = 30,
336354
api: Annotated[
337-
bool, typer.Option(help="Choose to call Code Carbon API or not")
338-
] = True,
355+
Optional[bool], typer.Option(help="Choose to call Code Carbon API or not")
356+
] = None,
339357
):
340358
"""Monitor your machine's carbon emissions.
341359
@@ -344,6 +362,8 @@ def monitor(
344362
api_call_interval (Annotated[int, typer.Argument, optional): Number of measures before calling API. Defaults to 30.
345363
api (Annotated[bool, typer.Option, optional): Choose to call Code Carbon API or not. Defaults to True.
346364
"""
365+
if api is None:
366+
api = get_api_enabled() == "1"
347367
experiment_id = get_existing_local_exp_id()
348368
if api:
349369
if experiment_id is None:

0 commit comments

Comments
 (0)