From 3c05e9831bc23f1b51b2401c85874e83fccbfa0c Mon Sep 17 00:00:00 2001
From: Chad Hanna <chad.hanna@ligo.org>
Date: Tue, 12 Mar 2019 06:29:53 -0700
Subject: [PATCH] inspiral.py: support offline gracedb mode where a file:// url
 causes the output to be written to disk

---
 gstlal-inspiral/python/inspiral.py | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/gstlal-inspiral/python/inspiral.py b/gstlal-inspiral/python/inspiral.py
index 78e260248d..ac4ac5e6f0 100644
--- a/gstlal-inspiral/python/inspiral.py
+++ b/gstlal-inspiral/python/inspiral.py
@@ -60,6 +60,7 @@ import time
 import httplib
 import tempfile
 import os
+import urlparse
 
 from glue import iterutils
 from ligo.lw import ligolw
@@ -395,6 +396,25 @@ class CoincsDocument(object):
 #
 
 
+class FakeGracedbResp(object):
+	def __init__(self):
+		self.status = httplib.CREATED
+	def json(self):
+		return {"graceid": -1}
+
+
+class FakeGracedbClient(object):
+	def __init__(self, service_url):
+		# Assumes that service url is a directory to write files to
+		self.path = urlparse.urlparse(service_url).path
+	def createEvent(self, group, pipeline, filename, filecontents, search):
+		with open(os.path.join(self.path, filename), "w") as f:
+			f.write(filecontents)
+		return FakeGracedbResp()
+	def writeLog(self, gracedb_id, message, filename, filecontents, tagname):
+		return FakeGracedbResp()
+
+
 class GracedBWrapper(object):
 	retries = 5
 	retry_delay = 5.0
@@ -430,7 +450,10 @@ class GracedBWrapper(object):
 		if far_threshold is None or far_threshold < 0.:
 			self.gracedb_client = None
 		else:
-			self.gracedb_client = gracedb.rest.GraceDb(self.service_url)
+			if self.service_url.startswith("file"):
+				self.gracedb_client = FakeGracedbClient(self.service_url)
+			else:
+				self.gracedb_client = gracedb.rest.GraceDb(self.service_url)
 
 	def web_get_gracedb_far_threshold(self):
 		with self.lock:
-- 
GitLab