add file handler stub
This commit is contained in:
parent
2e52cc3e6c
commit
a061e489dd
35
file_handler.py
Normal file
35
file_handler.py
Normal file
@ -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 = []
|
Loading…
Reference in New Issue
Block a user