Skip to content

Commit aeab7c3

Browse files
author
Peter Amstutz
committed
Merge branch 'master' of github.com:common-workflow-language/schema_salad
Conflicts: schema_salad/ref_resolver.py schema_salad/schema.py
2 parents e381f64 + 15dae9f commit aeab7c3

Some content is hidden

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

77 files changed

+3092
-398
lines changed

.travis.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ os:
55
env:
66
- TOX_ENV=py27-lint
77
- TOX_ENV=py27-unit
8-
# - TOX_ENV=py34-lint
9-
# - TOX_ENV=py33-lint
10-
# - TOX_ENV=py27-lint
11-
# - TOX_ENV=py34-unit
12-
# - TOX_ENV=py33-unit
8+
- TOX_ENV=py34-mypy
9+
# - TOX_ENV=py34-lint
10+
# - TOX_ENV=py33-lint
11+
# - TOX_ENV=py34-unit
12+
# - TOX_ENV=py33-unit
1313

1414
install:
1515
- pip install tox

Makefile

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,51 +102,50 @@ diff_pylint_report: pylint_report.txt
102102
diff-quality --violations=pylint pylint_report.txt
103103

104104
.coverage: $(PYSOURCES)
105-
python-coverage run --branch --source=${MODULE} tests/test_examples.py
106-
python-coverage run --append --branch --source=${MODULE} \
105+
coverage run --branch --source=${MODULE} setup.py test
106+
coverage run --append --branch --source=${MODULE} \
107107
-m schema_salad.main \
108108
--print-jsonld-context schema_salad/metaschema/metaschema.yml \
109109
> /dev/null
110-
python-coverage run --append --branch --source=${MODULE} \
110+
coverage run --append --branch --source=${MODULE} \
111111
-m schema_salad.main \
112112
--print-rdfs schema_salad/metaschema/metaschema.yml \
113113
> /dev/null
114-
python-coverage run --append --branch --source=${MODULE} \
114+
coverage run --append --branch --source=${MODULE} \
115115
-m schema_salad.main \
116116
--print-avro schema_salad/metaschema/metaschema.yml \
117117
> /dev/null
118-
python-coverage run --append --branch --source=${MODULE} \
118+
coverage run --append --branch --source=${MODULE} \
119119
-m schema_salad.main \
120120
--print-rdf schema_salad/metaschema/metaschema.yml \
121121
> /dev/null
122-
python-coverage run --append --branch --source=${MODULE} \
122+
coverage run --append --branch --source=${MODULE} \
123123
-m schema_salad.main \
124124
--print-pre schema_salad/metaschema/metaschema.yml \
125125
> /dev/null
126-
python-coverage run --append --branch --source=${MODULE} \
126+
coverage run --append --branch --source=${MODULE} \
127127
-m schema_salad.main \
128128
--print-index schema_salad/metaschema/metaschema.yml \
129129
> /dev/null
130-
python-coverage run --append --branch --source=${MODULE} \
130+
coverage run --append --branch --source=${MODULE} \
131131
-m schema_salad.main \
132132
--print-metadata schema_salad/metaschema/metaschema.yml \
133133
> /dev/null
134-
python-coverage run --append --branch --source=${MODULE} \
134+
coverage run --append --branch --source=${MODULE} \
135135
-m schema_salad.makedoc schema_salad/metaschema/metaschema.yml \
136136
> /dev/null
137137

138-
139138
coverage.xml: .coverage
140-
python-coverage xml
139+
coverage xml
141140

142141
coverage.html: htmlcov/index.html
143142

144143
htmlcov/index.html: .coverage
145-
python-coverage html
144+
coverage html
146145
@echo Test coverage of the Python code is now in htmlcov/index.html
147146

148147
coverage-report: .coverage
149-
python-coverage report
148+
coverage report
150149

151150
diff-cover: coverage-gcovr.xml coverage.xml
152151
diff-cover coverage-gcovr.xml coverage.xml
@@ -170,4 +169,14 @@ list-author-emails:
170169
@echo 'name, E-Mail Address'
171170
@git log --format='%aN,%aE' | sort -u | grep -v 'root'
172171

172+
mypy: ${PYSOURCES}
173+
MYPYPATH=typeshed/2.7 mypy --py2 schema_salad
174+
#MYPYPATH=typeshed/2.7 mypy --py2 --disallow-untyped-calls schema_salad
175+
176+
jenkins:
177+
if ! test -d env ; then virtualenv env ; fi
178+
. env/bin/activate ; \
179+
${MAKE} install-dep coverage.html coverage.xml mypy pep257_report.txt \
180+
sloccount.sc pep8_report.txt pylint_report.txt
181+
173182
FORCE:

schema_salad/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22
import sys
3-
if sys.version_info >= (2,7):
4-
import typing
3+
import typing
54

65
__author__ = '[email protected]'
76

schema_salad/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from . import main
22
import sys
3-
if sys.version_info >= (2,7):
4-
import typing
3+
import typing
54

65
sys.exit(main.main())

schema_salad/add_dictlist.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
2-
if sys.version_info >= (2,7):
3-
import typing
2+
from typing import Any, Dict
43

5-
def add_dictlist(di, key, val):
4+
def add_dictlist(di, key, val): # type: (Dict, Any, Any) -> None
65
if key not in di:
76
di[key] = []
87
di[key].append(val)

schema_salad/aslist.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import sys
2-
if sys.version_info >= (2,7):
3-
import typing
2+
from typing import Any, List
43

5-
def aslist(l):
4+
def aslist(l): # type: (Any) -> List
65
"""Convenience function to wrap single items and lists, and return lists unchanged."""
76

87
if isinstance(l, list):

schema_salad/flatten.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import sys
2-
if sys.version_info >= (2,7):
3-
import typing
2+
from typing import Any, Tuple
43

54
# http://rightfootin.blogspot.com/2006/09/more-on-python-flatten.html
65
def flatten(l, ltypes=(list, tuple)):
6+
# type: (Any, Any) -> Any
77
if l is None:
88
return []
99
if not isinstance(l, ltypes):
@@ -21,4 +21,4 @@ def flatten(l, ltypes=(list, tuple)):
2121
else:
2222
l[i:i + 1] = l[i]
2323
i += 1
24-
return ltype(l)
24+
return ltype(l) # type: ignore

schema_salad/jsonld_context.py

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import collections
12
import shutil
23
import json
34
import ruamel.yaml as yaml
45
try:
5-
from ruamel.yaml import CSafeLoader as SafeLoader
6+
from ruamel.yaml import CSafeLoader as SafeLoader
67
except ImportError:
7-
from ruamel.yaml import SafeLoader
8+
from ruamel.yaml import SafeLoader # type: ignore
89
import os
910
import subprocess
1011
import copy
@@ -15,62 +16,74 @@
1516
from rdflib import Graph, URIRef
1617
import rdflib.namespace
1718
from rdflib.namespace import RDF, RDFS
18-
try:
19-
import urlparse
20-
except ImportError:
21-
import urllib.parse as urlparse
22-
unicode=str
23-
basestring=str
19+
import urlparse
2420
import logging
2521
from .aslist import aslist
26-
if sys.version_info >= (2,7):
27-
import typing
22+
from typing import Any, cast, Dict, Iterable, Tuple, Union
23+
from .ref_resolver import Loader
2824

2925
_logger = logging.getLogger("salad")
3026

27+
3128
def pred(datatype, field, name, context, defaultBase, namespaces):
29+
# type: (Dict[str, Union[Dict, str]], Dict, str, Loader.ContextType, str, Dict[str, rdflib.namespace.Namespace]) -> Union[Dict, str]
3230
split = urlparse.urlsplit(name)
3331

34-
v = None
32+
vee = None # type: Union[str, unicode]
3533

3634
if split.scheme:
37-
v = name
38-
(ns, ln) = rdflib.namespace.split_uri(unicode(v))
35+
vee = name
36+
(ns, ln) = rdflib.namespace.split_uri(unicode(vee))
3937
name = ln
4038
if ns[0:-1] in namespaces:
41-
v = unicode(namespaces[ns[0:-1]][ln])
42-
_logger.debug("name, v %s %s", name, v)
39+
vee = unicode(namespaces[ns[0:-1]][ln])
40+
_logger.debug("name, v %s %s", name, vee)
41+
42+
v = None # type: Any
4343

4444
if field and "jsonldPredicate" in field:
4545
if isinstance(field["jsonldPredicate"], dict):
4646
v = {}
4747
for k, val in field["jsonldPredicate"].items():
48-
v[("@"+k[1:] if k.startswith("_") else k)] = val
48+
v[("@" + k[1:] if k.startswith("_") else k)] = val
4949
else:
5050
v = field["jsonldPredicate"]
5151
elif "jsonldPredicate" in datatype:
52-
for d in datatype["jsonldPredicate"]:
53-
if d["symbol"] == name:
54-
v = d["predicate"]
52+
if isinstance(datatype["jsonldPredicate"], collections.Iterable):
53+
for d in datatype["jsonldPredicate"]:
54+
if isinstance(d, dict):
55+
if d["symbol"] == name:
56+
v = d["predicate"]
57+
else:
58+
raise Exception(
59+
"entries in the jsonldPredicate List must be "
60+
"Dictionaries")
61+
else:
62+
raise Exception("jsonldPredicate must be a List of Dictionaries.")
5563
# if not v:
5664
# if field and "jsonldPrefix" in field:
5765
# defaultBase = field["jsonldPrefix"]
5866
# elif "jsonldPrefix" in datatype:
5967
# defaultBase = datatype["jsonldPrefix"]
6068

61-
if not v:
62-
v = defaultBase + name
69+
ret = v or vee
70+
71+
if not ret:
72+
ret = defaultBase + name
6373

6474
if name in context:
65-
if context[name] != v:
66-
raise Exception("Predicate collision on %s, '%s' != '%s'" % (name, context[name], v))
75+
if context[name] != ret:
76+
raise Exception("Predicate collision on %s, '%s' != '%s'" %
77+
(name, context[name], ret))
6778
else:
68-
_logger.debug("Adding to context '%s' %s (%s)", name, v, type(v))
69-
context[name] = v
79+
_logger.debug("Adding to context '%s' %s (%s)", name, ret, type(ret))
80+
context[name] = ret
81+
82+
return ret
7083

71-
return v
7284

7385
def process_type(t, g, context, defaultBase, namespaces, defaultPrefix):
86+
# type: (Dict[str, Any], Graph, Loader.ContextType, str, Dict[str, rdflib.namespace.Namespace], str) -> None
7487
if t["type"] == "record":
7588
recordname = t["name"]
7689

@@ -90,12 +103,14 @@ def process_type(t, g, context, defaultBase, namespaces, defaultPrefix):
90103
predicate = "%s:%s" % (defaultPrefix, recordname)
91104

92105
if context.get(recordname, predicate) != predicate:
93-
raise Exception("Predicate collision on '%s', '%s' != '%s'" % (recordname, context[recordname], predicate))
106+
raise Exception("Predicate collision on '%s', '%s' != '%s'" % (
107+
recordname, context[recordname], predicate))
94108

95109
if not recordname:
96110
raise Exception()
97111

98-
_logger.debug("Adding to context '%s' %s (%s)", recordname, predicate, type(predicate))
112+
_logger.debug("Adding to context '%s' %s (%s)",
113+
recordname, predicate, type(predicate))
99114
context[recordname] = predicate
100115

101116
for i in t.get("fields", []):
@@ -123,7 +138,8 @@ def process_type(t, g, context, defaultBase, namespaces, defaultPrefix):
123138
# TODO generate range from datatype.
124139

125140
if isinstance(i["type"], dict) and "name" in i["type"]:
126-
process_type(i["type"], g, context, defaultBase, namespaces, defaultPrefix)
141+
process_type(i["type"], g, context, defaultBase,
142+
namespaces, defaultPrefix)
127143

128144
if "extends" in t:
129145
for e in aslist(t["extends"]):
@@ -136,31 +152,26 @@ def process_type(t, g, context, defaultBase, namespaces, defaultPrefix):
136152

137153

138154
def salad_to_jsonld_context(j, schema_ctx):
139-
context = {}
155+
# type: (Iterable, Dict[str, Any]) -> Tuple[Loader.ContextType, Graph]
156+
context = {} # type: Loader.ContextType
140157
namespaces = {}
141158
g = Graph()
142159
defaultPrefix = ""
143160

144-
for k,v in schema_ctx.items():
161+
for k, v in schema_ctx.items():
145162
context[k] = v
146163
namespaces[k] = rdflib.namespace.Namespace(v)
147164

148165
if "@base" in context:
149-
defaultBase = context["@base"]
166+
defaultBase = cast(str, context["@base"])
150167
del context["@base"]
151168
else:
152169
defaultBase = ""
153170

154-
for k,v in namespaces.items():
171+
for k, v in namespaces.items():
155172
g.bind(k, v)
156173

157174
for t in j:
158175
process_type(t, g, context, defaultBase, namespaces, defaultPrefix)
159176

160177
return (context, g)
161-
162-
if __name__ == "__main__":
163-
with open(sys.argv[1]) as f:
164-
j = yaml.load(f, Loader=SafeLoader)
165-
(ctx, g) = salad_to_jsonld_context(j)
166-
print(json.dumps(ctx, indent=4, sort_keys=True))

0 commit comments

Comments
 (0)