Added inspect.get_config() method to get the configuration programmatically

This commit is contained in:
Fabio Manganiello 2020-08-31 18:26:08 +02:00
parent 3c3ee09d90
commit dc1b54961f
3 changed files with 29 additions and 6 deletions

View file

@ -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()
if key:
return _default_config_instance._config.get(key)
return _default_config_instance._config
# vim:sw=4:ts=4:et:

View file

@ -63,7 +63,11 @@ class Message(object):
if isinstance(obj, JSONAble):
return obj.to_json()
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()

View file

@ -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: