|
5 | 5 | from typing import Tuple, Callable, Mapping, Sequence |
6 | 6 | from io import IOBase |
7 | 7 |
|
8 | | -from .utils import recurse_check_structure |
9 | 8 | from .storage import StoreMixin, StoreNotOpenError |
10 | 9 | from errbot.backends.base import Message, Presence, Stream, Room, Identifier, ONLINE, Card |
11 | 10 |
|
12 | 11 | log = logging.getLogger(__name__) |
13 | 12 |
|
14 | 13 |
|
| 14 | +class ValidationException(Exception): |
| 15 | + pass |
| 16 | + |
| 17 | + |
| 18 | +def _recurse_check_plugin_configuration(sample, to_check): |
| 19 | + sample_type = type(sample) |
| 20 | + to_check_type = type(to_check) |
| 21 | + |
| 22 | + # Skip this check if the sample is None because it will always be something |
| 23 | + # other than NoneType when changed from the default. Raising ValidationException |
| 24 | + # would make no sense then because it would defeat the whole purpose of having |
| 25 | + # that key in the sample when it could only ever be None. |
| 26 | + if sample is not None and sample_type != to_check_type: |
| 27 | + raise ValidationException( |
| 28 | + '%s [%s] is not the same type as %s [%s]' % (sample, sample_type, to_check, to_check_type)) |
| 29 | + |
| 30 | + if sample_type in (list, tuple): |
| 31 | + for element in to_check: |
| 32 | + _recurse_check_plugin_configuration(sample[0], element) |
| 33 | + return |
| 34 | + |
| 35 | + if sample_type == dict: |
| 36 | + for key in sample: |
| 37 | + if key not in to_check: |
| 38 | + raise ValidationException("%s doesn't contain the key %s" % (to_check, key)) |
| 39 | + for key in to_check: |
| 40 | + if key not in sample: |
| 41 | + raise ValidationException("%s contains an unknown key %s" % (to_check, key)) |
| 42 | + for key in sample: |
| 43 | + _recurse_check_plugin_configuration(sample[key], to_check[key]) |
| 44 | + return |
| 45 | + |
| 46 | + |
15 | 47 | class CommandError(Exception): |
16 | 48 | """ |
17 | 49 | Use this class to report an error condition from your commands, the command |
@@ -312,7 +344,7 @@ def check_configuration(self, configuration: Mapping) -> None: |
312 | 344 |
|
313 | 345 | :param configuration: the configuration to be checked. |
314 | 346 | """ |
315 | | - recurse_check_structure(self.get_configuration_template(), configuration) # default behavior |
| 347 | + _recurse_check_plugin_configuration(self.get_configuration_template(), configuration) # default behavior |
316 | 348 |
|
317 | 349 | def configure(self, configuration: Mapping) -> None: |
318 | 350 | """ |
|
0 commit comments