Skip to content

Commit fa93acc

Browse files
authored
Merge pull request #109 from manu-chroma/modernize
Python3 compatible codebase !
2 parents caec629 + 52c054e commit fa93acc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+836
-1896
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22
.eggs
33
*.egg-info/
44
*pyc
5+
6+
build/
7+
dist/
8+
ruamel
9+
typeshed/2and3/ruamel/yaml
10+
# virtualenv
11+
venv/
12+
.cache/

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ sudo: false
22
language: python
33
python:
44
- 2.7
5+
- 3.3
6+
- 3.4
57
- 3.5
8+
- 3.6
69
os:
710
- linux
811

Makefile

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,20 @@ list-author-emails:
165165
@echo 'name, E-Mail Address'
166166
@git log --format='%aN,%aE' | sort -u | grep -v 'root'
167167

168-
mypy: ${PYSOURCES}
169-
rm -Rf typeshed/2.7/ruamel/yaml
168+
mypy2: ${PYSOURCES}
169+
rm -Rf typeshed/2and3/ruamel/yaml
170170
ln -s $(shell python -c 'from __future__ import print_function; import ruamel.yaml; import os.path; print(os.path.dirname(ruamel.yaml.__file__))') \
171-
typeshed/2.7/ruamel/
172-
MYPYPATH=typeshed/2.7 mypy --py2 --disallow-untyped-calls \
173-
--fast-parser --warn-redundant-casts --warn-unused-ignores \
171+
typeshed/2and3/ruamel/
172+
MYPYPATH=$MYPYPATH:typeshed/2.7:typeshed/2and3 mypy --py2 --disallow-untyped-calls \
173+
--warn-redundant-casts --warn-unused-ignores \
174+
schema_salad
175+
176+
mypy3: ${PYSOURCES}
177+
rm -Rf typeshed/2and3/ruamel/yaml
178+
ln -s $(shell python -c 'from __future__ import print_function; import ruamel.yaml; import os.path; print(os.path.dirname(ruamel.yaml.__file__))') \
179+
typeshed/2and3/ruamel/
180+
MYPYPATH=$MYPYPATH:typeshed/3:typeshed/2and3 mypy --disallow-untyped-calls \
181+
--warn-redundant-casts \
174182
schema_salad
175183

176184
jenkins:

mypy.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[mypy-ruamel.*]
2+
ignore_errors = True

mypy_requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
mypy==0.470 ; python_version>="3"
2-
typed-ast==0.6.3 ; python_version>="3"
1+
mypy==0.511 ; python_version>="3"

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
typing==3.5.2.2 ; python_version>="2.7"
1+
typing==3.5.3
22
avro-python3 ; python_version>="3"
33
avro==1.8.1 ; python_version<"3"
4-
ruamel.yaml==0.13.7
4+
ruamel.yaml==0.15.2
55
rdflib==4.2.2
66
rdflib-jsonld==0.4.0
77
mistune==0.7.3

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: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
from __future__ import absolute_import
12
import collections
23
import shutil
34
import json
5+
6+
import six
7+
from six.moves import urllib
8+
49
import ruamel.yaml as yaml
510
try:
611
from ruamel.yaml import CSafeLoader as SafeLoader
@@ -16,7 +21,6 @@
1621
from rdflib import Graph, URIRef
1722
import rdflib.namespace
1823
from rdflib.namespace import RDF, RDFS
19-
import urlparse
2024
import logging
2125
from schema_salad.utils import aslist
2226
from typing import (cast, Any, Dict, Iterable, List, Optional, Text, Tuple,
@@ -34,16 +38,16 @@ def pred(datatype, # type: Dict[str, Union[Dict, str]]
3438
namespaces # type: Dict[str, rdflib.namespace.Namespace]
3539
):
3640
# type: (...) -> Union[Dict, Text]
37-
split = urlparse.urlsplit(name)
41+
split = urllib.parse.urlsplit(name)
3842

39-
vee = None # type: Optional[Union[str, unicode]]
43+
vee = None # type: Optional[Text]
4044

4145
if split.scheme != '':
4246
vee = name
43-
(ns, ln) = rdflib.namespace.split_uri(unicode(vee))
47+
(ns, ln) = rdflib.namespace.split_uri(six.text_type(vee))
4448
name = ln
4549
if ns[0:-1] in namespaces:
46-
vee = unicode(namespaces[ns[0:-1]][ln])
50+
vee = six.text_type(namespaces[ns[0:-1]][ln])
4751
_logger.debug("name, v %s %s", name, vee)
4852

4953
v = None # type: Optional[Dict]
@@ -102,11 +106,11 @@ def process_type(t, # type: Dict[str, Any]
102106
classnode = URIRef(recordname)
103107
g.add((classnode, RDF.type, RDFS.Class))
104108

105-
split = urlparse.urlsplit(recordname)
109+
split = urllib.parse.urlsplit(recordname)
106110
predicate = recordname
107111
if t.get("inVocab", True):
108112
if split.scheme:
109-
(ns, ln) = rdflib.namespace.split_uri(unicode(recordname))
113+
(ns, ln) = rdflib.namespace.split_uri(six.text_type(recordname))
110114
predicate = recordname
111115
recordname = ln
112116
else:
@@ -129,15 +133,15 @@ def process_type(t, # type: Dict[str, Any]
129133
_logger.debug("Processing field %s", i)
130134

131135
v = pred(t, i, fieldname, context, defaultPrefix,
132-
namespaces) # type: Union[Dict[Any, Any], unicode, None]
136+
namespaces) # type: Union[Dict[Any, Any], Text, None]
133137

134-
if isinstance(v, basestring):
138+
if isinstance(v, six.string_types):
135139
v = v if v[0] != "@" else None
136140
elif v is not None:
137141
v = v["_@id"] if v.get("_@id", "@")[0] != "@" else None
138142

139143
if bool(v):
140-
(ns, ln) = rdflib.namespace.split_uri(unicode(v))
144+
(ns, ln) = rdflib.namespace.split_uri(six.text_type(v))
141145
if ns[0:-1] in namespaces:
142146
propnode = namespaces[ns[0:-1]][ln]
143147
else:
@@ -188,8 +192,8 @@ def salad_to_jsonld_context(j, schema_ctx):
188192
return (context, g)
189193

190194

191-
def fix_jsonld_ids(obj, # type: Union[Dict[unicode, Any], List[Dict[unicode, Any]]]
192-
ids # type: List[unicode]
195+
def fix_jsonld_ids(obj, # type: Union[Dict[Text, Any], List[Dict[Text, Any]]]
196+
ids # type: List[Text]
193197
):
194198
# type: (...) -> None
195199
if isinstance(obj, dict):
@@ -203,22 +207,22 @@ def fix_jsonld_ids(obj, # type: Union[Dict[unicode, Any], List[Dict[unicode,
203207
fix_jsonld_ids(entry, ids)
204208

205209

206-
def makerdf(workflow, # type: Union[str, unicode]
207-
wf, # type: Union[List[Dict[unicode, Any]], Dict[unicode, Any]]
210+
def makerdf(workflow, # type: Text
211+
wf, # type: Union[List[Dict[Text, Any]], Dict[Text, Any]]
208212
ctx, # type: ContextType
209213
graph=None # type: Graph
210214
):
211215
# type: (...) -> Graph
212216
prefixes = {}
213217
idfields = []
214-
for k, v in ctx.iteritems():
218+
for k, v in six.iteritems(ctx):
215219
if isinstance(v, dict):
216220
url = v["@id"]
217221
else:
218222
url = v
219223
if url == "@id":
220224
idfields.append(k)
221-
doc_url, frg = urlparse.urldefrag(url)
225+
doc_url, frg = urllib.parse.urldefrag(url)
222226
if "/" in frg:
223227
p = frg.split("/")[0]
224228
prefixes[p] = u"%s#%s/" % (doc_url, p)
@@ -242,7 +246,7 @@ def makerdf(workflow, # type: Union[str, unicode]
242246
for sub, pred, obj in g.triples((None, URIRef("@id"), None)):
243247
g.remove((sub, pred, obj))
244248

245-
for k2, v2 in prefixes.iteritems():
249+
for k2, v2 in six.iteritems(prefixes):
246250
g.namespace_manager.bind(k2, v2)
247251

248252
return g

schema_salad/main.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
from __future__ import print_function
2+
from __future__ import absolute_import
23
import argparse
34
import logging
45
import sys
56
import traceback
67
import json
78
import os
8-
import urlparse
9+
10+
from six.moves import urllib
911

1012
import pkg_resources # part of setuptools
1113

12-
from typing import Any, Dict, List, Union
14+
from typing import Any, Dict, List, Union, Text
1315

1416
from rdflib import Graph, plugin
1517
from rdflib.serializer import Serializer
@@ -20,6 +22,7 @@
2022
from . import validate
2123
from .sourceline import strip_dup_lineno
2224
from .ref_resolver import Loader
25+
import six
2326

2427
_logger = logging.getLogger("salad")
2528

@@ -28,8 +31,8 @@
2831

2932

3033
def printrdf(workflow, # type: str
31-
wf, # type: Union[List[Dict[unicode, Any]], Dict[unicode, Any]]
32-
ctx, # type: Dict[unicode, Any]
34+
wf, # type: Union[List[Dict[Text, Any]], Dict[Text, Any]]
35+
ctx, # type: Dict[Text, Any]
3336
sr # type: str
3437
):
3538
# type: (...) -> None
@@ -108,7 +111,7 @@ def main(argsl=None): # type: (List[str]) -> int
108111
# Load schema document and resolve refs
109112

110113
schema_uri = args.schema
111-
if not urlparse.urlparse(schema_uri)[0]:
114+
if not urllib.parse.urlparse(schema_uri)[0]:
112115
schema_uri = "file://" + os.path.abspath(schema_uri)
113116
schema_raw_doc = metaschema_loader.fetch(schema_uri)
114117

@@ -118,8 +121,8 @@ def main(argsl=None): # type: (List[str]) -> int
118121
except (validate.ValidationException) as e:
119122
_logger.error("Schema `%s` failed link checking:\n%s",
120123
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())
124+
_logger.debug("Index is %s", list(metaschema_loader.idx.keys()))
125+
_logger.debug("Vocabulary is %s", list(metaschema_loader.vocab.keys()))
123126
return 1
124127
except (RuntimeError) as e:
125128
_logger.error("Schema `%s` read error:\n%s",
@@ -132,7 +135,7 @@ def main(argsl=None): # type: (List[str]) -> int
132135
return 0
133136

134137
if not args.document and args.print_index:
135-
print(json.dumps(metaschema_loader.idx.keys(), indent=4))
138+
print(json.dumps(list(metaschema_loader.idx.keys()), indent=4))
136139
return 0
137140

138141
# Validate the schema document against the metaschema
@@ -205,12 +208,12 @@ def main(argsl=None): # type: (List[str]) -> int
205208
# Load target document and resolve refs
206209
try:
207210
uri = args.document
208-
if not urlparse.urlparse(uri)[0]:
211+
if not urllib.parse.urlparse(uri)[0]:
209212
doc = "file://" + os.path.abspath(uri)
210213
document, doc_metadata = document_loader.resolve_ref(uri)
211214
except (validate.ValidationException, RuntimeError) as e:
212215
_logger.error("Document `%s` failed validation:\n%s",
213-
args.document, strip_dup_lineno(unicode(e)), exc_info=args.debug)
216+
args.document, strip_dup_lineno(six.text_type(e)), exc_info=args.debug)
214217
return 1
215218

216219
# Optionally print the document after ref resolution
@@ -219,7 +222,7 @@ def main(argsl=None): # type: (List[str]) -> int
219222
return 0
220223

221224
if args.print_index:
222-
print(json.dumps(document_loader.idx.keys(), indent=4))
225+
print(json.dumps(list(document_loader.idx.keys()), indent=4))
223226
return 0
224227

225228
# Validate the schema document against the metaschema

0 commit comments

Comments
 (0)