Skip to content
Snippets Groups Projects
Verified Commit 5ad6f4d2 authored by Stefan Hynek's avatar Stefan Hynek :drooling_face:
Browse files

feat(textgrid_dav_provider): implement `begin_write` for `TextgridResource`s

uses a `FileLikeQueue` of chunks to streamed directly to the target
parent 8bc370b8
No related branches found
No related tags found
1 merge request!8Resolve "integrate with tgclients lib"
...@@ -3,11 +3,16 @@ ...@@ -3,11 +3,16 @@
import io import io
import logging import logging
from pprint import pformat
from tgclients.crud import TextgridCRUD
from tgclients.config import TextgridConfig
from tgclients.metadata import TextgridMetadata
from wsgidav.dav_provider import DAVCollection, DAVNonCollection, DAVProvider from wsgidav.dav_provider import DAVCollection, DAVNonCollection, DAVProvider
from wsgidav.stream_tools import FileLikeQueue
from wsgidav.util import join_uri, pop_path from wsgidav.util import join_uri, pop_path
from .tgapi import TextgridAuth, TextgridSearch
from .tgapi import TextgridAuth, TextgridCRUD, TextgridSearch
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
...@@ -23,7 +28,6 @@ class TextgridRoot(DAVCollection): ...@@ -23,7 +28,6 @@ class TextgridRoot(DAVCollection):
def __init__(self, path, environ): def __init__(self, path, environ):
DAVCollection.__init__(self, path, environ) DAVCollection.__init__(self, path, environ)
self._sid = environ["wsgidav.auth.user_name"] self._sid = environ["wsgidav.auth.user_name"]
_logger.debug("MY SID: %s", self._sid)
def get_display_info(self): def get_display_info(self):
return {"type": "Textgrid root collection"} return {"type": "Textgrid root collection"}
...@@ -85,7 +89,7 @@ class TextgridProject(DAVCollection): ...@@ -85,7 +89,7 @@ class TextgridProject(DAVCollection):
# path resolution has to be rewritten before we can work with resource titles # path resolution has to be rewritten before we can work with resource titles
resources = TextgridSearch().get_project_contents( resources = TextgridSearch().get_project_contents(
self._sid, self.path.split("/")[-1]) self._sid, self.path.split("/")[-1])
_logger.debug("RESOURCES: %s", resources) #_logger.debug("RESOURCES: %s", resources)
return resources.keys() return resources.keys()
def get_member(self, name): def get_member(self, name):
...@@ -192,19 +196,48 @@ class TextgridResource(DAVNonCollection): ...@@ -192,19 +196,48 @@ class TextgridResource(DAVNonCollection):
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).")
# return TextgridCRUD().get_metadata(self._sid, self.path.split("/")[-1])["content-length"]
return self._info[self.name]["extent"] return self._info[self.name]["extent"]
def get_content_type(self): def get_content_type(self):
_logger.debug("Called TextgridResource.get_content_type(self).") _logger.debug("Called TextgridResource.get_content_type(self).")
# return TextgridCRUD().get_metadata(self._sid, self.path.split("/")[-1])["content-type"]
return self._info[self.name]["format"] return self._info[self.name]["format"]
def get_content(self): def get_content(self):
return io.BytesIO(TextgridCRUD().get_data(self._sid, self.path.split("/")[-1])) config = TextgridConfig()
crud = TextgridCRUD(config.crud)
return io.BytesIO(crud.read_data(self.path.split("/")[-1], self._sid).content)
def get_content_title(self):
_logger.debug("Called TextgridResource.get_content_title(self).")
return self._info[self.name]["title"]
def begin_write(self, content_type=None): def begin_write(self, content_type=None):
pass _logger.debug(
"Called TextgridResource.begin_write(self, content_type=%s).", content_type)
config = TextgridConfig()
crud = TextgridCRUD(config.crud)
queue = FileLikeQueue(max_size=1)
# create metadata or update?
metadata = TextgridMetadata.create(
self.get_content_title(), self.get_content_type())
crud.update_resource(
self._sid, self.path.split("/")[-1], queue, metadata)
return queue
def end_write(self, with_errors):
_logger.debug(
"Called TextgridResource.end_write(self, with_errors=%s)", with_errors)
# temporary override for debugging
def resolve(self, script_name, path_info):
"""Return a _DAVResource object for the path (None, if not found).
`path_info`: is a URL relative to this object.
"""
_logger.debug(
"Called TextgridResource.resolve(self, %s, %s).", script_name, path_info)
if path_info in ("", "/"):
return self
return None
# ============================================================================ # ============================================================================
...@@ -214,12 +247,9 @@ class TextgridResourceProvider(DAVProvider): ...@@ -214,12 +247,9 @@ class TextgridResourceProvider(DAVProvider):
"""DAV provider that serves Textgrid resources. """DAV provider that serves Textgrid resources.
""" """
def __init__(self):
super(TextgridResourceProvider, self).__init__()
def get_resource_inst(self, path, environ): def get_resource_inst(self, path, environ):
_logger.debug( _logger.debug(
"Called TextgridResourceProvider.get_resource_inst(self, %s, %s).", path, environ) "Called TextgridResourceProvider.get_resource_inst(self, %s, %s).", path, pformat(environ))
self._count_get_resource_inst += 1 self._count_get_resource_inst += 1
root = TextgridRoot("/", environ) root = TextgridRoot("/", environ)
# an instance of _DAVResource (i.e. either DAVCollection or DAVNonCollection) # an instance of _DAVResource (i.e. either DAVCollection or DAVNonCollection)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment