-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathparse_result.py
101 lines (89 loc) · 3.53 KB
/
parse_result.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from __future__ import annotations
import enum
import typing as t
import click
import jsonschema
from ..formats import FormatOptions
from ..regex_variants import RegexImplementation, RegexVariantName
from ..transforms import Transform
class SchemaLoadingMode(enum.Enum):
filepath = "filepath"
builtin = "builtin"
metaschema = "metaschema"
class ParseResult:
def __init__(self) -> None:
# primary options: schema + instances
self.schema_mode: SchemaLoadingMode = SchemaLoadingMode.filepath
self.schema_path: str | None = None
self.base_uri: str | None = None
self.instancefiles: tuple[t.IO[bytes], ...] = ()
# cache controls
self.disable_cache: bool = False
self.cache_filename: str | None = None
# filetype detection (JSON, YAML, TOML, etc)
self.default_filetype: str = "json"
# data-transform (for Azure Pipelines and potentially future transforms)
self.data_transform: Transform | None = None
# validation behavioral controls
self.validator_class: type[jsonschema.protocols.Validator] | None = None
self.fill_defaults: bool = False
# regex format options
self.disable_all_formats: bool = False
self.disable_formats: tuple[str, ...] = ()
self.regex_variant: RegexVariantName = RegexVariantName.default
# error and output controls
self.verbosity: int = 1
self.traceback_mode: str = "short"
self.output_format: str = "text"
def set_regex_variant(
self,
variant_opt: t.Literal["python", "nonunicode", "default"] | None,
*,
legacy_opt: t.Literal["python", "nonunicode", "default"] | None = None,
) -> None:
variant_name: t.Literal["python", "nonunicode", "default"] | None = (
variant_opt or legacy_opt
)
if variant_name:
self.regex_variant = RegexVariantName(variant_name)
def set_schema(
self, schemafile: str | None, builtin_schema: str | None, check_metaschema: bool
) -> None:
mutex_arg_count = sum(
1 if x else 0 for x in (schemafile, builtin_schema, check_metaschema)
)
if mutex_arg_count == 0:
raise click.UsageError(
"Either --schemafile, --builtin-schema, or --check-metaschema "
"must be provided"
)
if mutex_arg_count > 1:
raise click.UsageError(
"--schemafile, --builtin-schema, and --check-metaschema "
"are mutually exclusive"
)
if schemafile:
self.schema_mode = SchemaLoadingMode.filepath
self.schema_path = schemafile
elif builtin_schema:
self.schema_mode = SchemaLoadingMode.builtin
self.schema_path = builtin_schema
else:
self.schema_mode = SchemaLoadingMode.metaschema
def set_validator(
self, validator_class: type[jsonschema.protocols.Validator] | None
) -> None:
if validator_class is None:
return
if self.schema_mode != SchemaLoadingMode.filepath:
raise click.UsageError(
"--validator-class can only be used with --schemafile for schema loading"
)
self.validator_class = validator_class
@property
def format_opts(self) -> FormatOptions:
return FormatOptions(
regex_impl=RegexImplementation(self.regex_variant),
enabled=not self.disable_all_formats,
disabled_formats=self.disable_formats,
)