|
1 | 1 | import platform
|
2 | 2 | import pytest
|
| 3 | +from functools import wraps |
| 4 | +import inspect |
3 | 5 |
|
4 | 6 | from dash.testing.browser import Browser
|
5 |
| -from preconditions import preconditions |
6 | 7 | from selenium.webdriver.common.action_chains import ActionChains
|
7 | 8 | from selenium.webdriver.common.by import By
|
8 | 9 | from selenium.webdriver.common.keys import Keys
|
9 | 10 | from selenium.webdriver.support import expected_conditions as EC
|
10 | 11 | from selenium.webdriver.support.wait import WebDriverWait
|
11 | 12 |
|
| 13 | + |
| 14 | + |
| 15 | +class PreconditionError(TypeError): |
| 16 | + pass |
| 17 | + |
| 18 | + |
| 19 | +def preconditions(*precs): |
| 20 | + stripped_source = lambda obj: inspect.getsource(obj).strip() |
| 21 | + |
| 22 | + if not precs: |
| 23 | + # This edge case makes ``@preconditions()`` efficiently delegate |
| 24 | + # to the wrapped function, which I anticipate will be useful |
| 25 | + # for stubbing and code consistency in applications: |
| 26 | + def null_decorator(f): |
| 27 | + f.nopre = f # Meet the .nopre interface requirement. |
| 28 | + return f |
| 29 | + |
| 30 | + return null_decorator |
| 31 | + |
| 32 | + precinfo = [] |
| 33 | + for p in precs: |
| 34 | + spec = inspect.getargspec(p) |
| 35 | + if spec.varargs or spec.keywords: |
| 36 | + raise PreconditionError( |
| 37 | + ( |
| 38 | + "Invalid precondition must not accept * nor ** args:\n" + " {!s}\n" |
| 39 | + ).format(stripped_source(p)) |
| 40 | + ) |
| 41 | + |
| 42 | + i = -len(spec.defaults or ()) |
| 43 | + if i == 0: |
| 44 | + appargs, closureargs = spec.args, [] |
| 45 | + else: |
| 46 | + appargs, closureargs = spec.args[:i], spec.args[i:] |
| 47 | + precinfo.append((appargs, closureargs, p)) |
| 48 | + |
| 49 | + def decorate(f): |
| 50 | + fspec = inspect.getargspec(f) |
| 51 | + |
| 52 | + for (appargs, closureargs, p) in precinfo: |
| 53 | + for apparg in appargs: |
| 54 | + if apparg not in fspec.args: |
| 55 | + raise PreconditionError( |
| 56 | + ( |
| 57 | + "Invalid precondition refers to unknown parameter {!r}:\n" |
| 58 | + + " {!s}\n" |
| 59 | + + "Known parameters: {!r}\n" |
| 60 | + ).format(apparg, stripped_source(p), fspec.args) |
| 61 | + ) |
| 62 | + for carg in closureargs: |
| 63 | + if carg in fspec.args: |
| 64 | + raise PreconditionError( |
| 65 | + ( |
| 66 | + "Invalid precondition masks parameter {!r}:\n" |
| 67 | + + " {!s}\n" |
| 68 | + + "Known parameters: {!r}\n" |
| 69 | + ).format(carg, stripped_source(p), fspec.args) |
| 70 | + ) |
| 71 | + |
| 72 | + @wraps(f) |
| 73 | + def g(*a, **kw): |
| 74 | + args = inspect.getcallargs(f, *a, **kw) |
| 75 | + for (appargs, _, p) in precinfo: |
| 76 | + if not p(*[args[aa] for aa in appargs]): |
| 77 | + raise PreconditionError( |
| 78 | + "Precondition failed in call {!r}{}:\n {!s}\n".format( |
| 79 | + g, |
| 80 | + inspect.formatargvalues( |
| 81 | + fspec.args, fspec.varargs, fspec.keywords, args |
| 82 | + ), |
| 83 | + stripped_source(p), |
| 84 | + ) |
| 85 | + ) |
| 86 | + |
| 87 | + return f(*a, **kw) |
| 88 | + |
| 89 | + g.nopre = f |
| 90 | + return g |
| 91 | + |
| 92 | + return decorate |
| 93 | + |
| 94 | + |
12 | 95 | _validate_col = lambda col: (isinstance(col, str) and len(col) > 0) or (
|
13 | 96 | isinstance(col, int) and col >= 0
|
14 | 97 | )
|
|
0 commit comments