|
1 | 1 | __author__ = "desultory" |
2 | | -__version__ = "0.4.1" |
| 2 | +__version__ = "0.5.0" |
3 | 3 |
|
4 | 4 |
|
5 | 5 | from functools import wraps |
6 | 6 | from .walk_dict import walk_dict |
7 | 7 |
|
8 | 8 |
|
9 | | -def check_dict(key, validate_dict=None, value=None, value_arg=None, contains=False, unset=False, |
10 | | - raise_exception=False, log_level=10, return_val=False, return_arg=None, message=None): |
| 9 | +def check_dict(key, validate_dict=None, value=None, value_arg=None, |
| 10 | + contains=False, unset=False, not_empty=False, |
| 11 | + raise_exception=False, return_val=False, return_arg=None, |
| 12 | + log_level=10, message=None): |
11 | 13 | """ |
12 | 14 | Adds a check for a dict key to a function. |
13 | 15 | If the dict is nto passed, uses the first argument of the function (often self). |
@@ -53,10 +55,14 @@ def dispatch_msg(msg): |
53 | 55 | return dispatch_msg("[%s] Key is set when it should be unset: %s." % (func.__name__, key)) |
54 | 56 | else: |
55 | 57 | dict_val = validate_dict[key] |
| 58 | + if not_empty and dict_val: |
| 59 | + return dispatch_msg("[%s] Key is not empty: %s." % (func.__name__, key)) |
56 | 60 | elif isinstance(key, dict): |
57 | 61 | if dict_val := walk_dict(validate_dict, key, fail_safe=not raise_exception): |
58 | 62 | if unset: |
59 | 63 | return dispatch_msg("[%s] Key is set when it should be unset: %s." % (func.__name__, dict_val)) |
| 64 | + if not_empty and dict_val: |
| 65 | + return dispatch_msg("[%s] Key is not empty: %s." % (func.__name__, dict_val)) |
60 | 66 | elif not unset: |
61 | 67 | raise ValueError("Unable to find key: %s." % key) |
62 | 68 |
|
|
0 commit comments