Skip to content

Commit 9874d11

Browse files
committed
use same import pattern for six.urllib accross the codebase
- also, re-ogranise six lib imports
1 parent d23c804 commit 9874d11

File tree

7 files changed

+41
-43
lines changed

7 files changed

+41
-43
lines changed

schema_salad/jsonld_context.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import collections
33
import shutil
44
import json
5-
import ruamel.yaml as yaml
5+
66
import six
7-
# import urlparse
8-
from six.moves.urllib import parse
7+
from six.moves import urllib
8+
9+
import ruamel.yaml as yaml
910
try:
1011
from ruamel.yaml import CSafeLoader as SafeLoader
1112
except ImportError:
@@ -37,7 +38,7 @@ def pred(datatype, # type: Dict[str, Union[Dict, str]]
3738
namespaces # type: Dict[str, rdflib.namespace.Namespace]
3839
):
3940
# type: (...) -> Union[Dict, Text]
40-
split = parse.urlsplit(name)
41+
split = urllib.parse.urlsplit(name)
4142

4243
vee = None # type: Optional[Union[str, Text]]
4344

@@ -105,7 +106,7 @@ def process_type(t, # type: Dict[str, Any]
105106
classnode = URIRef(recordname)
106107
g.add((classnode, RDF.type, RDFS.Class))
107108

108-
split = parse.urlsplit(recordname)
109+
split = urllib.parse.urlsplit(recordname)
109110
predicate = recordname
110111
if t.get("inVocab", True):
111112
if split.scheme:
@@ -221,7 +222,7 @@ def makerdf(workflow, # type: Union[str, Text]
221222
url = v
222223
if url == "@id":
223224
idfields.append(k)
224-
doc_url, frg = parse.urldefrag(url)
225+
doc_url, frg = urllib.parse.urldefrag(url)
225226
if "/" in frg:
226227
p = frg.split("/")[0]
227228
prefixes[p] = u"%s#%s/" % (doc_url, p)

schema_salad/main.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
import traceback
77
import json
88
import os
9-
# import urlparse
10-
from six.moves.urllib import parse
119

10+
from six.moves import urllib
1211

1312
import pkg_resources # part of setuptools
1413

@@ -112,7 +111,7 @@ def main(argsl=None): # type: (List[str]) -> int
112111
# Load schema document and resolve refs
113112

114113
schema_uri = args.schema
115-
if not parse.urlparse(schema_uri)[0]:
114+
if not urllib.parse.urlparse(schema_uri)[0]:
116115
schema_uri = "file://" + os.path.abspath(schema_uri)
117116
schema_raw_doc = metaschema_loader.fetch(schema_uri)
118117

@@ -209,7 +208,7 @@ def main(argsl=None): # type: (List[str]) -> int
209208
# Load target document and resolve refs
210209
try:
211210
uri = args.document
212-
if not parse.urlparse(uri)[0]:
211+
if not urllib.parse.urlparse(uri)[0]:
213212
doc = "file://" + os.path.abspath(uri)
214213
document, doc_metadata = document_loader.resolve_ref(uri)
215214
except (validate.ValidationException, RuntimeError) as e:

schema_salad/makedoc.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
import copy
88
import re
99
import sys
10-
1110
import logging
1211

1312
from . import schema
14-
from schema_salad.utils import add_dictlist, aslist
13+
from .utils import add_dictlist, aslist
1514

1615
import six
1716
from six.moves import range
18-
from six.moves.urllib import parse
17+
from six.moves import urllib
1918
from six import StringIO
2019

2120
from typing import cast, Any, Dict, IO, List, Optional, Set, Text, Union
@@ -42,7 +41,7 @@ def has_types(items): # type: (Any) -> List[basestring]
4241

4342

4443
def linkto(item): # type: (Text) -> Text
45-
_, frg = parse.urldefrag(item)
44+
_, frg = urllib.parse.urldefrag(item)
4645
return "[%s](#%s)" % (frg, to_id(frg))
4746

4847

@@ -210,8 +209,8 @@ def __init__(self, toc, j, renderlist, redirects, primitiveType):
210209
if tp not in self.uses:
211210
self.uses[tp] = []
212211
if (t["name"], f["name"]) not in self.uses[tp]:
213-
_, frg1 = parse.urldefrag(t["name"])
214-
_, frg2 = parse.urldefrag(f["name"])
212+
_, frg1 = urllib.parse.urldefrag(t["name"])
213+
_, frg2 = urllib.parse.urldefrag(f["name"])
215214
self.uses[tp].append((frg1, frg2))
216215
if tp not in basicTypes and tp not in self.record_refs[t["name"]]:
217216
self.record_refs[t["name"]].append(tp)
@@ -272,7 +271,7 @@ def typefmt(self,
272271
elif str(tp) in basicTypes:
273272
return """<a href="%s">%s</a>""" % (self.primitiveType, schema.avro_name(str(tp)))
274273
else:
275-
_, frg = parse.urldefrag(tp)
274+
_, frg = urllib.parse.urldefrag(tp)
276275
if frg is not '':
277276
tp = frg
278277
return """<a href="#%s">%s</a>""" % (to_id(tp), tp)
@@ -331,7 +330,7 @@ def extendsfrom(item, ex):
331330
lines.append(l)
332331
f["doc"] = "\n".join(lines)
333332

334-
_, frg = parse.urldefrag(f["name"])
333+
_, frg = urllib.parse.urldefrag(f["name"])
335334
num = self.toc.add_entry(depth, frg)
336335
doc = "%s %s %s\n" % (("#" * depth), num, frg)
337336
else:

schema_salad/ref_resolver.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import six
1010
from six.moves import range
11-
from six.moves.urllib import parse
1211
from six.moves import urllib
1312
from six import StringIO
1413

@@ -57,7 +56,7 @@ def file_uri(path, split_frag=False): # type: (str, bool) -> str
5756
return "file://%s%s" % (urlpath, frag)
5857

5958
def uri_file_path(url): # type: (str) -> str
60-
split = parse.urlsplit(url)
59+
split = urllib.parse.urlsplit(url)
6160
if split.scheme == "file":
6261
return urllib.request.url2pathname(
6362
str(split.path)) + ("#" + urllib.parse.unquote(str(split.fragment))
@@ -129,7 +128,7 @@ def fetch_text(self, url):
129128
if url in self.cache:
130129
return self.cache[url]
131130

132-
split = parse.urlsplit(url)
131+
split = urllib.parse.urlsplit(url)
133132
scheme, path = split.scheme, split.path
134133

135134
if scheme in [u'http', u'https'] and self.session is not None:
@@ -159,7 +158,7 @@ def check_exists(self, url): # type: (Text) -> bool
159158
if url in self.cache:
160159
return True
161160

162-
split = parse.urlsplit(url)
161+
split = urllib.parse.urlsplit(url)
163162
scheme, path = split.scheme, split.path
164163

165164
if scheme in [u'http', u'https'] and self.session is not None:
@@ -175,7 +174,7 @@ def check_exists(self, url): # type: (Text) -> bool
175174
raise ValueError('Unsupported scheme in url: %s' % url)
176175

177176
def urljoin(self, base_url, url): # type: (Text, Text) -> Text
178-
return parse.urljoin(base_url, url)
177+
return urllib.parse.urljoin(base_url, url)
179178

180179
class Loader(object):
181180
def __init__(self,
@@ -190,7 +189,7 @@ def __init__(self,
190189
):
191190
# type: (...) -> None
192191

193-
normalize = lambda url: parse.urlsplit(url).geturl()
192+
normalize = lambda url: urllib.parse.urlsplit(url).geturl()
194193
if idx is not None:
195194
self.idx = idx
196195
else:
@@ -276,20 +275,20 @@ def expand_url(self,
276275
if prefix in self.vocab:
277276
url = self.vocab[prefix] + url[len(prefix) + 1:]
278277

279-
split = parse.urlsplit(url)
278+
split = urllib.parse.urlsplit(url)
280279

281280
if (bool(split.scheme) or url.startswith(u"$(")
282281
or url.startswith(u"${")):
283282
pass
284283
elif scoped_id and not bool(split.fragment):
285-
splitbase = parse.urlsplit(base_url)
284+
splitbase = urllib.parse.urlsplit(base_url)
286285
frg = u""
287286
if bool(splitbase.fragment):
288287
frg = splitbase.fragment + u"/" + split.path
289288
else:
290289
frg = split.path
291290
pt = splitbase.path if splitbase.path != '' else "/"
292-
url = parse.urlunsplit(
291+
url = urllib.parse.urlunsplit(
293292
(splitbase.scheme, splitbase.netloc, pt, splitbase.query, frg))
294293
elif scoped_ref is not None and not split.fragment:
295294
pass
@@ -496,7 +495,7 @@ def resolve_ref(self,
496495
doc_url = url
497496
else:
498497
# Load structured document
499-
doc_url, frg = parse.urldefrag(url)
498+
doc_url, frg = urllib.parse.urldefrag(url)
500499
if doc_url in self.idx and (not mixin):
501500
# If the base document is in the index, it was already loaded,
502501
# so if we didn't find the reference earlier then it must not
@@ -872,7 +871,7 @@ def fetch(self, url, inject_ids=True): # type: (Text, bool) -> Any
872871

873872
def validate_scoped(self, field, link, docid):
874873
# type: (Text, Text, Text) -> Text
875-
split = parse.urlsplit(docid)
874+
split = urllib.parse.urlsplit(docid)
876875
sp = split.fragment.split(u"/")
877876
n = self.scoped_ref_fields[field]
878877
while n > 0 and len(sp) > 0:
@@ -881,7 +880,7 @@ def validate_scoped(self, field, link, docid):
881880
tried = []
882881
while True:
883882
sp.append(link)
884-
url = parse.urlunsplit((
883+
url = urllib.parse.urlunsplit((
885884
split.scheme, split.netloc, split.path, split.query,
886885
u"/".join(sp)))
887886
tried.append(url)

schema_salad/schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import avro.schema
1010
from . import validate
1111
import json
12+
import os
1213

13-
from six.moves.urllib import parse
1414
import six
15-
import os
15+
from six.moves import urllib
1616

1717
if six.PY3:
1818
AvroSchemaFromJSONData = avro.schema.SchemaFromJSONData
@@ -383,7 +383,7 @@ def replace_type(items, spec, loader, found):
383383

384384

385385
def avro_name(url): # type: (AnyStr) -> AnyStr
386-
doc_url, frg = parse.urldefrag(url)
386+
doc_url, frg = urllib.parse.urldefrag(url)
387387
if frg != '':
388388
if '/' in frg:
389389
return frg[frg.rindex('/') + 1:]

schema_salad/tests/test_fetch.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import ruamel.yaml as yaml
1010
import json
1111
import os
12-
# import urlparse
13-
from six.moves.urllib import parse
12+
13+
from six.moves import urllib
1414

1515
class TestFetcher(unittest.TestCase):
1616
def test_fetcher(self):
@@ -33,14 +33,14 @@ def check_exists(self, url): # type: (unicode) -> bool
3333
return False
3434

3535
def urljoin(self, base, url):
36-
urlsp = parse.urlsplit(url)
36+
urlsp = urllib.parse.urlsplit(url)
3737
if urlsp.scheme:
3838
return url
39-
basesp = parse.urlsplit(base)
39+
basesp = urllib.parse.urlsplit(base)
4040

4141
if basesp.scheme == "keep":
4242
return base + "/" + url
43-
return parse.urljoin(base, url)
43+
return urllib.parse.urljoin(base, url)
4444

4545
loader = schema_salad.ref_resolver.Loader({}, fetcher_constructor=TestFetcher)
4646
self.assertEqual({"hello": "foo"}, loader.resolve_ref("foo.txt")[0])

schema_salad/validate.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import avro.schema
44
from avro.schema import Schema
55
import sys
6-
# import urlparse
7-
from six.moves.urllib import parse
86
import re
97
import logging
108

11-
from typing import Any, List, Set, Union, Text
12-
from .sourceline import SourceLine, lineno_re, bullets, indent
139
import six
10+
from six.moves import urllib
1411
from six.moves import range
1512

13+
from typing import Any, List, Set, Union, Text
14+
from .sourceline import SourceLine, lineno_re, bullets, indent
15+
1616
_logger = logging.getLogger("salad")
1717

1818
class ValidationException(Exception):
@@ -301,7 +301,7 @@ def validate_ex(expected_schema, # type: Schema
301301
if (d not in identifiers and strict) and (
302302
d not in foreign_properties and strict_foreign_properties) and not raise_ex:
303303
return False
304-
split = parse.urlsplit(d)
304+
split = urllib.parse.urlsplit(d)
305305
if split.scheme:
306306
err = sl.makeError(u"unrecognized extension field `%s`%s."
307307
" Did you include "

0 commit comments

Comments
 (0)