-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathfile.py
33 lines (22 loc) · 949 Bytes
/
file.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
"""OpenAPI spec validator handlers file module."""
import io
import json
from yaml import load
from openapi_spec_validator.handlers.base import BaseHandler
from openapi_spec_validator.handlers.compat import UniqueSchemasLoader
from openapi_spec_validator.handlers.utils import uri_to_path
class FileObjectHandler(BaseHandler):
"""OpenAPI spec validator file-like object handler."""
def __init__(self, loader=UniqueSchemasLoader):
self.loader = loader
def __call__(self, f):
return json.loads(json.dumps(load(f, self.loader)))
class FileHandler(FileObjectHandler):
"""OpenAPI spec validator file path handler."""
def __call__(self, uri):
if isinstance(uri, io.StringIO):
return super(FileHandler, self).__call__(uri)
assert uri.startswith("file")
filepath = uri_to_path(uri)
with open(filepath) as fh:
return super(FileHandler, self).__call__(fh)