22
22
import socket
23
23
import sys
24
24
25
+ try :
26
+ from collections import MutableMapping
27
+ except ImportError :
28
+ from collections .abc import MutableMapping
29
+
25
30
try :
26
31
import requests
27
32
except ImportError :
48
53
49
54
FLOAT_TOLERANCE = 10 ** - 15
50
55
validators = {}
51
- meta_schemas = {}
52
56
53
57
54
58
class _Error (Exception ):
@@ -68,6 +72,41 @@ class RefResolutionError(Exception): pass
68
72
class UnknownType (Exception ): pass
69
73
70
74
75
+ class _URIDict (MutableMapping ):
76
+ """
77
+ Dictionary which uses normalized URIs as keys.
78
+
79
+ """
80
+
81
+ def normalize (self , uri ):
82
+ return urlparse .urlsplit (uri ).geturl ()
83
+
84
+ def __init__ (self , * args , ** kwargs ):
85
+ self .store = dict ()
86
+ self .store .update (* args , ** kwargs )
87
+
88
+ def __getitem__ (self , uri ):
89
+ return self .store [self .normalize (uri )]
90
+
91
+ def __setitem__ (self , uri , value ):
92
+ self .store [self .normalize (uri )] = value
93
+
94
+ def __delitem__ (self , uri ):
95
+ del self .store [self .normalize (uri )]
96
+
97
+ def __iter__ (self ):
98
+ return iter (self .store )
99
+
100
+ def __len__ (self ):
101
+ return len (self .store )
102
+
103
+ def __repr__ (self ):
104
+ return repr (self .store )
105
+
106
+
107
+ meta_schemas = _URIDict ()
108
+
109
+
71
110
def validates (version ):
72
111
"""
73
112
Register the decorated validator for a ``version`` of the specification.
@@ -923,7 +962,7 @@ def __init__(self, base_uri, referrer, store=(), cache_remote=True,
923
962
self .base_uri = base_uri
924
963
self .resolution_scope = base_uri
925
964
self .referrer = referrer
926
- self .store = dict (store , ** _meta_schemas ())
965
+ self .store = _URIDict (store , ** _meta_schemas ())
927
966
self .cache_remote = cache_remote
928
967
self .handlers = dict (handlers )
929
968
@@ -1239,6 +1278,6 @@ def _uniq(container):
1239
1278
1240
1279
def validate (instance , schema , cls = None , * args , ** kwargs ):
1241
1280
if cls is None :
1242
- cls = meta_schemas .get (schema .get ("$schema" ), Draft4Validator )
1281
+ cls = meta_schemas .get (schema .get ("$schema" , "" ), Draft4Validator )
1243
1282
cls .check_schema (schema )
1244
1283
cls (schema , * args , ** kwargs ).validate (instance )
0 commit comments