From 526f884c6d257ab105c34314ed854d98d261f352 Mon Sep 17 00:00:00 2001 From: Chris Allan Date: Thu, 16 Nov 2023 10:38:59 +0000 Subject: [PATCH] Allow plugins to use settings utilities There are various settings utilities included in the `omeroweb.settings` module that would be useful to use in plugins. Unfortunately, because of import order and plugin settings processing that's made impossible if they are in the same package. The `LeaveUnset` functionality is of particular interest. This moves them into `omeroweb.utils` so they can be if desired. --- omeroweb/settings.py | 55 ++++++++------------------------------------ omeroweb/utils.py | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 45 deletions(-) diff --git a/omeroweb/settings.py b/omeroweb/settings.py index 8591ab9b68..47a87caf1e 100755 --- a/omeroweb/settings.py +++ b/omeroweb/settings.py @@ -36,14 +36,22 @@ import tempfile import re import json -import pytz import random import string from builtins import str as text import portalocker from omero.util.concurrency import get_event -from omeroweb.utils import sort_properties_to_tuple +from omeroweb.utils import ( + LeaveUnset, + check_timezone, + identity, + parse_boolean, + leave_none_unset, + leave_none_unset_int, + sort_properties_to_tuple, + str_slash, +) from omeroweb.connector import Server logger = logging.getLogger(__name__) @@ -196,13 +204,6 @@ ) -def parse_boolean(s): - s = s.strip().lower() - if s in ("true", "1", "t"): - return True - return False - - def parse_paths(s): return [os.path.normpath(path) for path in json.loads(s)] @@ -224,42 +225,6 @@ def check_session_engine(s): return s -def identity(x): - return x - - -def check_timezone(s): - """ - Checks that string is a valid time-zone. If not, raise Exception - """ - pytz.timezone(s) - return s - - -def str_slash(s): - if s is not None: - s = str(s) - if s and not s.endswith("/"): - s += "/" - return s - - -class LeaveUnset(Exception): - pass - - -def leave_none_unset(s): - if s is None: - raise LeaveUnset() - return s - - -def leave_none_unset_int(s): - s = leave_none_unset(s) - if s is not None: - return int(s) - - CUSTOM_HOST = CUSTOM_SETTINGS.get("Ice.Default.Host", "localhost") CUSTOM_HOST = CUSTOM_SETTINGS.get("omero.master.host", CUSTOM_HOST) # DO NOT EDIT! diff --git a/omeroweb/utils.py b/omeroweb/utils.py index 0756b48f6f..a9bae69223 100644 --- a/omeroweb/utils.py +++ b/omeroweb/utils.py @@ -21,6 +21,8 @@ import logging +import pytz + from django.utils.http import urlencode from django.urls import reverse from django.urls import NoReverseMatch @@ -30,6 +32,49 @@ logger = logging.getLogger(__name__) +def parse_boolean(s): + s = s.strip().lower() + if s in ("true", "1", "t"): + return True + return False + + +def identity(x): + return x + + +def check_timezone(s): + """ + Checks that string is a valid time-zone. If not, raise Exception + """ + pytz.timezone(s) + return s + + +def str_slash(s): + if s is not None: + s = str(s) + if s and not s.endswith("/"): + s += "/" + return s + + +class LeaveUnset(Exception): + pass + + +def leave_none_unset(s): + if s is None: + raise LeaveUnset() + return s + + +def leave_none_unset_int(s): + s = leave_none_unset(s) + if s is not None: + return int(s) + + def is_ajax(request): """ Replicates the functionality of the Django 3.1 deprecated