@@ -23,11 +23,6 @@ def resolve_path(schema, fragment):
23
23
Return definition from path.
24
24
25
25
Path is unescaped according https://tools.ietf.org/html/rfc6901
26
-
27
- :argument schema: the referrant schema document
28
- :argument str fragment: a URI fragment to resolve within it
29
- :returns: the retrieved schema definition
30
-
31
26
"""
32
27
fragment = fragment .lstrip ('/' )
33
28
parts = unquote (fragment ).split ('/' ) if fragment else []
@@ -59,11 +54,6 @@ def resolve_remote(uri, handlers):
59
54
For unknown schemes urlib is used with UTF-8 encoding.
60
55
61
56
.. _Requests: http://pypi.python.org/pypi/requests/
62
-
63
- :argument str uri: the URI to resolve
64
- :argument dict handlers: the URI resolver functions for each scheme
65
- :returns: the retrieved schema document
66
-
67
57
"""
68
58
scheme = urlparse .urlsplit (uri ).scheme
69
59
if scheme in handlers :
@@ -78,18 +68,13 @@ def resolve_remote(uri, handlers):
78
68
class RefResolver :
79
69
"""
80
70
Resolve JSON References.
81
-
82
- :argument str base_uri: URI of the referring document
83
- :argument schema: the actual referring schema document
84
- :argument dict store: a mapping from URIs to documents to cache
85
- :argument bool cache: whether remote refs should be cached after
86
- first resolution
87
- :argument dict handlers: a mapping from URI schemes to functions that
88
- should be used to retrieve them
89
71
"""
90
72
91
73
# pylint: disable=dangerous-default-value,too-many-arguments
92
74
def __init__ (self , base_uri , schema , store = {}, cache = True , handlers = {}):
75
+ """
76
+ `base_uri` is URI of the referring document from the `schema`.
77
+ """
93
78
self .base_uri = base_uri
94
79
self .resolution_scope = base_uri
95
80
self .schema = schema
@@ -102,19 +87,19 @@ def __init__(self, base_uri, schema, store={}, cache=True, handlers={}):
102
87
def from_schema (cls , schema , handlers = {}, ** kwargs ):
103
88
"""
104
89
Construct a resolver from a JSON schema object.
105
-
106
- :argument schema schema: the referring schema
107
- :rtype: :class:`RefResolver`
108
90
"""
109
91
return cls (
110
- schema .get ('id' , '' ) if isinstance (schema , dict ) else '' ,
92
+ schema .get ('$ id' , schema . get ( 'id' , '' ) ) if isinstance (schema , dict ) else '' ,
111
93
schema ,
112
94
handlers = handlers ,
113
95
** kwargs
114
96
)
115
97
116
98
@contextlib .contextmanager
117
- def in_scope (self , scope ):
99
+ def in_scope (self , scope : str ):
100
+ """
101
+ Context manager to handle current scope.
102
+ """
118
103
old_scope = self .resolution_scope
119
104
self .resolution_scope = urlparse .urljoin (old_scope , scope )
120
105
try :
@@ -123,12 +108,10 @@ def in_scope(self, scope):
123
108
self .resolution_scope = old_scope
124
109
125
110
@contextlib .contextmanager
126
- def resolving (self , ref ):
111
+ def resolving (self , ref : str ):
127
112
"""
128
113
Context manager which resolves a JSON ``ref`` and enters the
129
114
resolution scope of this ref.
130
-
131
- :argument str ref: reference to resolve
132
115
"""
133
116
new_uri = urlparse .urljoin (self .resolution_scope , ref )
134
117
uri , fragment = urlparse .urldefrag (new_uri )
@@ -154,6 +137,9 @@ def get_uri(self):
154
137
return normalize (self .resolution_scope )
155
138
156
139
def get_scope_name (self ):
140
+ """
141
+ Get current scope and return it as a valid function name.
142
+ """
157
143
name = 'validate_' + unquote (self .resolution_scope ).replace ('~1' , '_' ).replace ('~0' , '_' )
158
144
name = re .sub (r'[:/#\.\-\%]' , '_' , name )
159
145
name = name .lower ().rstrip ('_' )
0 commit comments