From 2164f5543a6728f47ddcccf189b22b18a6f340ea Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Wed, 16 Aug 2023 16:14:01 -0400 Subject: [PATCH] cephadm: move assorted data manipulation funcs to data_utils.py Signed-off-by: John Mulligan Pair-programmed-with: Adam King Co-authored-by: Adam King --- src/cephadm/cephadm.py | 40 +-------------------------- src/cephadm/cephadmlib/data_utils.py | 41 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 39 deletions(-) create mode 100644 src/cephadm/cephadmlib/data_utils.py diff --git a/src/cephadm/cephadm.py b/src/cephadm/cephadm.py index 48f4b6a0aaea8..303ba4f8d2570 100755 --- a/src/cephadm/cephadm.py +++ b/src/cephadm/cephadm.py @@ -109,6 +109,7 @@ from cephadmlib.container_engines import ( check_container_engine, find_container_engine, ) +from cephadmlib.data_utils import dict_get, dict_get_join FuncT = TypeVar('FuncT', bound=Callable) @@ -1404,45 +1405,6 @@ def touch(file_path: str, uid: Optional[int] = None, gid: Optional[int] = None) ################################## -def dict_get(d: Dict, key: str, default: Any = None, require: bool = False) -> Any: - """ - Helper function to get a key from a dictionary. - :param d: The dictionary to process. - :param key: The name of the key to get. - :param default: The default value in case the key does not - exist. Default is `None`. - :param require: Set to `True` if the key is required. An - exception will be raised if the key does not exist in - the given dictionary. - :return: Returns the value of the given key. - :raises: :exc:`self.Error` if the given key does not exist - and `require` is set to `True`. - """ - if require and key not in d.keys(): - raise Error('{} missing from dict'.format(key)) - return d.get(key, default) # type: ignore - -################################## - - -def dict_get_join(d: Dict[str, Any], key: str) -> Any: - """ - Helper function to get the value of a given key from a dictionary. - `List` values will be converted to a string by joining them with a - line break. - :param d: The dictionary to process. - :param key: The name of the key to get. - :return: Returns the value of the given key. If it was a `list`, it - will be joining with a line break. - """ - value = d.get(key) - if isinstance(value, list): - value = '\n'.join(map(str, value)) - return value - -################################## - - def get_supported_daemons(): # type: () -> List[str] supported_daemons = list(Ceph.daemons) diff --git a/src/cephadm/cephadmlib/data_utils.py b/src/cephadm/cephadmlib/data_utils.py new file mode 100644 index 0000000000000..9453f77398f26 --- /dev/null +++ b/src/cephadm/cephadmlib/data_utils.py @@ -0,0 +1,41 @@ +# data_utils.py - assorted data management functions + + +from typing import Dict, Any + +from .exceptions import Error + + +def dict_get(d: Dict, key: str, default: Any = None, require: bool = False) -> Any: + """ + Helper function to get a key from a dictionary. + :param d: The dictionary to process. + :param key: The name of the key to get. + :param default: The default value in case the key does not + exist. Default is `None`. + :param require: Set to `True` if the key is required. An + exception will be raised if the key does not exist in + the given dictionary. + :return: Returns the value of the given key. + :raises: :exc:`self.Error` if the given key does not exist + and `require` is set to `True`. + """ + if require and key not in d.keys(): + raise Error('{} missing from dict'.format(key)) + return d.get(key, default) # type: ignore + + +def dict_get_join(d: Dict[str, Any], key: str) -> Any: + """ + Helper function to get the value of a given key from a dictionary. + `List` values will be converted to a string by joining them with a + line break. + :param d: The dictionary to process. + :param key: The name of the key to get. + :return: Returns the value of the given key. If it was a `list`, it + will be joining with a line break. + """ + value = d.get(key) + if isinstance(value, list): + value = '\n'.join(map(str, value)) + return value -- 2.39.5