Skip to content

Commit 2514a23

Browse files
committed
switch to ruamel.yaml, use C loader if available, only load safely (#20)
1 parent 37c129d commit 2514a23

File tree

10 files changed

+37
-21
lines changed

10 files changed

+37
-21
lines changed

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ provides for robust support of inline documentation.
7070
.. _JSON-LD: http://json-ld.org
7171
.. _Avro: http://avro.apache.org
7272
.. _metaschema: https://github.com/common-workflow-language/schema_salad/blob/master/schema_salad/metaschema/metaschema.yml
73-
.. _specification: https://common-workflow-language.github.io/draft-3/SchemaSalad.html
73+
.. _specification: http://www.commonwl.org/draft-3/SchemaSalad.html
7474
.. _Language: https://github.com/common-workflow-language/common-workflow-language/blob/master/draft-3/CommandLineTool.yml
7575
.. _RDF: https://www.w3.org/RDF/

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
requests
2-
PyYAML
2+
ruamel.yaml
33
rdflib >= 4.1.
44
rdflib-jsonld >= 0.3.0
55
mistune

schema_salad/jsonld_context.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import shutil
22
import json
3-
import yaml
3+
import ruamel.yaml as yaml
4+
try:
5+
from ruamel.yaml import CSafeLoader as SafeLoader
6+
except ImportError:
7+
from ruamel.yaml import SafeLoader
48
import os
59
import subprocess
610
import copy
@@ -157,6 +161,6 @@ def salad_to_jsonld_context(j, schema_ctx):
157161

158162
if __name__ == "__main__":
159163
with open(sys.argv[1]) as f:
160-
j = yaml.load(f)
164+
j = yaml.load(f, Loader=SafeLoader)
161165
(ctx, g) = salad_to_jsonld_context(j)
162166
print(json.dumps(ctx, indent=4, sort_keys=True))

schema_salad/main.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import json
1010
from rdflib import Graph, plugin
1111
from rdflib.serializer import Serializer
12-
import yaml
1312
import os
1413
try:
1514
import urlparse

schema_salad/makedoc.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import mistune
22
from . import schema
33
import json
4-
import yaml
54
import os
65
import copy
76
import re

schema_salad/ref_resolver.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import urlparse
1010
except:
1111
import urllib.parse as urlparse
12-
import yaml
12+
import ruamel.yaml as yaml
13+
try:
14+
from ruamel.yaml import CSafeLoader as SafeLoader
15+
except ImportError:
16+
from ruamel.yaml import SafeLoader
1317
from . import validate
1418
import pprint
1519
try:
@@ -456,7 +460,7 @@ def fetch(self, url):
456460
else:
457461
text = StringIO(text)
458462
text.name = url
459-
result = yaml.load(text)
463+
result = yaml.load(text, Loader=SafeLoader)
460464
except yaml.parser.ParserError as e:
461465
raise validate.ValidationException("Syntax error %s" % (e))
462466
if isinstance(result, dict) and self.identifiers:

schema_salad/schema.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import sys
55
import pprint
66
from pkg_resources import resource_stream
7-
import yaml
7+
import ruamel.yaml as yaml
8+
try:
9+
from ruamel.yaml import CSafeLoader as SafeLoader
10+
except ImportError:
11+
from ruamel.yaml import SafeLoader
812
import avro.schema
913
from . import validate
1014
import json
@@ -142,7 +146,8 @@ def get_metaschema():
142146
loader.cache["https://w3id.org/cwl/salad"] = rs.read()
143147
rs.close()
144148

145-
j = yaml.load(loader.cache["https://w3id.org/cwl/salad"])
149+
j = yaml.load(loader.cache["https://w3id.org/cwl/salad"],
150+
Loader=SafeLoader)
146151
j, _ = loader.resolve_all(j, "https://w3id.org/cwl/salad#")
147152

148153
#pprint.pprint(j)

schema_salad/validate.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pprint
22
import avro.schema
3-
import yaml
43
import sys
54
try:
65
import urlparse

setup.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
# In tox, it will cover them anyway.
2525
requirements = []
2626

27-
install_requires=[
28-
'requests',
29-
'PyYAML',
30-
'rdflib >= 4.1.0',
31-
'rdflib-jsonld >= 0.3.0',
32-
'mistune',
33-
'typing']
27+
install_requires = [
28+
'requests',
29+
'ruamel.yaml',
30+
'rdflib >= 4.1.0',
31+
'rdflib-jsonld >= 0.3.0',
32+
'mistune',
33+
'typing']
3434

3535
if sys.version_info.major < 3:
3636
install_requires.append("avro")

tests/test_examples.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import schema_salad.main
44
import schema_salad.schema
55
import rdflib
6-
import yaml
6+
import ruamel.yaml as yaml
7+
try:
8+
from ruamel.yaml import CSafeLoader as SafeLoader
9+
except ImportError:
10+
from ruamel.yaml import SafeLoader
711

812
class TestSchemas(unittest.TestCase):
913
def test_schemas(self):
@@ -111,11 +115,13 @@ def test_examples(self):
111115
for a in ["field_name", "ident_res", "link_res", "vocab_res"]:
112116
ldr, _, _ = schema_salad.schema.load_schema("schema_salad/metaschema/%s_schema.yml" % a)
113117
with open("schema_salad/metaschema/%s_src.yml" % a) as src_fp:
114-
src = ldr.resolve_all(yaml.load(src_fp), "")[0]
118+
src = ldr.resolve_all(yaml.load(src_fp, Loader=SafeLoader), "")[0]
115119
with open("schema_salad/metaschema/%s_proc.yml" % a) as src_proc:
116-
proc = yaml.load(src_proc)
120+
proc = yaml.load(src_proc, Loader=SafeLoader)
117121
self.assertEqual(proc, src)
118122

123+
def test_yaml_float_test(self):
124+
self.assertEqual(yaml.load("float-test: 2e-10")["float-test"], 2e-10)
119125

120126
if __name__ == '__main__':
121127
unittest.main()

0 commit comments

Comments
 (0)