From: Kefu Chai Date: Tue, 19 Jan 2021 06:08:39 +0000 (+0800) Subject: pybind/mgr: define HandleCommandResult using NamedTuple X-Git-Tag: v17.1.0~3164^2~11 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=d139166ea7514d113a41c28dae59184e3d2101d0;p=ceph.git pybind/mgr: define HandleCommandResult using NamedTuple simpler this way, so we don't need to define __new__ Signed-off-by: Kefu Chai --- diff --git a/src/pybind/mgr/cephadm/services/cephadmservice.py b/src/pybind/mgr/cephadm/services/cephadmservice.py index b6df0cd9b8e4..4632d5a0fa35 100644 --- a/src/pybind/mgr/cephadm/services/cephadmservice.py +++ b/src/pybind/mgr/cephadm/services/cephadmservice.py @@ -215,7 +215,7 @@ class CephadmService(metaclass=ABCMeta): if self.TYPE not in ['mon', 'osd', 'mds']: logger.info(out) - return HandleCommandResult(0, out, None) + return HandleCommandResult(0, out) r = HandleCommandResult(*self.mgr.mon_command({ 'prefix': f'{self.TYPE} ok-to-stop', diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 7da1f3c24749..5a87e528de03 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -1,7 +1,7 @@ import ceph_module # noqa from typing import cast, Tuple, Any, Dict, Generic, Optional, Callable, List, \ - Sequence, Union, TYPE_CHECKING + NamedTuple, Sequence, Union, TYPE_CHECKING if TYPE_CHECKING: import sys if sys.version_info >= (3, 8): @@ -15,7 +15,7 @@ import errno import functools import json import threading -from collections import defaultdict, namedtuple +from collections import defaultdict from enum import IntEnum import rados import re @@ -104,26 +104,20 @@ class CommandResult(object): return self.r, self.outb, self.outs -class HandleCommandResult(namedtuple('HandleCommandResult', ['retval', 'stdout', 'stderr'])): - def __new__(cls, retval=0, stdout="", stderr=""): - """ - Tuple containing the result of `handle_command()` - - Only write to stderr if there is an error, or in extraordinary circumstances +class HandleCommandResult(NamedTuple): + """ + Tuple containing the result of `handle_command()` - Avoid having `ceph foo bar` commands say "did foo bar" on success unless there - is critical information to include there. + Only write to stderr if there is an error, or in extraordinary circumstances - Everything programmatically consumable should be put on stdout + Avoid having `ceph foo bar` commands say "did foo bar" on success unless there + is critical information to include there. - :param retval: return code. E.g. 0 or -errno.EINVAL - :type retval: int - :param stdout: data of this result. - :type stdout: str - :param stderr: Typically used for error messages. - :type stderr: str - """ - return super(HandleCommandResult, cls).__new__(cls, retval, stdout, stderr) + Everything programmatically consumable should be put on stdout + """ + retval: int = 0 # return code. E.g. 0 or -errno.EINVAL + stdout: str = "" # data of this result. + stderr: str = "" # Typically used for error messages. class MonCommandFailed(RuntimeError): pass