Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating example directory when needed #2111

Merged
merged 4 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/ansys/mapdl/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
os.makedirs(USER_DATA_PATH)

EXAMPLES_PATH = os.path.join(USER_DATA_PATH, "examples")
if not os.path.exists(EXAMPLES_PATH): # pragma: no cover
os.makedirs(EXAMPLES_PATH)


from ansys.mapdl.core.logging import Logger

Expand Down
23 changes: 21 additions & 2 deletions src/ansys/mapdl/core/examples/downloads.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Functions to download sample datasets from the pyansys data repository.
"""
from functools import wraps
import os
import shutil
import urllib.request
Expand All @@ -10,6 +11,22 @@
from ansys.mapdl import core as pymapdl


def check_directory_exist(directory):
# Wrapping LISTING FUNCTIONS.
def wrap_function(func):
@wraps(func)
def inner_wrapper(*args, **kwargs):
# Check if folder exists
if not os.path.exists(directory): # pragma: no cover
os.makedirs(directory)

return func(*args, **kwargs)

return inner_wrapper

return wrap_function


def get_ext(filename):
"""Extract the extension of the filename"""
ext = os.path.splitext(filename)[1].lower()
Expand All @@ -18,11 +35,12 @@ def get_ext(filename):

def delete_downloads():
"""Delete all downloaded examples to free space or update the files"""
shutil.rmtree(pymapdl.EXAMPLES_PATH)
os.makedirs(pymapdl.EXAMPLES_PATH)
if os.path.exists(pymapdl.EXAMPLES_PATH):
shutil.rmtree(pymapdl.EXAMPLES_PATH)
return True


@check_directory_exist(pymapdl.EXAMPLES_PATH)
def _decompress(filename):
zip_ref = zipfile.ZipFile(filename, "r")
zip_ref.extractall(pymapdl.EXAMPLES_PATH)
Expand All @@ -45,6 +63,7 @@ def _check_url_exist(url):
return [False]


@check_directory_exist(pymapdl.EXAMPLES_PATH)
def _retrieve_file(url, filename, _test=False):
# scape test
if pymapdl.RUNNING_TESTS:
Expand Down
46 changes: 45 additions & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,63 @@

import pytest

from ansys.mapdl.core import examples
from ansys.mapdl.core import EXAMPLES_PATH, examples
from ansys.mapdl.core.examples.downloads import (
_download_file,
_download_rotor_tech_demo_plot,
check_directory_exist,
delete_downloads,
download_bracket,
download_cfx_mapping_example_data,
download_example_data,
download_manifold_example_data,
download_tech_demo_data,
download_vtk_rotor,
get_ext,
)


def test_check_directory_exist(tmpdir):
tmp_dir = os.path.join(tmpdir, "mytempdir")

assert not os.path.exists(tmp_dir)

@check_directory_exist(tmp_dir)
def myfunc():
return "Directory should exist after this"

assert not os.path.exists(tmp_dir)

assert myfunc()
assert os.path.exists(tmp_dir)


@pytest.mark.parametrize(
"filename,ext",
(
["asdf/adfasdf/asdfaf.tt", ".tt"],
["asdfadf/asdfasfdasf", ""],
["asdfsadf.qerq", ".qerq"],
),
)
def test_get_ext(filename, ext):
assert get_ext(filename) == ext


def test_delete_downloads():
if not os.path.exists(EXAMPLES_PATH):
os.mkdir(EXAMPLES_PATH)

# Check we can delete the folder with files even.
file_path = os.path.join(EXAMPLES_PATH, "myfile")
with open(file_path, "w") as fid:
fid.write("dummy file")

delete_downloads()
assert not os.path.exists(EXAMPLES_PATH)
assert not os.path.exists(file_path)


def test_load_verif():
for filename in examples.vmfiles.values():
assert os.path.isfile(filename)
Expand Down