Skip to content

Commit f5ddb42

Browse files
committed
WIP feat(cli) add option to disable api in command line
1 parent 6973ed9 commit f5ddb42

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
@@ -2,6 +2,7 @@
22
import time
33
from pathlib import Path
44
from typing import Optional
5+
from uuid import uuid4
56

67
import questionary
78
import requests
@@ -15,6 +16,7 @@
1516
from codecarbon import __app_name__, __version__
1617
from codecarbon.cli.cli_utils import (
1718
create_new_config_file,
19+
get_api_enabled,
1820
get_api_endpoint,
1921
get_config,
2022
get_existing_local_exp_id,
@@ -177,13 +179,29 @@ def config():
177179
else:
178180
file_path = create_new_config_file()
179181

182+
api_enabled = get_api_enabled()
183+
api_enabled = typer.prompt(
184+
f"API is currently {'enabled' if api_enabled == '1' else 'disabled'}. Press enter to continue or change to 0/1",
185+
type=str,
186+
default="0",
187+
)
188+
overwrite_local_config("api_enabled", api_enabled, path=file_path)
189+
190+
if api_enabled == "0":
191+
overwrite_local_config("experiment_id", str(uuid4()), path=file_path)
192+
return
193+
180194
api_endpoint = get_api_endpoint(file_path)
181195
api_endpoint = typer.prompt(
182-
f"Current API endpoint is {api_endpoint}. Press enter to continue or input other url",
196+
f"Current API endpoint is at {api_endpoint}. Press enter to continue or input other url",
183197
type=str,
184198
default=api_endpoint,
185199
)
186200
overwrite_local_config("api_endpoint", api_endpoint, path=file_path)
201+
202+
print(f"Logging into the auth server at {AUTH_SERVER_URL}")
203+
fief_auth.authorize()
204+
187205
api = ApiClient(endpoint_url=api_endpoint)
188206
api.set_access_token(_get_access_token())
189207
organizations = api.get_list_organizations()
@@ -305,8 +323,8 @@ def monitor(
305323
int, typer.Argument(help="Number of measures between API calls.")
306324
] = 30,
307325
api: Annotated[
308-
bool, typer.Option(help="Choose to call Code Carbon API or not")
309-
] = True,
326+
Optional[bool], typer.Option(help="Choose to call Code Carbon API or not")
327+
] = None,
310328
):
311329
"""Monitor your machine's carbon emissions.
312330
@@ -315,6 +333,8 @@ def monitor(
315333
api_call_interval (Annotated[int, typer.Argument, optional): Number of measures before calling API. Defaults to 30.
316334
api (Annotated[bool, typer.Option, optional): Choose to call Code Carbon API or not. Defaults to True.
317335
"""
336+
if api is None:
337+
api = get_api_enabled() == "1"
318338
experiment_id = get_existing_local_exp_id()
319339
if api and experiment_id is None:
320340
print("ERROR: No experiment id, call 'codecarbon init' first.", err=True)

0 commit comments

Comments
 (0)