Skip to content

Commit 3080c69

Browse files
committed
Apply Python3 modernize transforms
Main changes: - use of __future__ - using six lib to: - deal with checking unicode - dict operations - other API changes
1 parent caec629 commit 3080c69

File tree

9 files changed

+93
-74
lines changed

9 files changed

+93
-74
lines changed

schema_salad/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import absolute_import
12
import logging
23
import sys
34
import typing

schema_salad/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import absolute_import
12
from . import main
23
import sys
34
import typing

schema_salad/jsonld_context.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from __future__ import absolute_import
12
import collections
23
import shutil
34
import json
45
import ruamel.yaml as yaml
6+
import six
57
try:
68
from ruamel.yaml import CSafeLoader as SafeLoader
79
except ImportError:
@@ -40,10 +42,10 @@ def pred(datatype, # type: Dict[str, Union[Dict, str]]
4042

4143
if split.scheme != '':
4244
vee = name
43-
(ns, ln) = rdflib.namespace.split_uri(unicode(vee))
45+
(ns, ln) = rdflib.namespace.split_uri(six.text_type(vee))
4446
name = ln
4547
if ns[0:-1] in namespaces:
46-
vee = unicode(namespaces[ns[0:-1]][ln])
48+
vee = six.text_type(namespaces[ns[0:-1]][ln])
4749
_logger.debug("name, v %s %s", name, vee)
4850

4951
v = None # type: Optional[Dict]
@@ -106,7 +108,7 @@ def process_type(t, # type: Dict[str, Any]
106108
predicate = recordname
107109
if t.get("inVocab", True):
108110
if split.scheme:
109-
(ns, ln) = rdflib.namespace.split_uri(unicode(recordname))
111+
(ns, ln) = rdflib.namespace.split_uri(six.text_type(recordname))
110112
predicate = recordname
111113
recordname = ln
112114
else:
@@ -131,13 +133,13 @@ def process_type(t, # type: Dict[str, Any]
131133
v = pred(t, i, fieldname, context, defaultPrefix,
132134
namespaces) # type: Union[Dict[Any, Any], unicode, None]
133135

134-
if isinstance(v, basestring):
136+
if isinstance(v, six.string_types):
135137
v = v if v[0] != "@" else None
136138
elif v is not None:
137139
v = v["_@id"] if v.get("_@id", "@")[0] != "@" else None
138140

139141
if bool(v):
140-
(ns, ln) = rdflib.namespace.split_uri(unicode(v))
142+
(ns, ln) = rdflib.namespace.split_uri(six.text_type(v))
141143
if ns[0:-1] in namespaces:
142144
propnode = namespaces[ns[0:-1]][ln]
143145
else:
@@ -211,7 +213,7 @@ def makerdf(workflow, # type: Union[str, unicode]
211213
# type: (...) -> Graph
212214
prefixes = {}
213215
idfields = []
214-
for k, v in ctx.iteritems():
216+
for k, v in six.iteritems(ctx):
215217
if isinstance(v, dict):
216218
url = v["@id"]
217219
else:
@@ -242,7 +244,7 @@ def makerdf(workflow, # type: Union[str, unicode]
242244
for sub, pred, obj in g.triples((None, URIRef("@id"), None)):
243245
g.remove((sub, pred, obj))
244246

245-
for k2, v2 in prefixes.iteritems():
247+
for k2, v2 in six.iteritems(prefixes):
246248
g.namespace_manager.bind(k2, v2)
247249

248250
return g

schema_salad/main.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import print_function
2+
from __future__ import absolute_import
23
import argparse
34
import logging
45
import sys
@@ -20,6 +21,7 @@
2021
from . import validate
2122
from .sourceline import strip_dup_lineno
2223
from .ref_resolver import Loader
24+
import six
2325

2426
_logger = logging.getLogger("salad")
2527

@@ -118,8 +120,8 @@ def main(argsl=None): # type: (List[str]) -> int
118120
except (validate.ValidationException) as e:
119121
_logger.error("Schema `%s` failed link checking:\n%s",
120122
args.schema, e, exc_info=(True if args.debug else False))
121-
_logger.debug("Index is %s", metaschema_loader.idx.keys())
122-
_logger.debug("Vocabulary is %s", metaschema_loader.vocab.keys())
123+
_logger.debug("Index is %s", list(metaschema_loader.idx.keys()))
124+
_logger.debug("Vocabulary is %s", list(metaschema_loader.vocab.keys()))
123125
return 1
124126
except (RuntimeError) as e:
125127
_logger.error("Schema `%s` read error:\n%s",
@@ -132,7 +134,7 @@ def main(argsl=None): # type: (List[str]) -> int
132134
return 0
133135

134136
if not args.document and args.print_index:
135-
print(json.dumps(metaschema_loader.idx.keys(), indent=4))
137+
print(json.dumps(list(metaschema_loader.idx.keys()), indent=4))
136138
return 0
137139

138140
# Validate the schema document against the metaschema
@@ -210,7 +212,7 @@ def main(argsl=None): # type: (List[str]) -> int
210212
document, doc_metadata = document_loader.resolve_ref(uri)
211213
except (validate.ValidationException, RuntimeError) as e:
212214
_logger.error("Document `%s` failed validation:\n%s",
213-
args.document, strip_dup_lineno(unicode(e)), exc_info=args.debug)
215+
args.document, strip_dup_lineno(six.text_type(e)), exc_info=args.debug)
214216
return 1
215217

216218
# Optionally print the document after ref resolution
@@ -219,7 +221,7 @@ def main(argsl=None): # type: (List[str]) -> int
219221
return 0
220222

221223
if args.print_index:
222-
print(json.dumps(document_loader.idx.keys(), indent=4))
224+
print(json.dumps(list(document_loader.idx.keys()), indent=4))
223225
return 0
224226

225227
# Validate the schema document against the metaschema

schema_salad/makedoc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import absolute_import
12
import mistune
23
from . import schema
34
import json
@@ -12,6 +13,8 @@
1213
import re
1314
import argparse
1415
from typing import cast, Any, Dict, IO, List, Optional, Set, Text, Union
16+
import six
17+
from six.moves import range
1518

1619
_logger = logging.getLogger("salad")
1720

@@ -29,7 +32,7 @@ def has_types(items): # type: (Any) -> List[basestring]
2932
for i in items:
3033
r.extend(has_types(i))
3134
return r
32-
if isinstance(items, basestring):
35+
if isinstance(items, six.string_types):
3336
return [items]
3437
return []
3538

0 commit comments

Comments
 (0)