Skip to content

Commit

Permalink
Merge pull request #506 from mlco2/feat/rework_cli
Browse files Browse the repository at this point in the history
Feat/rework cli
WIP : works with local API only now. Needs some work to include authentication
  • Loading branch information
LuisBlanche authored Aug 8, 2024
2 parents 1dc5249 + 65be9fa commit 489ba66
Show file tree
Hide file tree
Showing 29 changed files with 1,371 additions and 304 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,6 @@ code_carbon.db
# Local file
emissions*.csv*
tests/test_data/rapl/*

#asciinema
*.cast
1 change: 1 addition & 0 deletions codecarbon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
)

__all__ = ["EmissionsTracker", "OfflineEmissionsTracker", "track_emissions"]
__app_name__ = "codecarbon"
76 changes: 70 additions & 6 deletions codecarbon/cli/cli_utils.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,44 @@
import configparser
from pathlib import Path
from typing import Optional

import typer
from rich.prompt import Confirm


def get_config(path: Optional[Path] = None):
p = path or Path.cwd().resolve() / ".codecarbon.config"

def get_api_endpoint():
p = Path.cwd().resolve() / ".codecarbon.config"
if p.exists():
config = configparser.ConfigParser()
config.read(str(p))
if "codecarbon" in config.sections():
d = dict(config["codecarbon"])
return d

else:
raise FileNotFoundError(
"No .codecarbon.config file found in the current directory."
)


def get_api_endpoint(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_endpoint" in d:
return d["api_endpoint"]
else:
with p.open("a") as f:
f.write("api_endpoint=https://api.codecarbon.io\n")
return "https://api.codecarbon.io"


def get_existing_local_exp_id():
p = Path.cwd().resolve() / ".codecarbon.config"
def get_existing_local_exp_id(path: Optional[Path] = None):
p = path or Path.cwd().resolve() / ".codecarbon.config"
if p.exists():
config = configparser.ConfigParser()
config.read(str(p))
Expand All @@ -25,8 +48,9 @@ def get_existing_local_exp_id():
return d["experiment_id"]


def write_local_exp_id(exp_id):
p = Path.cwd().resolve() / ".codecarbon.config"
def write_local_exp_id(exp_id, path: Optional[Path] = None):
p = path or Path.cwd().resolve() / ".codecarbon.config"

config = configparser.ConfigParser()
if p.exists():
config.read(str(p))
Expand All @@ -37,3 +61,43 @@ def write_local_exp_id(exp_id):

with p.open("w") as f:
config.write(f)


def overwrite_local_config(config_name, value, path: Optional[Path] = None):
p = path or Path.cwd().resolve() / ".codecarbon.config"

config = configparser.ConfigParser()
if p.exists():
config.read(str(p))
if "codecarbon" not in config.sections():
config.add_section("codecarbon")

config["codecarbon"][config_name] = value
with p.open("w") as f:
config.write(f)


def create_new_config_file():
typer.echo("Creating new config file")
file_path = typer.prompt(
"Where do you want to put your config file ?",
type=str,
default="~/.codecarbon.config",
)
if file_path[0] == "~":
file_path = Path.home() / file_path[2:]
else:
file_path = Path(file_path)

if not file_path.parent.exists():
create = Confirm.ask(
"Parent folder does not exist do you want to create it (and parents) ?"
)
if create:
file_path.parent.mkdir(parents=True, exist_ok=True)

file_path.touch()
with open(file_path, "w") as f:
f.write("[codecarbon]\n")
typer.echo(f"Config file created at {file_path}")
return file_path
Loading

0 comments on commit 489ba66

Please sign in to comment.