Skip to content
Snippets Groups Projects
Verified Commit 5bef02de authored by Ubbo Veentjer's avatar Ubbo Veentjer
Browse files

refactor: refactor dav providers to duplicate less code

parent a1fac5fd
No related branches found
No related tags found
1 merge request!17Resolve "refactor dav and named_dav providers to duplicate less code"
...@@ -172,6 +172,7 @@ class TextgridAggregation(DAVCollection): ...@@ -172,6 +172,7 @@ class TextgridAggregation(DAVCollection):
self._info = info self._info = info
self._tgconfig = TextgridConfig() self._tgconfig = TextgridConfig()
self._tgsearch = TextgridSearch(self._tgconfig.search) self._tgsearch = TextgridSearch(self._tgconfig.search)
self._tguri = self.path.split("/")[-1]
def create_empty_resource(self, name): def create_empty_resource(self, name):
pass pass
...@@ -184,7 +185,7 @@ class TextgridAggregation(DAVCollection): ...@@ -184,7 +185,7 @@ class TextgridAggregation(DAVCollection):
def get_member_names(self): def get_member_names(self):
_logger.debug("Called TextgridAggregation.get_member_names(self).") _logger.debug("Called TextgridAggregation.get_member_names(self).")
response = self._tgsearch.list_aggregation(self.path.split("/")[-1], self._sid) response = self._tgsearch.list_aggregation(self._tguri, self._sid)
names = [] names = []
for result in response.result: for result in response.result:
names.append(result.object_value.generic.generated.textgrid_uri.value) names.append(result.object_value.generic.generated.textgrid_uri.value)
...@@ -252,8 +253,11 @@ class TextgridResource(DAVNonCollection): ...@@ -252,8 +253,11 @@ class TextgridResource(DAVNonCollection):
DAVNonCollection.__init__(self, path, environ) DAVNonCollection.__init__(self, path, environ)
self._size = environ.get("CONTENT_LENGTH") self._size = environ.get("CONTENT_LENGTH")
self._sid = environ["wsgidav.auth.user_name"] self._sid = environ["wsgidav.auth.user_name"]
self._tguri = self.path.split("/")[-1]
self._info = info self._info = info
self.upload_thread = None self.upload_thread = None
config = TextgridConfig()
self._crud = TextgridCRUD(config.crud)
def get_content_length(self): def get_content_length(self):
_logger.debug("Called TextgridResource.get_content_length(self).") _logger.debug("Called TextgridResource.get_content_length(self).")
...@@ -265,9 +269,7 @@ class TextgridResource(DAVNonCollection): ...@@ -265,9 +269,7 @@ class TextgridResource(DAVNonCollection):
def get_content(self): 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)
config = TextgridConfig() return io.BytesIO(self._crud.read_data(self._tguri, self._sid).content)
crud = TextgridCRUD(config.crud)
return io.BytesIO(crud.read_data(self.path.split("/")[-1], self._sid).content)
def get_content_title(self): def get_content_title(self):
_logger.debug("Called TextgridResource.get_content_title(self).") _logger.debug("Called TextgridResource.get_content_title(self).")
...@@ -279,13 +281,11 @@ class TextgridResource(DAVNonCollection): ...@@ -279,13 +281,11 @@ class TextgridResource(DAVNonCollection):
) )
queue = FileLikeQueue(int(self._size)) queue = FileLikeQueue(int(self._size))
config = TextgridConfig() metadata = self._crud.read_metadata(self._tguri, self._sid).content
crud = TextgridCRUD(config.crud)
metadata = crud.read_metadata(self.path.split("/")[-1], self._sid).content
def worker(): def worker():
_logger.debug("Called TextgridResource.begin_write.worker().") _logger.debug("Called TextgridResource.begin_write.worker().")
crud.update_resource(self._sid, self.path.split("/")[-1], queue, metadata) self._crud.update_resource(self._sid, self._tguri, queue, metadata)
thread = threading.Thread(target=worker) thread = threading.Thread(target=worker)
thread.setDaemon(True) thread.setDaemon(True)
......
import io
import logging import logging
import threading
from pprint import pformat from pprint import pformat
from tgclients.config import TextgridConfig from tgclients.config import TextgridConfig
...@@ -73,14 +71,8 @@ class TextgridNamedProject(TextgridProject): ...@@ -73,14 +71,8 @@ class TextgridNamedProject(TextgridProject):
def get_member_names(self): def get_member_names(self):
_logger.debug("Called TextgridNamedProject.get_member_names(self).") _logger.debug("Called TextgridNamedProject.get_member_names(self).")
# names = []
# the item keys are textgrid uris but we want the resource titles only
# for _, member_dict in self._resources.items():
# names.append(member_dict.get("title"))
# return names
#
# path resolution has to be rewritten before we can work with resource titles
name = self.path.split("/")[-1] name = self.path.split("/")[-1]
# the projectID is found between square brackets at the end of the name string
project_id = name[name.rfind("[") + 1 : name.rfind("]")] project_id = name[name.rfind("[") + 1 : name.rfind("]")]
response = self._tgsearch.list_project_root(project_id, self._sid) response = self._tgsearch.list_project_root(project_id, self._sid)
names = [] names = []
...@@ -116,11 +108,11 @@ class TextgridNamedAggregation(TextgridAggregation): ...@@ -116,11 +108,11 @@ class TextgridNamedAggregation(TextgridAggregation):
) )
super().__init__(path, environ, info) super().__init__(path, environ, info)
self._tgmeta = tgmeta self._tgmeta = tgmeta
self._tguri = "textgrid:" + self._tgmeta.id_from_filename(self.path.split("/")[-1])
def get_member_names(self): def get_member_names(self):
_logger.debug("Called TextgridNamedAggregation.get_member_names(self).") _logger.debug("Called TextgridNamedAggregation.get_member_names(self).")
tguri = "textgrid:" + self._tgmeta.id_from_filename(self.path.split("/")[-1]) response = self._tgsearch.list_aggregation(self._tguri, self._sid)
response = self._tgsearch.list_aggregation(tguri, self._sid)
names = [] names = []
for result in response.result: for result in response.result:
names.append(self._tgmeta.filename_from_metadata(result)) names.append(self._tgmeta.filename_from_metadata(result))
...@@ -154,33 +146,4 @@ class TextgridNamedResource(TextgridResource): ...@@ -154,33 +146,4 @@ class TextgridNamedResource(TextgridResource):
_logger.debug("Called TextgridNamedResource.__init__(self, %s, environ).", path) _logger.debug("Called TextgridNamedResource.__init__(self, %s, environ).", path)
super().__init__(path, environ, info) super().__init__(path, environ, info)
self._tgmeta = tgmeta self._tgmeta = tgmeta
config = TextgridConfig() self._tguri = "textgrid:" + self._tgmeta.id_from_filename(self.path.split("/")[-1])
self._crud = TextgridCRUD(config.crud)
def get_content(self):
_logger.debug(
"Called TextgridNamedResource.get_content(self) with path: %s", self.path
)
# TODO: make tguri (super!) class member
tguri = "textgrid:" + self._tgmeta.id_from_filename(self.path.split("/")[-1])
return io.BytesIO(self._crud.read_data(tguri, self._sid).content)
def begin_write(self, content_type=None):
_logger.debug(
"Called TextgridResource.begin_write(self, content_type=%s).", content_type
)
queue = FileLikeQueue(int(self._size))
tguri = "textgrid:" + self._tgmeta.id_from_filename(self.path.split("/")[-1])
metadata = self._crud.read_metadata(tguri, self._sid).content
def worker():
_logger.debug("Called TextgridResource.begin_write.worker().")
self._crud.update_resource(self._sid, tguri, queue, metadata)
thread = threading.Thread(target=worker)
thread.setDaemon(True)
thread.start()
self.upload_thread = thread
return queue
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment