Skip to content

Commit

Permalink
refactor(framework) Move print_json_error to utility function (#4711)
Browse files Browse the repository at this point in the history
  • Loading branch information
chongshenng authored Dec 16, 2024
1 parent 7a5ba4b commit 2c2e116
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 32 deletions.
19 changes: 3 additions & 16 deletions src/py/flwr/cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
import json
from datetime import datetime, timedelta
from pathlib import Path
from typing import Annotated, Optional, Union
from typing import Annotated, Optional

import typer
from rich.console import Console
from rich.table import Table
from rich.text import Text
from typer import Exit

from flwr.cli.config_utils import (
exit_if_no_address,
Expand All @@ -35,7 +34,7 @@
)
from flwr.common.constant import FAB_CONFIG_FILE, CliOutputFormat, SubStatus
from flwr.common.date import format_timedelta, isoformat8601_utc
from flwr.common.logger import redirect_output, remove_emojis, restore_output
from flwr.common.logger import print_json_error, redirect_output, restore_output
from flwr.common.serde import run_from_proto
from flwr.common.typing import Run
from flwr.proto.exec_pb2 import ( # pylint: disable=E0611
Expand Down Expand Up @@ -145,7 +144,7 @@ def ls( # pylint: disable=too-many-locals, too-many-branches
if suppress_output:
restore_output()
e_message = captured_output.getvalue()
_print_json_error(e_message, err)
print_json_error(e_message, err)
else:
typer.secho(
f"{err}",
Expand Down Expand Up @@ -323,15 +322,3 @@ def _display_one_run(
Console().print_json(_to_json(formatted_runs))
else:
Console().print(_to_table(formatted_runs))


def _print_json_error(msg: str, e: Union[Exit, Exception]) -> None:
"""Print error message as JSON."""
Console().print_json(
json.dumps(
{
"success": False,
"error-message": remove_emojis(str(msg) + "\n" + str(e)),
}
)
)
18 changes: 3 additions & 15 deletions src/py/flwr/cli/run/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import json
import subprocess
from pathlib import Path
from typing import Annotated, Any, Optional, Union
from typing import Annotated, Any, Optional

import typer
from rich.console import Console
Expand All @@ -37,7 +37,7 @@
user_config_to_configsrecord,
)
from flwr.common.constant import CliOutputFormat
from flwr.common.logger import redirect_output, remove_emojis, restore_output
from flwr.common.logger import print_json_error, redirect_output, restore_output
from flwr.common.serde import (
configs_record_to_proto,
fab_to_proto,
Expand Down Expand Up @@ -122,7 +122,7 @@ def run(
if suppress_output:
restore_output()
e_message = captured_output.getvalue()
_print_json_error(e_message, err)
print_json_error(e_message, err)
else:
typer.secho(
f"{err}",
Expand Down Expand Up @@ -239,15 +239,3 @@ def _run_without_exec_api(
check=True,
text=True,
)


def _print_json_error(msg: str, e: Union[typer.Exit, Exception]) -> None:
"""Print error message as JSON."""
Console().print_json(
json.dumps(
{
"success": False,
"error-message": remove_emojis(str(msg) + "\n" + str(e)),
}
)
)
17 changes: 16 additions & 1 deletion src/py/flwr/common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Flower Logger."""


import json as _json
import logging
import re
import sys
Expand All @@ -27,6 +28,8 @@
from typing import TYPE_CHECKING, Any, Optional, TextIO, Union

import grpc
import typer
from rich.console import Console

from flwr.proto.log_pb2 import PushLogsRequest # pylint: disable=E0611
from flwr.proto.node_pb2 import Node # pylint: disable=E0611
Expand Down Expand Up @@ -377,7 +380,7 @@ def stop_log_uploader(
log_uploader.join()


def remove_emojis(text: str) -> str:
def _remove_emojis(text: str) -> str:
"""Remove emojis from the provided text."""
emoji_pattern = re.compile(
"["
Expand All @@ -391,3 +394,15 @@ def remove_emojis(text: str) -> str:
flags=re.UNICODE,
)
return emoji_pattern.sub(r"", text)


def print_json_error(msg: str, e: Union[typer.Exit, Exception]) -> None:
"""Print error message as JSON."""
Console().print_json(
_json.dumps(
{
"success": False,
"error-message": _remove_emojis(str(msg) + "\n" + str(e)),
}
)
)

0 comments on commit 2c2e116

Please sign in to comment.