Skip to content

Commit

Permalink
Add lots of static typing
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Garcia <[email protected]>
  • Loading branch information
luigi311 committed Nov 9, 2024
1 parent 53ee540 commit f6c68bc
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 154 deletions.
91 changes: 41 additions & 50 deletions src/black_white.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@


def setup_black_white_lists(
blacklist_library: str,
whitelist_library: str,
blacklist_library_type: str,
whitelist_library_type: str,
blacklist_users: str,
whitelist_users: str,
library_mapping=None,
user_mapping=None,
blacklist_library: list[str] | None,
whitelist_library: list[str] | None,
blacklist_library_type: list[str] | None,
whitelist_library_type: list[str] | None,
blacklist_users: list[str] | None,
whitelist_users: list[str] | None,
library_mapping: dict[str, str] | None = None,
user_mapping: dict[str, str] | None = None,
):
blacklist_library, blacklist_library_type, blacklist_users = setup_x_lists(
blacklist_library,
Expand Down Expand Up @@ -40,53 +40,44 @@ def setup_black_white_lists(


def setup_x_lists(
xlist_library,
xlist_library_type,
xlist_users,
xlist_type,
library_mapping=None,
user_mapping=None,
):
xlist_library: list[str] | None,
xlist_library_type: list[str] | None,
xlist_users: list[str] | None,
xlist_type: str | None,
library_mapping: dict[str, str] | None = None,
user_mapping: dict[str, str] | None = None,
) -> tuple[list[str], list[str], list[str]]:
out_library: list[str] = []
if xlist_library:
if len(xlist_library) > 0:
xlist_library = xlist_library.split(",")
xlist_library = [x.strip() for x in xlist_library]
if library_mapping:
temp_library = []
for library in xlist_library:
library_other = search_mapping(library_mapping, library)
if library_other:
temp_library.append(library_other)
out_library = [x.strip() for x in xlist_library]
if library_mapping:
temp_library: list[str] = []
for library in xlist_library:
library_other = search_mapping(library_mapping, library)
if library_other:
temp_library.append(library_other)

xlist_library = xlist_library + temp_library
else:
xlist_library = []
logger(f"{xlist_type}list Library: {xlist_library}", 1)
out_library = out_library + temp_library
logger(f"{xlist_type}list Library: {xlist_library}", 1)

out_library_type: list[str] = []
if xlist_library_type:
if len(xlist_library_type) > 0:
xlist_library_type = xlist_library_type.split(",")
xlist_library_type = [x.lower().strip() for x in xlist_library_type]
else:
xlist_library_type = []
logger(f"{xlist_type}list Library Type: {xlist_library_type}", 1)
out_library_type = [x.lower().strip() for x in xlist_library_type]

logger(f"{xlist_type}list Library Type: {out_library_type}", 1)

out_users: list[str] = []
if xlist_users:
if len(xlist_users) > 0:
xlist_users = xlist_users.split(",")
xlist_users = [x.lower().strip() for x in xlist_users]
if user_mapping:
temp_users = []
for user in xlist_users:
user_other = search_mapping(user_mapping, user)
if user_other:
temp_users.append(user_other)
out_users = [x.lower().strip() for x in xlist_users]
if user_mapping:
temp_users: list[str] = []
for user in out_users:
user_other = search_mapping(user_mapping, user)
if user_other:
temp_users.append(user_other)

out_users = out_users + temp_users

xlist_users = xlist_users + temp_users
else:
xlist_users = []
else:
xlist_users = []
logger(f"{xlist_type}list Users: {xlist_users}", 1)
logger(f"{xlist_type}list Users: {out_users}", 1)

return xlist_library, xlist_library_type, xlist_users
return out_library, out_library_type, out_users
30 changes: 18 additions & 12 deletions src/connection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from typing import Literal
from dotenv import load_dotenv

from src.functions import logger, str_to_bool
Expand All @@ -9,24 +10,26 @@
load_dotenv(override=True)


def jellyfin_emby_server_connection(server_baseurl, server_token, server_type):
servers = []
def jellyfin_emby_server_connection(
server_baseurl: str, server_token: str, server_type: Literal["jellyfin", "emby"]
) -> list[tuple[Literal["jellyfin", "emby"], Jellyfin | Emby]]:
servers: list[tuple[Literal["jellyfin", "emby"], Jellyfin | Emby]] = []

server_baseurl = server_baseurl.split(",")
server_token = server_token.split(",")
server_baseurls = server_baseurl.split(",")
server_tokens = server_token.split(",")

if len(server_baseurl) != len(server_token):
if len(server_baseurls) != len(server_tokens):
raise Exception(
f"{server_type.upper()}_BASEURL and {server_type.upper()}_TOKEN must have the same number of entries"
)

for i, baseurl in enumerate(server_baseurl):
for i, baseurl in enumerate(server_baseurls):
baseurl = baseurl.strip()
if baseurl[-1] == "/":
baseurl = baseurl[:-1]

if server_type == "jellyfin":
server = Jellyfin(baseurl=baseurl, token=server_token[i].strip())
server = Jellyfin(baseurl=baseurl, token=server_tokens[i].strip())
servers.append(
(
"jellyfin",
Expand All @@ -35,7 +38,7 @@ def jellyfin_emby_server_connection(server_baseurl, server_token, server_type):
)

elif server_type == "emby":
server = Emby(baseurl=baseurl, token=server_token[i].strip())
server = Emby(baseurl=baseurl, token=server_tokens[i].strip())
servers.append(
(
"emby",
Expand All @@ -50,8 +53,12 @@ def jellyfin_emby_server_connection(server_baseurl, server_token, server_type):
return servers


def generate_server_connections():
servers = []
def generate_server_connections() -> (
list[tuple[Literal["plex", "jellyfin", "emby"], Plex | Jellyfin | Emby]]
):
servers: list[
tuple[Literal["plex", "jellyfin", "emby"], Plex | Jellyfin | Emby]
] = []

plex_baseurl = os.getenv("PLEX_BASEURL", None)
plex_token = os.getenv("PLEX_TOKEN", None)
Expand Down Expand Up @@ -120,7 +127,6 @@ def generate_server_connections():

jellyfin_baseurl = os.getenv("JELLYFIN_BASEURL", None)
jellyfin_token = os.getenv("JELLYFIN_TOKEN", None)

if jellyfin_baseurl and jellyfin_token:
servers.extend(
jellyfin_emby_server_connection(
Expand All @@ -130,8 +136,8 @@ def generate_server_connections():

emby_baseurl = os.getenv("EMBY_BASEURL", None)
emby_token = os.getenv("EMBY_TOKEN", None)

if emby_baseurl and emby_token:

servers.extend(
jellyfin_emby_server_connection(emby_baseurl, emby_token, "emby")
)
Expand Down
2 changes: 1 addition & 1 deletion src/emby.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from src.jellyfin_emby import JellyfinEmby
from packaging.version import (parse, Version)
from packaging.version import parse, Version


class Emby(JellyfinEmby):
Expand Down
18 changes: 14 additions & 4 deletions src/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def log_marked(

# Reimplementation of distutils.util.strtobool due to it being deprecated
# Source: https://github.com/PostHog/posthog/blob/01e184c29d2c10c43166f1d40a334abbc3f99d8a/posthog/utils.py#L668
def str_to_bool(value: any) -> bool:
def str_to_bool(value: str) -> bool:
if not value:
return False
return str(value).lower() in ("y", "yes", "t", "true", "on", "1")
Expand All @@ -84,7 +84,7 @@ def contains_nested(element, lst):


# Get mapped value
def search_mapping(dictionary: dict, key_value: str):
def search_mapping(dictionary: dict[str, str], key_value: str) -> str | None:
if key_value in dictionary.keys():
return dictionary[key_value]
elif key_value.lower() in dictionary.keys():
Expand All @@ -100,8 +100,10 @@ def search_mapping(dictionary: dict, key_value: str):


# Return list of objects that exist in both lists including mappings
def match_list(list1, list2, list_mapping=None):
output = []
def match_list(
list1: list[str], list2: list[str], list_mapping: dict[str, str] | None = None
) -> list[str]:
output: list[str] = []
for element in list1:
if element in list2:
output.append(element)
Expand Down Expand Up @@ -146,3 +148,11 @@ def future_thread_executor(
raise Exception(e)

return results


def parse_string_to_list(string: str | None) -> list[str]:
output: list[str] = []
if string and len(string) > 0:
output = string.split(",")

return output
2 changes: 1 addition & 1 deletion src/jellyfin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from src.jellyfin_emby import JellyfinEmby
from packaging.version import (parse, Version)
from packaging.version import parse, Version


class Jellyfin(JellyfinEmby):
Expand Down
Loading

0 comments on commit f6c68bc

Please sign in to comment.