selectolax-schema 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- selectolax_schema/__init__.py +54 -0
- selectolax_schema/cli.py +75 -0
- selectolax_schema/core.py +986 -0
- selectolax_schema/types.py +173 -0
- selectolax_schema-0.1.0.dist-info/METADATA +299 -0
- selectolax_schema-0.1.0.dist-info/RECORD +10 -0
- selectolax_schema-0.1.0.dist-info/WHEEL +5 -0
- selectolax_schema-0.1.0.dist-info/entry_points.txt +2 -0
- selectolax_schema-0.1.0.dist-info/licenses/LICENSE +22 -0
- selectolax_schema-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
2
|
+
|
|
3
|
+
from .core import (
|
|
4
|
+
ExtractError,
|
|
5
|
+
ValidationError,
|
|
6
|
+
extract,
|
|
7
|
+
extract_from_file,
|
|
8
|
+
extract_from_yaml,
|
|
9
|
+
schema,
|
|
10
|
+
schema_from_file,
|
|
11
|
+
schema_from_yaml,
|
|
12
|
+
)
|
|
13
|
+
from .types import (
|
|
14
|
+
Clear,
|
|
15
|
+
Field,
|
|
16
|
+
FieldType,
|
|
17
|
+
Options,
|
|
18
|
+
RegexSub,
|
|
19
|
+
ScalarType,
|
|
20
|
+
ArrayType,
|
|
21
|
+
Schema,
|
|
22
|
+
Split,
|
|
23
|
+
TransformStep,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
__version__ = version("selectolax-schema")
|
|
28
|
+
except PackageNotFoundError:
|
|
29
|
+
# Keep source-tree imports usable before the distribution is installed.
|
|
30
|
+
__version__ = "0+unknown"
|
|
31
|
+
|
|
32
|
+
__all__ = [
|
|
33
|
+
"__version__",
|
|
34
|
+
"ExtractError",
|
|
35
|
+
"ValidationError",
|
|
36
|
+
"extract",
|
|
37
|
+
"extract_from_file",
|
|
38
|
+
"extract_from_yaml",
|
|
39
|
+
"schema",
|
|
40
|
+
"schema_from_file",
|
|
41
|
+
"schema_from_yaml",
|
|
42
|
+
# types
|
|
43
|
+
"Schema",
|
|
44
|
+
"Field",
|
|
45
|
+
"FieldType",
|
|
46
|
+
"ScalarType",
|
|
47
|
+
"ArrayType",
|
|
48
|
+
"Options",
|
|
49
|
+
"Clear",
|
|
50
|
+
"RegexSub",
|
|
51
|
+
"Split",
|
|
52
|
+
"TransformStep",
|
|
53
|
+
]
|
|
54
|
+
|
selectolax_schema/cli.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from . import __version__
|
|
8
|
+
from .core import ExtractError, ValidationError, extract_from_file, schema_from_file
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def main(argv: list[str] | None = None) -> int:
|
|
12
|
+
parser = argparse.ArgumentParser(prog="selectolax-schema")
|
|
13
|
+
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
14
|
+
parser.add_argument("--spec", required=True, help="Path to YAML spec file.")
|
|
15
|
+
parser.add_argument(
|
|
16
|
+
"--schema",
|
|
17
|
+
action="store_true",
|
|
18
|
+
help="Emit JSON Schema for the output of the given spec, then exit.",
|
|
19
|
+
)
|
|
20
|
+
parser.add_argument(
|
|
21
|
+
"--schema-out",
|
|
22
|
+
help="Write emitted JSON Schema to a file instead of stdout.",
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
group = parser.add_mutually_exclusive_group(required=False)
|
|
26
|
+
group.add_argument("--html", help="HTML string input.")
|
|
27
|
+
group.add_argument("--html-file", help="Path to an HTML file.")
|
|
28
|
+
|
|
29
|
+
args = parser.parse_args(argv)
|
|
30
|
+
|
|
31
|
+
if args.schema:
|
|
32
|
+
if args.html or args.html_file:
|
|
33
|
+
print("--schema cannot be used with --html/--html-file.", file=sys.stderr)
|
|
34
|
+
return 2
|
|
35
|
+
try:
|
|
36
|
+
s = schema_from_file(args.spec, title="selectolax-schema output")
|
|
37
|
+
except ExtractError as e:
|
|
38
|
+
print(f"Spec error: {e}", file=sys.stderr)
|
|
39
|
+
return 2
|
|
40
|
+
out_fp = open(args.schema_out, "w", encoding="utf-8") if args.schema_out else sys.stdout
|
|
41
|
+
try:
|
|
42
|
+
json.dump(s, out_fp, ensure_ascii=False, indent=2)
|
|
43
|
+
out_fp.write("\n")
|
|
44
|
+
finally:
|
|
45
|
+
if out_fp is not sys.stdout:
|
|
46
|
+
out_fp.close()
|
|
47
|
+
return 0
|
|
48
|
+
|
|
49
|
+
if not args.html and not args.html_file:
|
|
50
|
+
print("One of --html or --html-file is required.", file=sys.stderr)
|
|
51
|
+
return 2
|
|
52
|
+
|
|
53
|
+
if args.html_file:
|
|
54
|
+
with open(args.html_file, "r", encoding="utf-8") as f:
|
|
55
|
+
html = f.read()
|
|
56
|
+
else:
|
|
57
|
+
html = args.html
|
|
58
|
+
|
|
59
|
+
try:
|
|
60
|
+
data = extract_from_file(html, args.spec)
|
|
61
|
+
except ValidationError as e:
|
|
62
|
+
print(f"Validation error: {e}", file=sys.stderr)
|
|
63
|
+
return 3
|
|
64
|
+
except ExtractError as e:
|
|
65
|
+
print(f"Spec error: {e}", file=sys.stderr)
|
|
66
|
+
return 2
|
|
67
|
+
|
|
68
|
+
json.dump(data, sys.stdout, ensure_ascii=False, indent=2)
|
|
69
|
+
sys.stdout.write("\n")
|
|
70
|
+
return 0
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
if __name__ == "__main__":
|
|
74
|
+
raise SystemExit(main())
|
|
75
|
+
|