Skip to content

Commit e80d05a

Browse files
committed
[py] Add bidi.py to define base classes for generated bidi classes
1 parent a14b659 commit e80d05a

File tree

1 file changed

+59
-0
lines changed
  • py/selenium/webdriver/common/bidi

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from dataclasses import dataclass
19+
from dataclasses import fields
20+
from dataclasses import is_dataclass
21+
from typing import get_type_hints
22+
23+
24+
@dataclass
25+
class BidiObject:
26+
def to_json(self):
27+
json = {}
28+
for field in fields(self):
29+
value = getattr(self, field.name)
30+
if value is None:
31+
continue
32+
if is_dataclass(value):
33+
value = value.to_json()
34+
elif isinstance(value, list):
35+
value = [v.to_json() if hasattr(v, "to_json") else v for v in value]
36+
elif isinstance(value, dict):
37+
value = {k: v.to_json() if hasattr(v, "to_json") else v for k, v in value.items()}
38+
key = field.name[:-1] if field.name.endswith("_") else field.name
39+
json[key] = value
40+
return json
41+
42+
@classmethod
43+
def from_json(cls, json):
44+
return cls(**json)
45+
46+
47+
@dataclass
48+
class BidiEvent(BidiObject):
49+
@classmethod
50+
def from_json(cls, json):
51+
params = get_type_hints(cls)["params"].from_json(json)
52+
return cls(params)
53+
54+
55+
@dataclass
56+
class BidiCommand(BidiObject):
57+
def cmd(self):
58+
result = yield self.to_json()
59+
return result

0 commit comments

Comments
 (0)