From 9f2bc65e67b124c8251a806d5efb19e111ba396d Mon Sep 17 00:00:00 2001
From: vhirtham <volker.hirthammer@bam.de>
Date: Thu, 17 Feb 2022 13:25:10 +0100
Subject: [PATCH] Download `single_pass_weld.wx` directly in the tutorials
 (#705)

---
 doc/conf.py                               | 39 -----------------------
 tutorials/01_01_introduction.ipynb        | 16 ++++------
 tutorials/01_02_time_dependent_data.ipynb | 22 ++++++++++++-
 tutorials/01_03_geometry.ipynb            | 30 +++++++++++++++--
 tutorials/util.py                         | 12 ++++---
 5 files changed, 62 insertions(+), 57 deletions(-)

diff --git a/doc/conf.py b/doc/conf.py
index ca2f0eec3..974bf01d0 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -89,45 +89,6 @@ def _copy_tut_files():
     shutil.copy(f, tutorials_dir)
 
 
-def download_tutorial_input_file():
-    from urllib.request import urlretrieve
-
-    url = "https://github.com/BAMWelDX/IIW2021_AA_CXII/blob/weldx_0.5.0/single_pass_weld.weldx?raw=true"
-    sha256sum = "29e4f11ef1185f818b4611860842ef52d386ad2866a2680257950f160e1e098a"
-
-    def hash_path(path):
-        import hashlib
-
-        h = hashlib.sha256()
-        with open(path, "rb") as fh:
-            h.update(fh.read())
-        return h.hexdigest()
-
-    dest = tutorials_dir / "single_pass_weld.wx"
-
-    # check if existing files matches desired one.
-    if dest.exists():
-        hash_local = hash_path(dest)
-        if hash_local == sha256sum:
-            logger.info(f"File %s already downloaded.", dest)
-            return
-
-    # does not exist or hash mismatched, so download it.
-    logger.info("trying to download: %s", url)
-    out_file, header = urlretrieve(url, dest)
-    sha256sum_actual = hash_path(out_file)
-    if not sha256sum_actual == sha256sum:
-        raise RuntimeError(
-            f"hash mismatch:\n actual = \t{sha256sum_actual}\n"
-            f"desired = \t{sha256sum}"
-        )
-
-    logger.info("download successful.")
-
-
-download_tutorial_input_file()
-
-
 # -- Project information -----------------------------------------------------
 _now = datetime.datetime.now().year
 
diff --git a/tutorials/01_01_introduction.ipynb b/tutorials/01_01_introduction.ipynb
index 4e81aba6f..c38c02684 100644
--- a/tutorials/01_01_introduction.ipynb
+++ b/tutorials/01_01_introduction.ipynb
@@ -48,18 +48,16 @@
     "jupyter": {
      "outputs_hidden": false
     },
-    "pycharm": {
-     "name": "#%%\n"
-    },
-    "tags": [
-     "hide-cell"
-    ]
+    "nbsphinx": "hidden",
+    "tags": []
    },
    "outputs": [],
    "source": [
-    "import util\n",
+    "# download the example file for this tutorial\n",
+    "\n",
+    "from util import download_tutorial_input_file\n",
     "\n",
-    "util.download_tutorial_input_file()"
+    "download_tutorial_input_file(print_status=False)"
    ]
   },
   {
@@ -194,7 +192,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.9.7"
+   "version": "3.8.12"
   }
  },
  "nbformat": 4,
diff --git a/tutorials/01_02_time_dependent_data.ipynb b/tutorials/01_02_time_dependent_data.ipynb
index a18b4a6e5..b43be252d 100644
--- a/tutorials/01_02_time_dependent_data.ipynb
+++ b/tutorials/01_02_time_dependent_data.ipynb
@@ -19,6 +19,26 @@
     "- Opening and navigating through WelDX files ([tutorial](01_01_introduction.ipynb))"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": false,
+    "jupyter": {
+     "outputs_hidden": false
+    },
+    "nbsphinx": "hidden",
+    "tags": []
+   },
+   "outputs": [],
+   "source": [
+    "# download the example file for this tutorial\n",
+    "\n",
+    "from util import download_tutorial_input_file\n",
+    "\n",
+    "download_tutorial_input_file(print_status=False)"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {
@@ -430,7 +450,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.8.11"
+   "version": "3.8.12"
   }
  },
  "nbformat": 4,
diff --git a/tutorials/01_03_geometry.ipynb b/tutorials/01_03_geometry.ipynb
index a208e0b42..6e20b5375 100644
--- a/tutorials/01_03_geometry.ipynb
+++ b/tutorials/01_03_geometry.ipynb
@@ -2,7 +2,9 @@
  "cells": [
   {
    "cell_type": "markdown",
-   "metadata": {},
+   "metadata": {
+    "tags": []
+   },
    "source": [
     "# Groove based workpiece data and geometry\n",
     "\n",
@@ -19,9 +21,31 @@
     "- Opening and navigating through WelDX files ([tutorial](01_01_introduction.ipynb))"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": false,
+    "jupyter": {
+     "outputs_hidden": false
+    },
+    "nbsphinx": "hidden",
+    "tags": []
+   },
+   "outputs": [],
+   "source": [
+    "# download the example file for this tutorial\n",
+    "\n",
+    "from util import download_tutorial_input_file\n",
+    "\n",
+    "download_tutorial_input_file(print_status=False)"
+   ]
+  },
   {
    "cell_type": "markdown",
-   "metadata": {},
+   "metadata": {
+    "tags": []
+   },
    "source": [
     "## Plotting the specimen's groove\n",
     "\n",
@@ -367,7 +391,7 @@
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.9.9"
+   "version": "3.8.12"
   }
  },
  "nbformat": 4,
diff --git a/tutorials/util.py b/tutorials/util.py
index 92cd2f13a..1ab8a0d8b 100644
--- a/tutorials/util.py
+++ b/tutorials/util.py
@@ -4,7 +4,7 @@
 tutorials_dir = Path(__file__).parent.absolute()
 
 
-def download_tutorial_input_file():
+def download_tutorial_input_file(print_status=True):
     from urllib.request import urlretrieve
 
     url = (
@@ -27,11 +27,13 @@ def hash_path(path):
     if dest.exists():
         hash_local = hash_path(dest)
         if hash_local == sha256sum:
-            print(f"File {dest} already downloaded.")
+            if print_status:
+                print(f"File {dest} already downloaded.")
             return
 
     # does not exist or hash mismatched, so download it.
-    print("trying to download: {url}")
+    if print_status:
+        print("trying to download: {url}")
     out_file, _ = urlretrieve(url, dest)  # skipcq: BAN-B310
     sha256sum_actual = hash_path(out_file)
     if sha256sum_actual != sha256sum:
@@ -39,5 +41,5 @@ def hash_path(path):
             f"hash mismatch:\n actual = \t{sha256sum_actual}\n"
             f"desired = \t{sha256sum}"
         )
-
-    print("download successful.")
+    if print_status:
+        print("download successful.")