From a061e489dd7e7fd93c6f40daee7dcf1900edcb7e Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan.grewe@g-node.org>
Date: Sat, 26 Dec 2020 13:51:53 +0100
Subject: [PATCH] add file handler stub

---
 file_handler.py | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
 create mode 100644 file_handler.py

diff --git a/file_handler.py b/file_handler.py
new file mode 100644
index 0000000..bd83908
--- /dev/null
+++ b/file_handler.py
@@ -0,0 +1,35 @@
+import os
+import nixio as nix
+from nixio import file
+
+class FileHandler():
+    
+    def __init__(self) -> None:
+        super().__init__()
+        self._nix_file = None
+        self._file_requests = []
+    
+    def open(self, filename):
+        """[summary]
+
+        Args:
+            filename ([type]): [description]
+        """
+        print("open file: %s" % filename)
+        self.close()
+      
+        if not os.path.exists(filename):
+            return False, "File %s could not be found!" % filename
+        try:
+            self._nix_file = nix.File.open(filename, nix.FileMode.ReadOnly)
+            return True, "Successfully opened file %s." % filename.split(os.sep)[-1]
+        except RuntimeError as e:
+            return False, "Failed to open file %s! \n Error message is: %s" % (filename, e) 
+
+    def close(self):
+        print("close file")
+        # TODO check if there are any open file requests!
+        if self._nix_file is not None and self._nix_file.is_open():
+            self._nix_file.close()
+            self._nix_file = None
+            self._file_requests = []
\ No newline at end of file