-
-
Notifications
You must be signed in to change notification settings - Fork 308
feat(cli): forward monitor tracker options #1275
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
Open
lntutor
wants to merge
1
commit into
mlco2:master
Choose a base branch
from
lntutor:fix/monitor-options-1273
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+119
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,8 +2,9 @@ | |
| import signal | ||
| import sys | ||
| import time | ||
| from inspect import signature | ||
| from pathlib import Path | ||
| from typing import Optional | ||
| from typing import Optional, get_args | ||
|
|
||
| import typer | ||
| from rich import print | ||
|
|
@@ -27,6 +28,78 @@ | |
| codecarbon = typer.Typer(no_args_is_help=True) | ||
|
|
||
|
|
||
| def _tracker_option_types() -> dict[str, object]: | ||
| """Return CLI-forwardable options from the tracker constructor.""" | ||
| from codecarbon.emissions_tracker import BaseEmissionsTracker | ||
|
|
||
| return { | ||
| name: parameter.annotation | ||
| for name, parameter in signature( | ||
| BaseEmissionsTracker.__init__ | ||
| ).parameters.items() | ||
| if name != "self" and parameter.kind.name != "VAR_KEYWORD" | ||
| } | ||
|
|
||
|
|
||
| def _tracker_option_value_type(annotation: object) -> object: | ||
| if isinstance(annotation, str): | ||
| for value_type in (bool, int, float, str): | ||
| if value_type.__name__ in annotation: | ||
| return value_type | ||
| return str | ||
|
|
||
| candidates = [ | ||
| candidate for candidate in get_args(annotation) if candidate is not type(None) | ||
| ] | ||
| return candidates[0] if candidates else annotation | ||
|
|
||
|
|
||
| def _parse_tracker_option(value: str, annotation: object) -> object: | ||
| """Convert a CLI value using the scalar type declared by the tracker.""" | ||
| value_type = _tracker_option_value_type(annotation) | ||
| if value_type is bool: | ||
| return value.lower() == "true" | ||
| if value_type in (int, float, str): | ||
| return value_type(value) | ||
| return value | ||
|
|
||
|
|
||
| def _extract_tracker_options(args: list[str]) -> tuple[dict[str, object], list[str]]: | ||
| """Remove tracker options that Typer does not declare from command arguments.""" | ||
| option_types = _tracker_option_types() | ||
| tracker_options: dict[str, object] = {} | ||
| remaining = list(args) | ||
|
|
||
| while remaining and remaining[0].startswith("--"): | ||
| raw_option = remaining[0][2:] | ||
| option_name, separator, raw_value = raw_option.partition("=") | ||
| negated = option_name.startswith("no-") | ||
| normalized_name = option_name[3:] if negated else option_name | ||
| parameter_name = normalized_name.replace("-", "_") | ||
| annotation = option_types.get(parameter_name) | ||
| if annotation is None: | ||
| break | ||
|
|
||
| remaining.pop(0) | ||
| value_type = _tracker_option_value_type(annotation) | ||
| if value_type is bool: | ||
| if separator: | ||
| tracker_options[parameter_name] = _parse_tracker_option( | ||
| raw_value, annotation | ||
| ) | ||
| else: | ||
| tracker_options[parameter_name] = not negated | ||
| continue | ||
|
|
||
| if not separator: | ||
| if not remaining: | ||
| raise typer.BadParameter(f"Option '--{option_name}' requires a value") | ||
| raw_value = remaining.pop(0) | ||
| tracker_options[parameter_name] = _parse_tracker_option(raw_value, annotation) | ||
|
|
||
| return tracker_options, remaining | ||
|
|
||
|
|
||
| def main(): | ||
| """ | ||
| Main entry point for the CodeCarbon CLI application. | ||
|
|
@@ -362,11 +435,17 @@ def monitor( | |
| ): | ||
| """Monitor your machine's carbon emissions.""" | ||
|
|
||
| extra_tracker_args, command_args = _extract_tracker_options( | ||
| list(getattr(ctx, "args", None) or []) | ||
| ) | ||
| ctx.args = command_args | ||
|
|
||
| # Shared tracker args so monitor and run_and_monitor behave the same | ||
| tracker_args = { | ||
| "measure_power_secs": measure_power_secs, | ||
| "api_call_interval": api_call_interval, | ||
| "log_level": log_level, | ||
| **extra_tracker_args, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a fan of flexible arguments passing, even if it includes some lag with the tracker's supported arguments list I prefer that we have all of them listed here. Can you set them explicitely ? |
||
| } | ||
| # Set up the tracker arguments based on mode (offline vs online) and validate required args for each mode | ||
| if offline: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move those functions below the main one ? It might also be interesting to just move them to another file and import them, this file starts to get big & diverse.