Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/common/devtools/pdl.py
2884 views
1
# Copyright 2018 The Chromium Authors. All rights reserved.
2
# Use of this source code is governed by a BSD-style license that can be
3
# found in the LICENSE file.
4
5
from __future__ import print_function
6
import collections
7
import json
8
import re
9
import sys
10
11
description = ""
12
13
14
primitiveTypes = [
15
"integer",
16
"number",
17
"boolean",
18
"string",
19
"object",
20
"any",
21
"array",
22
"binary",
23
]
24
25
26
def assignType(item, type, is_array=False, map_binary_to_string=False):
27
if is_array:
28
item["type"] = "array"
29
item["items"] = collections.OrderedDict()
30
assignType(item["items"], type, False, map_binary_to_string)
31
return
32
33
if type == "enum":
34
type = "string"
35
if map_binary_to_string and type == "binary":
36
type = "string"
37
if type in primitiveTypes:
38
item["type"] = type
39
else:
40
item["$ref"] = type
41
42
43
def createItem(d, experimental, deprecated, name=None):
44
result = collections.OrderedDict(d)
45
if name:
46
result["name"] = name
47
global description
48
if description:
49
result["description"] = description.strip()
50
if experimental:
51
result["experimental"] = True
52
if deprecated:
53
result["deprecated"] = True
54
return result
55
56
57
def parse(data, file_name, map_binary_to_string=False):
58
protocol = collections.OrderedDict()
59
protocol["version"] = collections.OrderedDict()
60
protocol["domains"] = []
61
domain = None
62
item = None
63
subitems = None
64
nukeDescription = False
65
global description
66
lines = data.split("\n")
67
for i in range(0, len(lines)):
68
if nukeDescription:
69
description = ""
70
nukeDescription = False
71
line = lines[i]
72
trimLine = line.strip()
73
74
if trimLine.startswith("#"):
75
if len(description):
76
description += "\n"
77
description += trimLine[2:]
78
continue
79
else:
80
nukeDescription = True
81
82
if len(trimLine) == 0:
83
continue
84
85
match = re.compile(r"^(experimental )?(deprecated )?domain (.*)").match(line)
86
if match:
87
domain = createItem(
88
{"domain": match.group(3)}, match.group(1), match.group(2)
89
)
90
protocol["domains"].append(domain)
91
continue
92
93
match = re.compile(r"^ depends on ([^\s]+)").match(line)
94
if match:
95
if "dependencies" not in domain:
96
domain["dependencies"] = []
97
domain["dependencies"].append(match.group(1))
98
continue
99
100
match = re.compile(
101
r"^ (experimental )?(deprecated )?type (.*) "
102
r"extends (array of )?([^\s]+)"
103
).match(line)
104
if match:
105
if "types" not in domain:
106
domain["types"] = []
107
item = createItem({"id": match.group(3)}, match.group(1), match.group(2))
108
assignType(item, match.group(5), match.group(4), map_binary_to_string)
109
domain["types"].append(item)
110
continue
111
112
match = re.compile(
113
r"^ (experimental )?(deprecated )?(command|event) (.*)"
114
).match(line)
115
if match:
116
list = []
117
if match.group(3) == "command":
118
if "commands" in domain:
119
list = domain["commands"]
120
else:
121
list = domain["commands"] = []
122
else:
123
if "events" in domain:
124
list = domain["events"]
125
else:
126
list = domain["events"] = []
127
128
item = createItem({}, match.group(1), match.group(2), match.group(4))
129
list.append(item)
130
continue
131
132
match = re.compile(
133
r"^ (experimental )?(deprecated )?(optional )?"
134
r"(array of )?([^\s]+) ([^\s]+)"
135
).match(line)
136
if match:
137
param = createItem({}, match.group(1), match.group(2), match.group(6))
138
if match.group(3):
139
param["optional"] = True
140
assignType(param, match.group(5), match.group(4), map_binary_to_string)
141
if match.group(5) == "enum":
142
enumliterals = param["enum"] = []
143
subitems.append(param)
144
continue
145
146
match = re.compile(r"^ (parameters|returns|properties)").match(line)
147
if match:
148
subitems = item[match.group(1)] = []
149
continue
150
151
match = re.compile(r"^ enum").match(line)
152
if match:
153
enumliterals = item["enum"] = []
154
continue
155
156
match = re.compile(r"^version").match(line)
157
if match:
158
continue
159
160
match = re.compile(r"^ major (\d+)").match(line)
161
if match:
162
protocol["version"]["major"] = match.group(1)
163
continue
164
165
match = re.compile(r"^ minor (\d+)").match(line)
166
if match:
167
protocol["version"]["minor"] = match.group(1)
168
continue
169
170
match = re.compile(r"^ redirect ([^\s]+)").match(line)
171
if match:
172
item["redirect"] = match.group(1)
173
continue
174
175
match = re.compile(r"^ ( )?[^\s]+$").match(line)
176
if match:
177
# enum literal
178
enumliterals.append(trimLine)
179
continue
180
181
print("Error in %s:%s, illegal token: \t%s" % (file_name, i, line))
182
sys.exit(1)
183
return protocol
184
185
186
def loads(data, file_name, map_binary_to_string=False):
187
if file_name.endswith(".pdl"):
188
return parse(data, file_name, map_binary_to_string)
189
return json.loads(data)
190
191