forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_resources_tool.py
More file actions
85 lines (70 loc) · 2.67 KB
/
generate_resources_tool.py
File metadata and controls
85 lines (70 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
"""Generate C# ResourceUtilities partial class with embedded JS resources.
Usage:
generate_resources_tool.py --output path/to/ResourceUtilities.g.cs \
--input Ident1=path/to/file1.js \
--input Ident2=path/to/file2.js ...
Each identifier becomes a const string in ResourceUtilities class.
The content is emitted as a C# raw string literal using 5-quotes.
"""
import argparse
import os
import sys
from typing import List, Tuple
def parse_args(argv: List[str]) -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--output", required=True)
parser.add_argument("--input", action="append", default=[], help="IDENT=path")
return parser.parse_args(argv)
def parse_input_spec(spec: str) -> Tuple[str, str]:
if "=" not in spec:
raise ValueError(f"Invalid --input value, expected IDENT=path, got: {spec}")
ident, path = spec.split("=", 1)
ident = ident.strip()
path = path.strip()
if not ident:
raise ValueError(f"Empty identifier in --input value: {spec}")
if not path:
raise ValueError(f"Empty path in --input value: {spec}")
return ident, path
def generate(output: str, inputs: List[Tuple[str, str]]) -> None:
props: List[str] = []
for ident, path in inputs:
with open(path, "r", encoding="utf-8") as f:
content = f.read()
# Use a C# raw string literal with five quotes. For a valid raw
# literal, the content must start on a new line and the closing
# quotes must be on their own line as well. We assume the content
# does not contain a sequence of five consecutive double quotes.
#
# Resulting C# will look like:
# """""
# <content>
# """""
literal = '"""""\n' + content + '\n"""""'
props.append(
f" internal const string {ident} = {literal};"
)
lines: List[str] = []
lines.append("// <auto-generated />")
lines.append("namespace OpenQA.Selenium.Internal;")
lines.append("")
lines.append("internal static partial class ResourceUtilities")
lines.append("{")
for p in props:
lines.append(p)
lines.append("}")
lines.append("")
os.makedirs(os.path.dirname(output), exist_ok=True)
with open(output, "w", encoding="utf-8", newline="\n") as f:
f.write("\n".join(lines))
def main(argv: List[str]) -> int:
args = parse_args(argv)
inputs: List[Tuple[str, str]] = []
for spec in args.input:
ident, path = parse_input_spec(spec)
inputs.append((ident, path))
generate(args.output, inputs)
return 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1:]))