summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/services/settings.py
blob: 373d3966ab7a3a0a08b30b5cb20375d3ed779d7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# -*- coding: utf-8 -*-
from contextlib import contextmanager

import cherrypy


class SettingsService:
    @contextmanager
    # pylint: disable=no-self-argument
    def attribute_handler(name):
        """
        :type name: str|dict[str, str]
        :rtype: str|dict[str, str]
        """
        if isinstance(name, dict):
            result = {
                _to_native(key): value
                for key, value in name.items()
            }
        else:
            result = _to_native(name)

        try:
            yield result
        except AttributeError:  # pragma: no cover - handling is too obvious
            raise cherrypy.NotFound(result)  # pragma: no cover - handling is too obvious


def _to_native(setting):
    return setting.upper().replace('-', '_')