Skip to content

Commit

Permalink
Use yaml.safe_load() instead of load()
Browse files Browse the repository at this point in the history
Fix warnings like:

```
planemo/shed/__init__.py:436: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  config = yaml.load(f)
```
  • Loading branch information
nsoranzo committed Apr 16, 2019
1 parent 381069e commit 1facb88
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion planemo/bioc_tool_builder.py
Expand Up @@ -234,7 +234,7 @@ def __init__(self, requirement, bioconda_path=None, update=False):
"r-" + name.lower(),
"meta.yaml")
with open(recipe_path, 'r') as f:
doc = yaml.load(f)
doc = yaml.safe_load(f)
if not version:
version = doc["package"]["version"]
package_url = doc["about"]["home"]
Expand Down
2 changes: 1 addition & 1 deletion planemo/conda_verify/recipe.py
Expand Up @@ -73,7 +73,7 @@ def select_lines(data, namespace):

@memoized
def yamlize(data):
res = yaml.load(data)
res = yaml.safe_load(data)
# ensure the result is a dict
if res is None:
res = {}
Expand Down
2 changes: 1 addition & 1 deletion planemo/config.py
Expand Up @@ -132,7 +132,7 @@ def read_global_config(config_path):
return DEFAULT_CONFIG

with open(config_path) as f:
return yaml.load(f)
return yaml.safe_load(f)


__all__ = (
Expand Down
2 changes: 1 addition & 1 deletion planemo/galaxy/activity.py
Expand Up @@ -248,7 +248,7 @@ def create_collection_func(element_identifiers, collection_type):
return dataset_collection

with open(job_path, "r") as f:
job = yaml.load(f)
job = yaml.safe_load(f)

# Figure out what "." should be here instead.
job_dir = os.path.dirname(job_path)
Expand Down
6 changes: 3 additions & 3 deletions planemo/galaxy/workflows.py
Expand Up @@ -24,13 +24,13 @@ def load_shed_repos(runnable):
if path.endswith(".ga"):
generate_tool_list_from_ga_workflow_files.generate_tool_list_from_workflow([path], "Tools from workflows", "tools.yml")
with open("tools.yml", "r") as f:
tools = yaml.load(f)["tools"]
tools = yaml.safe_load(f)["tools"]

else:
# It'd be better to just infer this from the tool shed ID somehow than
# require explicit annotation like this... I think?
with open(path, "r") as f:
workflow = yaml.load(f)
workflow = yaml.safe_load(f)

tools = workflow.get("tools", [])

Expand Down Expand Up @@ -81,7 +81,7 @@ def _raw_dict(path, importer=None):
workflow_directory = os.path.dirname(path)
workflow_directory = os.path.abspath(workflow_directory)
with open(path, "r") as f:
workflow = yaml.load(f)
workflow = yaml.safe_load(f)
workflow = python_to_workflow(workflow, importer, workflow_directory)

return workflow
Expand Down
2 changes: 1 addition & 1 deletion planemo/runnable.py
Expand Up @@ -121,7 +121,7 @@ def normalize_to_tests_path(path):
return os.path.normpath(absolute_path)

with open(tests_path, "r") as f:
tests_def = yaml.load(f)
tests_def = yaml.safe_load(f)

if not isinstance(tests_def, list):
message = TEST_FILE_NOT_LIST_MESSAGE % tests_path
Expand Down
2 changes: 1 addition & 1 deletion planemo/shed/__init__.py
Expand Up @@ -433,7 +433,7 @@ def shed_repo_config(ctx, path, name=None):
config = {}
if os.path.exists(shed_yaml_path):
with open(shed_yaml_path, "r") as f:
config = yaml.load(f)
config = yaml.safe_load(f)

if config is None: # yaml may yield None
config = {}
Expand Down
3 changes: 2 additions & 1 deletion planemo/shed_lint.py
Expand Up @@ -290,7 +290,8 @@ def lint_shed_yaml(realized_repository, lint_ctx):
lint_ctx.info("No .shed.yml file found, skipping.")
return
try:
yaml.load(open(shed_yaml, "r"))
with open(shed_yaml, "r") as fh:
yaml.safe_load(fh)
except Exception as e:
lint_ctx.warn("Failed to parse .shed.yml file [%s]" % str(e))
return
Expand Down
2 changes: 1 addition & 1 deletion planemo/training/tutorial.py
Expand Up @@ -262,7 +262,7 @@ def init_from_existing_tutorial(self, tuto_name):
tuto_split_regex = re.search(regex, tuto_content)
if not tuto_split_regex:
raise Exception("No metadata found at the top of the tutorial")
metadata = yaml.load(tuto_split_regex.group("metadata"))
metadata = yaml.safe_load(tuto_split_regex.group("metadata"))
self.title = metadata["title"]
self.zenodo_link = metadata["zenodo_link"]
self.questions = metadata["questions"]
Expand Down
2 changes: 1 addition & 1 deletion planemo/training/utils.py
Expand Up @@ -74,7 +74,7 @@ def export_to_ordered_dict(self):
def load_yaml(filepath):
"""Load the content of a YAML file to a dictionary."""
with open(filepath, "r") as m_file:
content = yaml.load(m_file)
content = yaml.safe_load(m_file)
return content


Expand Down
2 changes: 1 addition & 1 deletion scripts/update_bioconda.bash
Expand Up @@ -39,7 +39,7 @@ git checkout master
git merge --ff-only origin/master

METADATA="$RECIPE/meta.yaml"
OLD_VERSION=`python2 -c "import yaml; print yaml.load(open('$METADATA', 'r').read())['package']['version']"`
OLD_VERSION=`python2 -c "import yaml; print yaml.safe_load(open('$METADATA', 'r').read())['package']['version']"`

OLD_RECIPE="$RECIPE/$OLD_VERSION"
mkdir "$OLD_RECIPE"
Expand Down
4 changes: 2 additions & 2 deletions tests/test_build_and_lint.py
Expand Up @@ -41,7 +41,7 @@ def test_cwl(self):
self._check_lint(filename="seqtk_seq.cwl", exit_code=0)

with open(os.path.join(f, "seqtk_seq.cwl")) as stream:
process_dict = yaml.load(stream)
process_dict = yaml.safe_load(stream)
assert process_dict["id"] == "seqtk_seq"
assert process_dict["label"] == "Convert to FASTA (seqtk)"
assert process_dict["baseCommand"] == ["seqtk", "seq"]
Expand All @@ -55,7 +55,7 @@ def test_cwl(self):
assert process_dict["stdout"] == "out"

with open(os.path.join(f, "seqtk_seq_tests.yml")) as stream:
test_dict = yaml.load(stream)
test_dict = yaml.safe_load(stream)
assert test_dict

@skip_if_environ("PLANEMO_SKIP_CWLTOOL_TESTS")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config_init.py
Expand Up @@ -14,7 +14,7 @@ def test_config(self):
assert os.path.exists(self.planemo_yaml_path)
with open(self.planemo_yaml_path, "r") as f:
# Ensure it is valid YAML
yaml.load(f)
yaml.safe_load(f)

def test_config_wont_overwrite(self):
with self._isolate():
Expand Down
3 changes: 2 additions & 1 deletion tests/test_shed.py
Expand Up @@ -53,7 +53,8 @@ def test_shed(self):
"--shed_target", shed_url
]
self._check_exit_code(init_cmd)
contents_dict = yaml.load(open(".shed.yml", "r"))
with open(".shed.yml", "r") as fh:
contents_dict = yaml.safe_load(fh)
contents_dict["description"] = "Update test repository."
io.write_file(".shed.yml", yaml.dump(contents_dict))
update_cmd = [
Expand Down
6 changes: 4 additions & 2 deletions tests/test_shed_init.py
Expand Up @@ -23,7 +23,8 @@ def test_minimal(self):
])
shed_config_path = os.path.join(f, ".shed.yml")
assert os.path.exists(shed_config_path)
shed_config = yaml.load(open(shed_config_path, "r"))
with open(shed_config_path, "r") as fh:
shed_config = yaml.safe_load(fh)
assert shed_config["name"] == "samtools_filter"
assert shed_config["owner"] == "iuc"
assert shed_config["description"] == "samtools_filter"
Expand All @@ -48,7 +49,8 @@ def test_more_options(self):
self._check_exit_code(init_command)
shed_config_path = os.path.join(f, ".shed.yml")
assert os.path.exists(shed_config_path)
shed_config = yaml.load(open(shed_config_path, "r"))
with open(shed_config_path, "r") as fh:
shed_config = yaml.safe_load(fh)
assert shed_config["name"] == "samtools_filter"
assert shed_config["owner"] == "devteam"
assert shed_config["description"] == "A samtools repo"
Expand Down

0 comments on commit 1facb88

Please sign in to comment.