1
+ from argparse import ArgumentParser
1
2
import logging
2
- import argparse
3
3
import sys
4
+ from typing import Optional
5
+ from typing import Sequence
4
6
5
7
from jsonschema .exceptions import best_match
8
+ from jsonschema .exceptions import ValidationError
6
9
7
- from openapi_spec_validator import (
8
- openapi_v2_spec_validator ,
9
- openapi_v30_spec_validator ,
10
- openapi_v31_spec_validator ,
11
- )
12
- from openapi_spec_validator .validation .exceptions import ValidationError
13
- from openapi_spec_validator .readers import read_from_stdin , read_from_filename
10
+ from openapi_spec_validator import openapi_v2_spec_validator
11
+ from openapi_spec_validator import openapi_v30_spec_validator
12
+ from openapi_spec_validator import openapi_v31_spec_validator
13
+ from openapi_spec_validator .readers import read_from_filename
14
+ from openapi_spec_validator .readers import read_from_stdin
14
15
15
16
logger = logging .getLogger (__name__ )
16
17
logging .basicConfig (
17
- format = ' %(asctime)s %(levelname)s %(name)s %(message)s' ,
18
- level = logging .WARNING
18
+ format = " %(asctime)s %(levelname)s %(name)s %(message)s" ,
19
+ level = logging .WARNING ,
19
20
)
20
21
21
22
22
- def print_validationerror (exc , errors = "best-match" ):
23
+ def print_validationerror (exc : ValidationError , errors : str = "best-match" ) -> None :
23
24
print ("# Validation Error\n " )
24
25
print (exc )
25
26
if exc .cause :
@@ -35,14 +36,14 @@ def print_validationerror(exc, errors="best-match"):
35
36
print ("## " + str (best_match (exc .context )))
36
37
if len (exc .context ) > 1 :
37
38
print (
38
- "\n ({} more subschemas errors," . format ( len (exc .context ) - 1 ) ,
39
+ f "\n ({ len (exc .context ) - 1 } more subschemas errors," ,
39
40
"use --errors=all to see them.)" ,
40
41
)
41
42
42
43
43
- def main (args = None ):
44
- parser = argparse . ArgumentParser ()
45
- parser .add_argument (' filename' , help = "Absolute or relative path to file" )
44
+ def main (args : Optional [ Sequence [ str ]] = None ) -> None :
45
+ parser = ArgumentParser ()
46
+ parser .add_argument (" filename" , help = "Absolute or relative path to file" )
46
47
parser .add_argument (
47
48
"--errors" ,
48
49
choices = ("best-match" , "all" ),
@@ -51,46 +52,46 @@ def main(args=None):
51
52
"""use "all" to get all subschema errors.""" ,
52
53
)
53
54
parser .add_argument (
54
- ' --schema' ,
55
+ " --schema" ,
55
56
help = "OpenAPI schema (default: 3.1.0)" ,
56
57
type = str ,
57
- choices = [' 2.0' , ' 3.0.0' , ' 3.1.0' ],
58
- default = ' 3.1.0'
58
+ choices = [" 2.0" , " 3.0.0" , " 3.1.0" ],
59
+ default = " 3.1.0" ,
59
60
)
60
- args = parser .parse_args (args )
61
+ args_parsed = parser .parse_args (args )
61
62
62
63
# choose source
63
64
reader = read_from_filename
64
- if args .filename in ['-' , '/-' ]:
65
+ if args_parsed .filename in ["-" , "/-" ]:
65
66
reader = read_from_stdin
66
67
67
68
# read source
68
69
try :
69
- spec , spec_url = reader (args .filename )
70
+ spec , spec_url = reader (args_parsed .filename )
70
71
except Exception as exc :
71
72
print (exc )
72
73
sys .exit (1 )
73
74
74
75
# choose the validator
75
76
validators = {
76
- ' 2.0' : openapi_v2_spec_validator ,
77
- ' 3.0.0' : openapi_v30_spec_validator ,
78
- ' 3.1.0' : openapi_v31_spec_validator ,
77
+ " 2.0" : openapi_v2_spec_validator ,
78
+ " 3.0.0" : openapi_v30_spec_validator ,
79
+ " 3.1.0" : openapi_v31_spec_validator ,
79
80
}
80
- validator = validators [args .schema ]
81
+ validator = validators [args_parsed .schema ]
81
82
82
83
# validate
83
84
try :
84
85
validator .validate (spec , spec_url = spec_url )
85
86
except ValidationError as exc :
86
- print_validationerror (exc , args .errors )
87
+ print_validationerror (exc , args_parsed .errors )
87
88
sys .exit (1 )
88
89
except Exception as exc :
89
90
print (exc )
90
91
sys .exit (2 )
91
92
else :
92
- print ('OK' )
93
+ print ("OK" )
93
94
94
95
95
- if __name__ == ' __main__' :
96
+ if __name__ == " __main__" :
96
97
main ()
0 commit comments