From ec96eafce05805ff4fe44e0829e6dc55bd40969b Mon Sep 17 00:00:00 2001
From: Nicolas Barthes <nicolas.barthes@cnrs.fr>
Date: Fri, 21 Jun 2024 13:53:26 +0200
Subject: [PATCH] Class to manage spectra md5 hash

---
 src/Class_Mod/Hash.py  | 26 ++++++++++++++++++++++++++
 src/Modules.py         |  1 +
 src/data/hash/hash.txt |  0
 3 files changed, 27 insertions(+)
 create mode 100644 src/Class_Mod/Hash.py
 create mode 100644 src/data/hash/hash.txt

diff --git a/src/Class_Mod/Hash.py b/src/Class_Mod/Hash.py
new file mode 100644
index 0000000..8a96cbd
--- /dev/null
+++ b/src/Class_Mod/Hash.py
@@ -0,0 +1,26 @@
+from Packages import *
+
+def create_hash(spectra):
+    #using the md5 hash function.
+    hash_func = hashlib.md5()
+    spectra = str(spectra)
+    encoded_spectra = spectra.encode()
+    hash_func.update(encoded_spectra)
+    hash = hash_func.hexdigest()
+    return hash
+
+def check_hash(hash):
+    # path to hash file and grep/cat functions for Win
+    subprocess_path = Path("src/data/hash/")
+    # run a grep from the hash onto the hash file
+    nb_hash = subprocess.run([subprocess_path / 'grep.exe', '-c', hash, subprocess_path / "hash.txt"], shell=True)
+    # if hash present
+    if 'returncode=0' in str(nb_hash):
+        return 'existing hash'
+    # if hash not present, add it to the file with cat function
+    else:
+        add_hash = subprocess.run(['echo', str(hash) + '>>', subprocess_path / "hash.txt"], shell=True)
+        if 'returncode=0' in str(add_hash):
+            return 'hash added'
+        else:
+            return 'error while adding the new hash'
\ No newline at end of file
diff --git a/src/Modules.py b/src/Modules.py
index 8699e56..ba9e784 100644
--- a/src/Modules.py
+++ b/src/Modules.py
@@ -3,6 +3,7 @@ from Class_Mod import Plsr, LinearPCA, Umap, find_col_index, PinardPlsr, Nmf, AP
 from Class_Mod import LWPLSR, list_files, metrics, TpeIpls, reg_plot, resid_plot, Sk_Kmeans, DxRead, Hdbscan, read_dx, PlsProcess
 from Class_Mod.DATA_HANDLING import *
 from Class_Mod.Miscellaneous import prediction, download_results, plot_spectra, local_css
+from Class_Mod.Hash import create_hash, check_hash
 from Report import report
 css_file = Path("style/")
 pages_folder = Path("pages/")
diff --git a/src/data/hash/hash.txt b/src/data/hash/hash.txt
new file mode 100644
index 0000000..e69de29
-- 
GitLab