From dc1b54961f1048b66f3508d1d802340a1cfd2c71 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Mon, 31 Aug 2020 18:26:08 +0200 Subject: [PATCH] Added `inspect.get_config()` method to get the configuration programmatically --- platypush/config/__init__.py | 14 +++++++++----- platypush/message/__init__.py | 6 +++++- platypush/plugins/inspect.py | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index 90357972d..04418a896 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -344,15 +344,19 @@ class Config(object): _default_config_instance = Config(cfgfile) @staticmethod - def get(key): + def get(key: Optional[str] = None): """ - Gets a config value - Params: - key -- Config key to get + Get a config value or the whole configuration object. + + :param key: Configuration entry to get (default: all entries). """ global _default_config_instance if _default_config_instance is None: _default_config_instance = Config() - return _default_config_instance._config.get(key) + + if key: + return _default_config_instance._config.get(key) + + return _default_config_instance._config # vim:sw=4:ts=4:et: diff --git a/platypush/message/__init__.py b/platypush/message/__init__.py index 5975a065c..fcd7cbd0e 100644 --- a/platypush/message/__init__.py +++ b/platypush/message/__init__.py @@ -63,7 +63,11 @@ class Message(object): if isinstance(obj, JSONAble): return obj.to_json() - return super().default(obj) + try: + return super().default(obj) + except Exception as e: + logger.warning('Could not serialize object type {}: {}: {}'.format( + type(obj), str(e), obj)) def __init__(self, timestamp=None, *args, **kwargs): self.timestamp = timestamp or time.time() diff --git a/platypush/plugins/inspect.py b/platypush/plugins/inspect.py index d41d622e1..0a6dad4a7 100644 --- a/platypush/plugins/inspect.py +++ b/platypush/plugins/inspect.py @@ -4,6 +4,7 @@ import json import pkgutil import re import threading +from typing import Optional import platypush.backend import platypush.plugins @@ -339,5 +340,19 @@ class InspectPlugin(Plugin): """ return json.loads(json.dumps(Config.get_procedures(), cls=ProcedureEncoder)) + @action + def get_config(self, entry: Optional[str] = None) -> dict: + """ + Return the configuration of the application or of a section. + + :param entry: [Optional] configuration entry name to retrieve (e.g. ``workdir`` or ``backend.http``). + :return: The requested configuration object. + """ + if entry: + return Config.get(entry) + + cfg = Config.get() + return cfg + # vim:sw=4:ts=4:et: