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

feat: provide modules for config, errors and textgrid api

a `TextgridConfig` class that reads its values from os env; a `EnvNotSetError` that can be raised on config errors; a `TextgridAuth` class that speaks to the textgrid authentication service
parent e1f70d19
No related branches found
No related tags found
No related merge requests found
import logging
import os
from .errors import EnvNotSetError
_logger = logging.getLogger(__name__)
def lookup_env_name(internal_name: str) -> str:
mapping = {
"_auth_wsdl": "tg_auth_wsdl",
"_auth_address": "tg_auth_address",
}
return mapping[internal_name]
class TextgridConfig:
def __init__(self):
self._auth_wsdl = os.getenv(lookup_env_name("_auth_wsdl"))
self._auth_address = os.getenv(lookup_env_name("_auth_address"))
@property
def auth_wsdl(self):
if self._auth_wsdl:
_logger.debug(self._auth_wsdl)
return self._auth_wsdl
raise EnvNotSetError(lookup_env_name("_auth_wsdl"))
@property
def auth_address(self):
if self._auth_address:
return self._auth_address
raise EnvNotSetError(lookup_env_name("_auth_address"))
"""Custom error classes
"""
import logging
#from wsgidav import dav_error
_logger = logging.getLogger(__name__)
class _ConfigError(Exception):
"""Base class for config errors. Do not import directly.
"""
pass
class EnvNotSetError(_ConfigError):
"""Exception to raise if an environment variable is not set (correctly).
"""
def __init__(self, env_name):
self.env_name = env_name
self.message = "Environment variable '%s' is not set."
_logger.error(self.message, self.env_name)
super().__init__(self.message)
#from typing import List
from zeep import Client
from zeep.exceptions import TransportError
from .config import TextgridConfig
class TextgridAuth:
def __init__(self):
self._config = TextgridConfig()
def _connect(self) -> Client:
"""Internal helper that provides a SOAP client that is configured for
the use with the Textgrid Auth service.
Returns:
Client: A SOAP client.
"""
client = Client(self._config.auth_wsdl)
# this is dirty hack; should be remediated
client.service._binding_options["address"] = self._config.auth_address
return client
# replace ":" with " -> List | None:" when switching to python3.10
def assigned_projects(self, sid: str):
"""Return an array of project id strings
"""
client = self._connect()
try:
return client.service.tgAssignedProjects(sid)
except TransportError:
return None
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