seleniumbase 4.32.1__py3-none-any.whl → 4.32.3__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,110 @@
1
+ import warnings as _warnings
2
+ from collections.abc import Mapping as _Mapping, Sequence as _Sequence
3
+ import logging
4
+
5
+ __logger__ = logging.getLogger(__name__)
6
+ __all__ = ["cdict", "ContraDict"]
7
+
8
+
9
+ def cdict(*args, **kwargs):
10
+ """Factory function"""
11
+ return ContraDict(*args, **kwargs)
12
+
13
+
14
+ class ContraDict(dict):
15
+ """
16
+ Directly inherited from dict.
17
+ Accessible by attribute. o.x == o['x']
18
+ This works also for all corner cases.
19
+ Native json.dumps and json.loads work with it.
20
+
21
+ Names like "keys", "update", "values" etc won't overwrite the methods,
22
+ but will just be available using dict lookup notation obj['items']
23
+ instead of obj.items.
24
+
25
+ All key names are converted to snake_case.
26
+ Hyphen's (-), dot's (.) or whitespaces are replaced by underscore (_).
27
+ Autocomplete works even if the objects comes from a list.
28
+ Recursive action. Dict assignments will be converted too.
29
+ """
30
+ __module__ = None
31
+
32
+ def __init__(self, *args, **kwargs):
33
+ super().__init__()
34
+ silent = kwargs.pop("silent", False)
35
+ _ = dict(*args, **kwargs)
36
+
37
+ super().__setattr__("__dict__", self)
38
+ for k, v in _.items():
39
+ _check_key(k, self, False, silent)
40
+ super().__setitem__(k, _wrap(self.__class__, v))
41
+
42
+ def __setitem__(self, key, value):
43
+ super().__setitem__(key, _wrap(self.__class__, value))
44
+
45
+ def __setattr__(self, key, value):
46
+ super().__setitem__(key, _wrap(self.__class__, value))
47
+
48
+ def __getattribute__(self, attribute):
49
+ if attribute in self:
50
+ return self[attribute]
51
+ if not _check_key(attribute, self, True, silent=True):
52
+ return getattr(super(), attribute)
53
+ return object.__getattribute__(self, attribute)
54
+
55
+
56
+ def _wrap(cls, v):
57
+ if isinstance(v, _Mapping):
58
+ v = cls(v)
59
+ elif isinstance(v, _Sequence) and not isinstance(
60
+ v, (str, bytes, bytearray, set, tuple)
61
+ ):
62
+ v = list([_wrap(cls, x) for x in v])
63
+ return v
64
+
65
+
66
+ _warning_names = (
67
+ "items",
68
+ "keys",
69
+ "values",
70
+ "update",
71
+ "clear",
72
+ "copy",
73
+ "fromkeys",
74
+ "get",
75
+ "items",
76
+ "keys",
77
+ "pop",
78
+ "popitem",
79
+ "setdefault",
80
+ "update",
81
+ "values",
82
+ "class",
83
+ )
84
+
85
+ _warning_names_message = """\n\
86
+ While creating a ContraDict object, a key offending key name '{0}'
87
+ has been found, which might behave unexpected.
88
+ You will only be able to look it up using key,
89
+ eg. myobject['{0}']. myobject.{0} will not work with that name."""
90
+
91
+
92
+ def _check_key(
93
+ key: str, mapping: _Mapping, boolean: bool = False, silent=False
94
+ ):
95
+ """Checks `key` and warns if needed.
96
+ :param key:
97
+ :param boolean: return True or False instead of passthrough
98
+ """
99
+ e = None
100
+ if not isinstance(key, (str,)):
101
+ if boolean:
102
+ return True
103
+ return key
104
+ if key.lower() in _warning_names or any(_ in key for _ in ("-", ".")):
105
+ if not silent:
106
+ _warnings.warn(_warning_names_message.format(key))
107
+ e = True
108
+ if not boolean:
109
+ return key
110
+ return not e