From efb5498167271803d49c7443b44876b5318e58e8 Mon Sep 17 00:00:00 2001
From: bgeels <bgeels@usgs.gov>
Date: Tue, 22 Oct 2024 15:01:19 -0600
Subject: [PATCH] Make fcntl package optional in Util.py

---
 geomagio/Util.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/geomagio/Util.py b/geomagio/Util.py
index d1cb0a73..e6b74ed9 100644
--- a/geomagio/Util.py
+++ b/geomagio/Util.py
@@ -4,7 +4,12 @@ from obspy.core import Stats, Trace
 from obspy import UTCDateTime
 from io import BytesIO
 import json
-import fcntl
+import warnings
+
+try:
+    import fcntl
+except:
+    pass
 
 
 class ObjectView(object):
@@ -270,6 +275,10 @@ def write_state_file(filename, data, directory=None, encoder=None):
                 fcntl.flock(f, fcntl.LOCK_EX)
                 json.dump(data, f, default=encoder)
                 fcntl.flock(f, fcntl.LOCK_UN)
+            except NameError as e:
+                print(
+                    f"The fcntl module is not supported in Windows. Reading/writing state files will not work: {e}"
+                )
             except IOError as e:
                 print(f"Error locking or writing to file: {e}")
                 raise
@@ -281,6 +290,9 @@ def write_state_file(filename, data, directory=None, encoder=None):
         raise
 
 
+write_state_file("testjunkfile", "dataaaaa", "/home/bgeels/")
+
+
 def read_state_file(filename, directory=None, decoder=None):
     """
     Reads data from a state file in a thread-safe manner.
@@ -318,6 +330,10 @@ def read_state_file(filename, directory=None, decoder=None):
                 data = json.load(f, object_hook=decoder)
                 fcntl.flock(f, fcntl.LOCK_UN)
                 return data
+            except NameError as e:
+                print(
+                    f"The fcntl module is not supported in Windows. Reading/writing state files will not work: {e}"
+                )
             except IOError as e:
                 print(f"Error locking or reading from file: {e}")
                 raise
-- 
GitLab