SectionCreatable.get_metadata()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
from coalib.settings.FunctionMetadata import FunctionMetadata
2
3
4
class SectionCreatable:
5
    """
6
    A SectionCreatable is an object that is creatable out of a section object.
7
    Thus this is the class for many helper objects provided by the bearlib.
8
9
    If you want to use an object that inherits from this class the following
10
    approach is recommended: Instantiate it via the from_section method. You
11
    can provide default arguments via the lower case keyword arguments.
12
13
    Example:
14
15
    ::
16
17
        SpacingHelper.from_section(section, tabwidth=8)
18
19
    creates a SpacingHelper and if the "tabwidth" setting is needed and not
20
    contained in section, 8 will be taken.
21
22
    It is recommended to write the prototype of the __init__ method according
23
    to this example:
24
25
    ::
26
27
        def __init__(self, setting_one: int, setting_two: bool=False):
28
            pass  # Implementation
29
30
    This way the get_optional_settings and the get_non_optional_settings method
31
    will extract automatically that:
32
33
    -  setting_one should be an integer
34
    -  setting_two should be a bool and defaults to False
35
36
    If you write a documentation comment, you can use :param to add
37
    descriptions to your parameters. These will be available too automatically.
38
    """
39
40
    def __init__(self):
41
        pass  # Method needs to be available
42
43
    @classmethod
44
    def from_section(cls, section, **kwargs):
45
        """
46
        Creates the object from a section object.
47
48
        :param section: A section object containing at least the settings
49
                        specified by get_non_optional_settings()
50
        :param kwargs:  Additional keyword arguments
51
        """
52
        kwargs.update(cls.get_metadata().create_params_from_section(section))
53
54
        return cls(**kwargs)
55
56
    @classmethod
57
    def get_metadata(cls):
58
        return FunctionMetadata.from_function(cls.__init__, omit={"self"})
59
60
    @classmethod
61
    def get_non_optional_settings(cls):
62
        """
63
        Retrieves the minimal set of settings that need to be defined in order
64
        to use this object.
65
66
        :return: a dictionary of needed settings as keys and help texts as
67
                 values
68
        """
69
        return cls.get_metadata().non_optional_params
70
71
    @classmethod
72
    def get_optional_settings(cls):
73
        """
74
        Retrieves the settings needed IN ADDITION to the ones of
75
        get_non_optional_settings to use this object without internal defaults.
76
77
        :return: a dictionary of needed settings as keys and help texts as
78
                 values
79
        """
80
        return cls.get_metadata().optional_params
81