|
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
|
|
|
SpacingHelper.from_section(section, tabwidth=8) |
|
15
|
|
|
creates a SpacingHelper and if the "tabwidth" setting is needed and not |
|
16
|
|
|
contained in section, 8 will be taken. |
|
17
|
|
|
|
|
18
|
|
|
It is recommended to write the prototype of the __init__ method according |
|
19
|
|
|
to this example: |
|
20
|
|
|
def __init__(self, setting_one: int, setting_two: bool=False): |
|
21
|
|
|
pass # Implementation |
|
22
|
|
|
|
|
23
|
|
|
This way the get_optional_settings and the get_non_optional_settings method |
|
24
|
|
|
will extract automatically that |
|
25
|
|
|
* setting_one should be an integer |
|
26
|
|
|
* setting_two should be a bool and defaults to False |
|
27
|
|
|
|
|
28
|
|
|
If you write a documentation comment, you can use :param to add |
|
29
|
|
|
descriptions to your parameters. These will be available too automatically. |
|
30
|
|
|
""" |
|
31
|
|
|
def __init__(self): |
|
32
|
|
|
pass # Method needs to be available |
|
33
|
|
|
|
|
34
|
|
|
@classmethod |
|
35
|
|
|
def from_section(cls, section, **kwargs): |
|
36
|
|
|
""" |
|
37
|
|
|
Creates the object from a section object. |
|
38
|
|
|
|
|
39
|
|
|
:param section: A section object containing at least the settings |
|
40
|
|
|
specified by get_non_optional_settings() |
|
41
|
|
|
:param kwargs: Additional keyword arguments |
|
42
|
|
|
""" |
|
43
|
|
|
kwargs.update(cls.get_metadata().create_params_from_section(section)) |
|
44
|
|
|
|
|
45
|
|
|
return cls(**kwargs) |
|
46
|
|
|
|
|
47
|
|
|
@classmethod |
|
48
|
|
|
def get_metadata(cls): |
|
49
|
|
|
return FunctionMetadata.from_function(cls.__init__, omit={"self"}) |
|
50
|
|
|
|
|
51
|
|
|
@classmethod |
|
52
|
|
|
def get_non_optional_settings(cls): |
|
53
|
|
|
""" |
|
54
|
|
|
Retrieves the minimal set of settings that need to be defined in order |
|
55
|
|
|
to use this object. |
|
56
|
|
|
|
|
57
|
|
|
:return: a dictionary of needed settings as keys and help texts as |
|
58
|
|
|
values |
|
59
|
|
|
""" |
|
60
|
|
|
return cls.get_metadata().non_optional_params |
|
61
|
|
|
|
|
62
|
|
|
@classmethod |
|
63
|
|
|
def get_optional_settings(cls): |
|
64
|
|
|
""" |
|
65
|
|
|
Retrieves the settings needed IN ADDITION to the ones of |
|
66
|
|
|
get_non_optional_settings to use this object without internal defaults. |
|
67
|
|
|
|
|
68
|
|
|
:return: a dictionary of needed settings as keys and help texts as |
|
69
|
|
|
values |
|
70
|
|
|
""" |
|
71
|
|
|
return cls.get_metadata().optional_params |
|
72
|
|
|
|