mirror of
https://github.com/Casvt/MIND.git
synced 2026-04-25 03:00:20 -04:00
Small backend refactoring dealing with None values
This commit is contained in:
@@ -8,7 +8,10 @@ import logging
|
||||
from enum import Enum
|
||||
from os.path import abspath, dirname, join
|
||||
from sys import version_info
|
||||
from typing import Any, Callable, TypeVar, Union
|
||||
|
||||
T = TypeVar('T')
|
||||
U = TypeVar('U')
|
||||
|
||||
def folder_path(*folders) -> str:
|
||||
"""Turn filepaths relative to the project folder into absolute paths
|
||||
@@ -51,6 +54,23 @@ def search_filter(query: str, result: dict) -> bool:
|
||||
)
|
||||
|
||||
|
||||
def when_not_none(value: T, to_run: Callable[[T], U]) -> Union[U, None]:
|
||||
"""Run `to_run` with argument `value` iff `value is not None`. Else return
|
||||
`None`.
|
||||
|
||||
Args:
|
||||
value (T): The value to check.
|
||||
to_run (Callable[[T], U]): The function to run.
|
||||
|
||||
Returns:
|
||||
Union[U, None]: Either the return value of `to_run`, or `None`.
|
||||
"""
|
||||
if value is None:
|
||||
return None
|
||||
else:
|
||||
return to_run(value)
|
||||
|
||||
|
||||
class Singleton(type):
|
||||
_instances = {}
|
||||
def __call__(cls, *args, **kwargs):
|
||||
|
||||
@@ -9,14 +9,14 @@ from apprise import Apprise
|
||||
from backend.custom_exceptions import (NotificationServiceInUse,
|
||||
NotificationServiceNotFound)
|
||||
from backend.db import get_db
|
||||
from backend.helpers import when_not_none
|
||||
|
||||
remove_named_groups = compile(r'(?<=\()\?P<\w+>')
|
||||
|
||||
def process_regex(regex: Union[List[str], None]):
|
||||
return (
|
||||
[remove_named_groups.sub('', regex[0]), regex[1]]
|
||||
if regex is not None else
|
||||
None
|
||||
def process_regex(regex: Union[List[str], None]) -> Union[None, List[str]]:
|
||||
return when_not_none(
|
||||
regex,
|
||||
lambda r: [remove_named_groups.sub('', r[0]), r[1]]
|
||||
)
|
||||
|
||||
def _sort_tokens(t: dict) -> int:
|
||||
|
||||
@@ -14,7 +14,7 @@ from backend.custom_exceptions import (InvalidKeyValue, InvalidTime,
|
||||
NotificationServiceNotFound,
|
||||
ReminderNotFound)
|
||||
from backend.db import get_db
|
||||
from backend.helpers import RepeatQuantity, Singleton, SortingMethod, search_filter
|
||||
from backend.helpers import RepeatQuantity, Singleton, SortingMethod, search_filter, when_not_none
|
||||
|
||||
|
||||
def __next_selected_day(
|
||||
@@ -265,10 +265,10 @@ class Reminder:
|
||||
'text': text,
|
||||
'repeat_quantity': repeat_quantity,
|
||||
'repeat_interval': repeat_interval,
|
||||
'weekdays':
|
||||
",".join(map(str, sorted(weekdays)))
|
||||
if weekdays is not None else
|
||||
None,
|
||||
'weekdays': when_not_none(
|
||||
weekdays,
|
||||
lambda w: ",".join(map(str, sorted(w)))
|
||||
),
|
||||
'color': color
|
||||
}
|
||||
for k, v in new_values.items():
|
||||
@@ -279,9 +279,10 @@ class Reminder:
|
||||
data[k] = v
|
||||
|
||||
# Update database
|
||||
rq = (data["repeat_quantity"].value
|
||||
if data["repeat_quantity"] is not None else
|
||||
None)
|
||||
rq = when_not_none(
|
||||
data["repeat_quantity"],
|
||||
lambda q: q.value
|
||||
)
|
||||
if repeated_reminder:
|
||||
next_time = _find_next_time(
|
||||
data["time"],
|
||||
@@ -556,11 +557,14 @@ class Reminders:
|
||||
else:
|
||||
original_time = None
|
||||
|
||||
if weekdays is not None:
|
||||
weekdays = ",".join(map(str, sorted(weekdays)))
|
||||
|
||||
if repeat_quantity is not None:
|
||||
repeat_quantity = repeat_quantity.value
|
||||
weekdays = when_not_none(
|
||||
weekdays,
|
||||
lambda w: ",".join(map(str, sorted(w)))
|
||||
)
|
||||
repeat_quantity = when_not_none(
|
||||
repeat_quantity,
|
||||
lambda q: q.value
|
||||
)
|
||||
|
||||
cursor.connection.isolation_level = None
|
||||
cursor.execute("BEGIN TRANSACTION;")
|
||||
@@ -716,13 +720,15 @@ class ReminderHandler(metaclass=Singleton):
|
||||
# Set next time
|
||||
new_time = _find_next_time(
|
||||
reminder['original_time'],
|
||||
RepeatQuantity(reminder['repeat_quantity'])
|
||||
if reminder['repeat_quantity'] is not None else
|
||||
None,
|
||||
when_not_none(
|
||||
reminder["repeat_quantity"],
|
||||
lambda q: RepeatQuantity(q)
|
||||
),
|
||||
reminder['repeat_interval'],
|
||||
[int(d) for d in reminder['weekdays'].split(',')]
|
||||
if reminder['weekdays'] is not None else
|
||||
None
|
||||
when_not_none(
|
||||
reminder["weekdays"],
|
||||
lambda w: [int(d) for d in w.split(',')]
|
||||
)
|
||||
)
|
||||
cursor.execute(
|
||||
"UPDATE reminders SET time = ? WHERE id = ?;",
|
||||
|
||||
Reference in New Issue
Block a user