1
0
mirror of synced 2024-11-24 22:30:10 +01:00
artemis/core/utils.py

36 lines
1.1 KiB
Python
Raw Normal View History

2023-02-24 20:13:31 +01:00
from typing import Dict, Any
2023-02-16 23:13:41 +01:00
from types import ModuleType
from twisted.web.http import Request
2023-02-24 20:13:31 +01:00
import logging
2023-02-16 23:13:41 +01:00
import importlib
from os import walk
2023-03-09 17:38:58 +01:00
2023-02-16 23:13:41 +01:00
class Utils:
@classmethod
def get_all_titles(cls) -> Dict[str, ModuleType]:
ret: Dict[str, Any] = {}
for root, dirs, files in walk("titles"):
2023-03-09 17:38:58 +01:00
for dir in dirs:
2023-02-16 23:13:41 +01:00
if not dir.startswith("__"):
try:
mod = importlib.import_module(f"titles.{dir}")
if hasattr(mod, "game_codes") and hasattr(
mod, "index"
): # Minimum required to function
2023-04-15 06:12:45 +02:00
ret[dir] = mod
2023-02-16 23:13:41 +01:00
except ImportError as e:
2023-02-24 20:13:31 +01:00
logging.getLogger("core").error(f"get_all_titles: {dir} - {e}")
raise
2023-02-16 23:13:41 +01:00
return ret
@classmethod
def get_ip_addr(cls, req: Request) -> str:
return (
req.getAllHeaders()[b"x-forwarded-for"].decode()
if b"x-forwarded-for" in req.getAllHeaders()
else req.getClientAddress().host
)