Declare a section. The section declaration is a context manager where the context is a function, which can be used to declare a option. Example: with config_decl.declare('my_section') as my: my('my_option', conv = str, default = 'my_defa
(self, section_name)
| 53 | |
| 54 | @contextlib.contextmanager |
| 55 | def declare(self, section_name): |
| 56 | ''' Declare a section. |
| 57 | |
| 58 | The section declaration is a context manager where the context is a |
| 59 | function, which can be used to declare a option. Example: |
| 60 | |
| 61 | with config_decl.declare('my_section') as my: |
| 62 | my('my_option', |
| 63 | conv = str, |
| 64 | default = 'my_default') |
| 65 | ... |
| 66 | ''' |
| 67 | |
| 68 | # Create a section declaration if it does not exists |
| 69 | if section_name not in self.__sections: |
| 70 | section = self.__sections[section_name] = {} |
| 71 | |
| 72 | else: |
| 73 | section = self.__sections[section_name] |
| 74 | |
| 75 | # The option declaration function returned as the context |
| 76 | def declarator(option_name, |
| 77 | conv = str, |
| 78 | default = self.REQUIRED): |
| 79 | # Check if the section already contains the option |
| 80 | if option_name in section: |
| 81 | raise KeyError('Duplicated option declaration %s:%s' % (section_name, option_name)) |
| 82 | |
| 83 | # Add the option to the section |
| 84 | self.__sections[section_name][option_name] = self.Option(conv = conv, |
| 85 | default = default) |
| 86 | |
| 87 | # Return the function as context |
| 88 | yield declarator |
| 89 | |
| 90 | |
| 91 | @property |
no outgoing calls
no test coverage detected