(value, checks, name)
| 22 | |
| 23 | |
| 24 | def check(value, checks, name): |
| 25 | def get_check_value(key, default_result): |
| 26 | return checks[key] if key in checks else default_result |
| 27 | |
| 28 | incorrect = False |
| 29 | if value == {}: |
| 30 | incorrect = True |
| 31 | if not incorrect and "type" in checks: |
| 32 | try: |
| 33 | value = eval(checks["type"])(value) # fixme remove eval |
| 34 | except: |
| 35 | incorrect = True |
| 36 | |
| 37 | if ( |
| 38 | not incorrect and "options" in checks and value not in checks["options"] |
| 39 | ): # FAILSTATE Value is not one of the options |
| 40 | incorrect = True |
| 41 | if ( |
| 42 | not incorrect |
| 43 | and "regex" in checks |
| 44 | and ( |
| 45 | (isinstance(value, str) and re.match(checks["regex"], value) is None) |
| 46 | or not isinstance(value, str) |
| 47 | ) |
| 48 | ): # FAILSTATE Value doesn't match regex, or has regex but is not a string. |
| 49 | incorrect = True |
| 50 | |
| 51 | if ( |
| 52 | not incorrect |
| 53 | and not hasattr(value, "__iter__") |
| 54 | and ( |
| 55 | ("nmin" in checks and checks["nmin"] is not None and value < checks["nmin"]) |
| 56 | or ("nmax" in checks and checks["nmax"] is not None and value > checks["nmax"]) |
| 57 | ) |
| 58 | ): |
| 59 | incorrect = True |
| 60 | if ( |
| 61 | not incorrect |
| 62 | and hasattr(value, "__iter__") |
| 63 | and ( |
| 64 | ("nmin" in checks and checks["nmin"] is not None and len(value) < checks["nmin"]) |
| 65 | or ("nmax" in checks and checks["nmax"] is not None and len(value) > checks["nmax"]) |
| 66 | ) |
| 67 | ): |
| 68 | incorrect = True |
| 69 | |
| 70 | if incorrect: |
| 71 | value = handle_input( |
| 72 | message=( |
| 73 | (("[blue]Example: " + str(checks["example"]) + "\n") if "example" in checks else "") |
| 74 | + "[red]" |
| 75 | + ("Non-optional ", "Optional ")["optional" in checks and checks["optional"] is True] |
| 76 | ) |
| 77 | + "[#C0CAF5 bold]" |
| 78 | + str(name) |
| 79 | + "[#F7768E bold]=", |
| 80 | extra_info=get_check_value("explanation", ""), |
| 81 | check_type=eval(get_check_value("type", "False")), # fixme remove eval |
no test coverage detected