From 2c2e11617e404f6eaa11da470d07e44e76d9f226 Mon Sep 17 00:00:00 2001 From: Chong Shen Ng Date: Mon, 16 Dec 2024 19:42:13 +0000 Subject: [PATCH] refactor(framework) Move `print_json_error` to utility function (#4711) --- src/py/flwr/cli/ls.py | 19 +++---------------- src/py/flwr/cli/run/run.py | 18 +++--------------- src/py/flwr/common/logger.py | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/py/flwr/cli/ls.py b/src/py/flwr/cli/ls.py index 1e8b56b576e4..e845583d61ac 100644 --- a/src/py/flwr/cli/ls.py +++ b/src/py/flwr/cli/ls.py @@ -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, @@ -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 @@ -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}", @@ -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)), - } - ) - ) diff --git a/src/py/flwr/cli/run/run.py b/src/py/flwr/cli/run/run.py index 34e7fc50e2f0..19db2ade39dc 100644 --- a/src/py/flwr/cli/run/run.py +++ b/src/py/flwr/cli/run/run.py @@ -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 @@ -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, @@ -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}", @@ -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)), - } - ) - ) diff --git a/src/py/flwr/common/logger.py b/src/py/flwr/common/logger.py index 00395cacf2b8..64763faf740d 100644 --- a/src/py/flwr/common/logger.py +++ b/src/py/flwr/common/logger.py @@ -15,6 +15,7 @@ """Flower Logger.""" +import json as _json import logging import re import sys @@ -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 @@ -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( "[" @@ -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)), + } + ) + )