Skip to content

Commit cfdfa42

Browse files
committed
extend API
1 parent 3d45a7f commit cfdfa42

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

native_fisher_py/python/native_fisher_py/__init__.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def close_raw_file(): pass
4545
if os.path.exists(_lib_path) and "THERMO_NATIVE_LIB" not in os.environ:
4646
os.environ["THERMO_NATIVE_LIB"] = _lib_path
4747

48+
from .exceptions import RawFileException
49+
4850
class RawFile(object):
4951
"""
5052
A high-level wrapper to provide a drop-in replacement for fisher_py.RawFile
@@ -57,9 +59,18 @@ def __init__(self, path: str):
5759
path: Path to the .raw file.
5860
"""
5961
self._path = path
62+
self._is_open = False
63+
self._is_error = False
64+
65+
if not os.path.isfile(path):
66+
raise FileNotFoundError(f'No raw file with path "{path}" found.')
67+
6068
res = open_raw_file(path)
61-
if res != 0:
62-
raise RuntimeError(f"Could not open RAW file: {path}")
69+
if res == 0:
70+
self._is_open = True
71+
else:
72+
self._is_error = True
73+
raise RawFileException(f"Could not open RAW file: {path}")
6374

6475
@staticmethod
6576
def file_factory(path: str):
@@ -103,6 +114,26 @@ def last_scan(self) -> int:
103114
"""Last scan number."""
104115
return get_last_scan()
105116

117+
@property
118+
def is_open(self) -> bool:
119+
"""Check if the file was successfully opened."""
120+
return self._is_open
121+
122+
@property
123+
def is_error(self) -> bool:
124+
"""Check if the last operation caused an error."""
125+
return self._is_error
126+
127+
@property
128+
def in_acquisition(self) -> bool:
129+
"""Check if file is still being acquired."""
130+
return False
131+
132+
@property
133+
def has_ms_data(self) -> bool:
134+
"""Check if file contains MS data."""
135+
return True
136+
106137
@property
107138
def total_time_min(self) -> float:
108139
"""Total acquisition time in minutes."""
@@ -224,6 +255,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
224255
def close(self):
225256
"""Close the file and release the reader resources."""
226257
close_raw_file()
258+
self._is_open = False
227259

228260
class MSOrder:
229261
Ms = 1
@@ -249,8 +281,8 @@ class TraceType:
249281
from . import native_fisher_py_backend
250282
__doc__ = native_fisher_py_backend.__doc__
251283
if hasattr(native_fisher_py_backend, "__all__"):
252-
__all__ = native_fisher_py_backend.__all__ + ["RawFile", "MSOrder", "MassAnalyzer", "TraceType"]
284+
__all__ = native_fisher_py_backend.__all__ + ["RawFile", "MSOrder", "MassAnalyzer", "TraceType", "RawFileException"]
253285
else:
254-
__all__ = ["RawFile", "MSOrder", "MassAnalyzer", "TraceType"]
286+
__all__ = ["RawFile", "MSOrder", "MassAnalyzer", "TraceType", "RawFileException"]
255287
else:
256-
__all__ = ["RawFile", "MSOrder", "MassAnalyzer", "TraceType"]
288+
__all__ = ["RawFile", "MSOrder", "MassAnalyzer", "TraceType", "RawFileException"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class RawFileException(Exception):
2+
"""Base exception for native_fisher_py errors."""
3+
pass

0 commit comments

Comments
 (0)