From cbaba47f033e1289471849a413e12ac29b21fd59 Mon Sep 17 00:00:00 2001
From: Christoph Schmidt <christoph.,schmidt@tugraz.at>
Date: Wed, 22 Nov 2023 10:47:03 +0100
Subject: [PATCH] Updated Files. Configs are now saved as the filename and a
 path can be specified

---
 .idea/confighandler.iml                       |  2 +-
 .idea/fileTemplates/internal/Python Script.py |  7 +++
 .idea/jupyter-settings.xml                    | 30 +++++++++++++
 .idea/misc.xml                                |  2 +-
 examples/ApplicationConfig.py                 |  6 +--
 examples/LaserConfig.py                       |  4 +-
 pyproject.toml                                |  2 +-
 src/confighandler/controller/ConfigNode.py    | 45 ++++++++++++-------
 8 files changed, 73 insertions(+), 25 deletions(-)
 create mode 100644 .idea/fileTemplates/internal/Python Script.py
 create mode 100644 .idea/jupyter-settings.xml

diff --git a/.idea/confighandler.iml b/.idea/confighandler.iml
index d6194b2..d49f8ab 100644
--- a/.idea/confighandler.iml
+++ b/.idea/confighandler.iml
@@ -4,7 +4,7 @@
     <content url="file://$MODULE_DIR$">
       <excludeFolder url="file://$MODULE_DIR$/.venv" />
     </content>
-    <orderEntry type="jdk" jdkName="Python 3.11 (confighandler)" jdkType="Python SDK" />
+    <orderEntry type="jdk" jdkName="Python 3.12 (confighandler)" jdkType="Python SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
 </module>
\ No newline at end of file
diff --git a/.idea/fileTemplates/internal/Python Script.py b/.idea/fileTemplates/internal/Python Script.py
new file mode 100644
index 0000000..1e351b8
--- /dev/null
+++ b/.idea/fileTemplates/internal/Python Script.py	
@@ -0,0 +1,7 @@
+# -*- coding: utf-8 -*-
+"""
+Author(s): Christoph Schmidt <christoph.schmidt@tugraz.at>
+Created: 2023-10-19 12:35
+Package Version: ${version}
+Description: 
+"""
diff --git a/.idea/jupyter-settings.xml b/.idea/jupyter-settings.xml
new file mode 100644
index 0000000..15b238b
--- /dev/null
+++ b/.idea/jupyter-settings.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="JupyterPersistentConnectionParameters">
+    <option name="moduleParameters">
+      <map>
+        <entry key="G:/Andere Computer/Arbeits-Laptop/Documents/2_mmWave/2_workspace/mmWaveProject/.idea/mmWaveProject.iml">
+          <value>
+            <JupyterConnectionParameters>
+              <option name="managed" value="true" />
+            </JupyterConnectionParameters>
+          </value>
+        </entry>
+        <entry key="G:/Meine Ablage/Dokumente/TU Graz/PhD Elektrotechnik/1_workspace/2d-to-3d/.idea/2d-to-3d.iml">
+          <value>
+            <JupyterConnectionParameters>
+              <option name="managed" value="true" />
+            </JupyterConnectionParameters>
+          </value>
+        </entry>
+        <entry key="G:/Meine Ablage/Dokumente/TU Graz/PhD Elektrotechnik/1_workspace/mmWave_lense_optimization/.idea/mmWaveProject.iml">
+          <value>
+            <JupyterConnectionParameters>
+              <option name="managed" value="true" />
+            </JupyterConnectionParameters>
+          </value>
+        </entry>
+      </map>
+    </option>
+  </component>
+</project>
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 554019a..004e8ce 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -3,5 +3,5 @@
   <component name="Black">
     <option name="sdkName" value="Python 3.11 (.venv312_al) (4)" />
   </component>
-  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (confighandler)" project-jdk-type="Python SDK" />
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (confighandler)" project-jdk-type="Python SDK" />
 </project>
\ No newline at end of file
diff --git a/examples/ApplicationConfig.py b/examples/ApplicationConfig.py
index 6f06733..fafceda 100644
--- a/examples/ApplicationConfig.py
+++ b/examples/ApplicationConfig.py
@@ -8,8 +8,8 @@ from LaserConfig import LaserConfig
 
 class ApplicationConfig(cfg.ConfigNode):
 
-    def __init__(self) -> None:
-        super().__init__()
+    def __init__(self, path="./configs") -> None:
+        super().__init__(path=path)
 
         self.output_directory: cfg.Field[Path] = cfg.Field(Path("C:\\{wafer_nr}"),
                                                            friendly_name="Output Directory",
@@ -40,7 +40,7 @@ class ApplicationConfig(cfg.ConfigNode):
                                                      friendly_name="wafer_list",
                                                      description="The version of the wafer")
 
-        #self.laser_config: LaserConfig = LaserConfig()
+        self.laser_config: LaserConfig = LaserConfig(path='configs')
 
 
         self.register()
diff --git a/examples/LaserConfig.py b/examples/LaserConfig.py
index 1e473d4..3d04c6b 100644
--- a/examples/LaserConfig.py
+++ b/examples/LaserConfig.py
@@ -3,8 +3,8 @@ import confighandler as cfg
 
 class LaserConfig(cfg.ConfigNode):
 
-    def __init__(self) -> None:
-        super().__init__()
+    def __init__(self, path=None) -> None:
+        super().__init__(path=path)
         self.wavelength_range = cfg.Field(850)
         self.velocity = cfg.Field(2.0)
         self.acceleration = cfg.Field(1.0)
diff --git a/pyproject.toml b/pyproject.toml
index 3f1ff49..6338df0 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [project]
 name = "confighandler"
-version = "0.0.2"
+version = "0.0.3"
 authors = [
   { name="Christoph Schmidt", email="cschmidt.fs@gmail.com" },
 ]
diff --git a/src/confighandler/controller/ConfigNode.py b/src/confighandler/controller/ConfigNode.py
index 47d3aa8..8ce4970 100644
--- a/src/confighandler/controller/ConfigNode.py
+++ b/src/confighandler/controller/ConfigNode.py
@@ -11,21 +11,21 @@ import logging
 
 import yaml
 
-from confighandler.controller.CSignal import CSignal
-from confighandler.controller.Field import Field
-from confighandler.view.ConfigView import ConfigView
+from .CSignal import CSignal
+from .Field import Field
+from ..view.ConfigView import ConfigView
+
+import pathlib
 
 
 class ConfigNode(object):
     field_changed = CSignal()
 
-
     cur_time = datetime.datetime.now()
 
-
-
-    def __init__(self):
+    def __init__(self, path: str = None):
         super().__init__()
+
         self.name = self.__class__.__name__
         self.logger = logging.getLogger(self.name)
         self.owner = None
@@ -35,11 +35,21 @@ class ConfigNode(object):
 
         self.fields = {}
         self.configs = {}
-        self.keywords =  {
+        self.keywords = {
             "date": self.cur_time.strftime("%Y_%m_%d"),
             "time": self.cur_time.strftime("%H_%M"),
             "date_time": self.cur_time.strftime("%Y%m%d_%H%M")
         }
+
+        if path is None:
+            self._path = pathlib.Path(".")
+        else:
+            self._path = pathlib.Path(path)
+            # Check if the path exists otherwise create it
+            if not self._path.exists():
+                self._path.mkdir(parents=True, exist_ok=True)
+
+
         self.field_changed.connect(self._on_field_changed)
 
     # ==================================================================================================================
@@ -121,9 +131,9 @@ class ConfigNode(object):
     def _register_field(self):
         for attr, val in self.__dict__.items():
             if isinstance(val, Field):
-                #val.__set_name__(self.__class__.__name__, attr)
+                # val.__set_name__(self.__class__.__name__, attr)
                 self.fields[attr] = val
-                #val.register(self.keywords, self.view.keywords_changed)
+                # val.register(self.keywords, self.view.keywords_changed)
                 val.register(self.__class__.__name__, attr,
                              self.keywords, self.field_changed)
         self.view.keywords_changed.emit(self.keywords)
@@ -140,11 +150,11 @@ class ConfigNode(object):
     # ==================================================================================================================
     # I/O Operations
     # ==================================================================================================================
-    def save(self, file: str, background_save = True):
+    def save(self, file: str, background_save=True):
         # write the string to a file
         with open(file, 'w+') as stream:
             stream.write(self.serialize())
-            #print(self.serialize())
+            # print(self.serialize())
         if not background_save:
             self.logger.debug(f"Saved config to {file}")
         # with open(file, 'w+') as stream:
@@ -156,19 +166,20 @@ class ConfigNode(object):
     def load(self, file: str):
         # load the yaml file
         with open(file, 'r') as stream:
-            conent = yaml.load(stream, Loader=yaml.FullLoader)
-        self.deserialize(conent)
+            content = yaml.load(stream, Loader=yaml.FullLoader)
+        self.deserialize(content)
 
     # ==================================================================================================================
     # Functions that happens on a change
     # ==================================================================================================================
     def _on_field_changed(self):
         # Emit that a field has changed, thus the keywords have changed
-        #print(f"Field changed {self.keywords}")
+        # print(f"Field changed {self.keywords}")
         for attr, val in self.fields.items():
             val: Field
             val._on_keyword_changed()
 
         if self._level == 0:
-            #print(f"Saving config {self.name}")
-            self.save("config.yaml", background_save=True)
\ No newline at end of file
+            # print(f"Saving config {self.name}")
+            file = f"{self._path}/{self.__class__.__name__}.yaml"
+            self.save(file=file, background_save=True)
-- 
GitLab