From fab21bcf60737f8d710f3e8419195ba10d20139f Mon Sep 17 00:00:00 2001
From: Stefan Hynek <stefan.hynek@uni-goettingen.de>
Date: Mon, 9 Jan 2023 10:29:59 +0100
Subject: [PATCH] feat(textgrid_dav_provider): initialize textgrid config with
 "tg_host" from wsgidav environ

this allows for configuration of the textgrid host from the environment
---
 src/repdav/textgrid_dav_provider.py | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/repdav/textgrid_dav_provider.py b/src/repdav/textgrid_dav_provider.py
index cbdf786..cc8a89a 100644
--- a/src/repdav/textgrid_dav_provider.py
+++ b/src/repdav/textgrid_dav_provider.py
@@ -21,6 +21,16 @@ from repdav.stream_tools import FileLikeQueue
 _logger = logging.getLogger(__name__)
 
 
+def tg_config(environ):
+    """Initialize Textgrid configuration either with or without host setting. This
+    works around an [issue with tgclients](https://gitlab.gwdg.de/dariah-de/textgridrep/textgrid-python-clients/-/issues/59).
+    """
+    tg_host = environ["wsgidav.config"]["tg_host"]
+    if tg_host:
+        return TextgridConfig(tg_host)
+    return TextgridConfig()
+
+
 class TextgridRoot(DAVCollection):
     """Top level collection that incorporates Textgrid projects.
 
@@ -30,8 +40,9 @@ class TextgridRoot(DAVCollection):
     def __init__(self, path, environ):
         DAVCollection.__init__(self, path, environ)
         self._sid = environ["wsgidav.auth.user_name"]
-        config = TextgridConfig()
+        config = tg_config(environ)
         self._auth = TextgridAuth(config)
+        self.projects = ()
 
     def get_display_info(self):
         return {"type": "Textgrid root collection"}
@@ -83,7 +94,7 @@ class TextgridProject(DAVCollection):
         _logger.debug("Called TextgridProject.__init__(self, %s, environ).", path)
         DAVCollection.__init__(self, path, environ)
         self._sid = environ["wsgidav.auth.user_name"]
-        config = TextgridConfig()
+        config = tg_config(environ)
         self._tgsearch = TextgridSearch(config.search)
         self._project_id = self.path.split("/")[-1]
 
@@ -175,7 +186,7 @@ class TextgridAggregation(DAVCollection):
         DAVCollection.__init__(self, path, environ)
         self._sid = environ["wsgidav.auth.user_name"]
         self._info = info
-        config = TextgridConfig()
+        config = tg_config(environ)
         self._tgsearch = TextgridSearch(config.search)
         self._tguri = self.path.split("/")[-1]
 
@@ -261,7 +272,7 @@ class TextgridResource(DAVNonCollection):
         self._tguri = self.path.split("/")[-1]
         self._info = info
         self.upload_thread = None
-        config = TextgridConfig()
+        config = tg_config(environ)
         self._crud = TextgridCRUD(config.crud)
 
     def get_content_length(self):
@@ -273,7 +284,9 @@ class TextgridResource(DAVNonCollection):
         return self._info[self.name]["format"]
 
     def get_content(self):
-        _logger.debug("Called TextgridResource.get_content(self) with path: %s", self.path)
+        _logger.debug(
+            "Called TextgridResource.get_content(self) with path: %s", self.path
+        )
         return io.BytesIO(self._crud.read_data(self._tguri, self._sid).content)
 
     def get_content_title(self):
-- 
GitLab