Skip to content

Commit

Permalink
Merge pull request #166 from ArtrenH/main
Browse files Browse the repository at this point in the history
2023-09-22 2
  • Loading branch information
Belissimo-T authored Sep 22, 2023
2 parents fb8364d + 7cd3012 commit 5a9e89e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
63 changes: 63 additions & 0 deletions backend/creds_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import abc
import json
import logging
from pathlib import Path

import pymongo.database
from dotenv.main import DotEnv, find_dotenv


class CredsProvider(abc.ABC):
@abc.abstractmethod
def get_creds(self) -> dict:
pass


class FileCredsProvider(CredsProvider):
def __init__(self, path: Path):
self.path = path

def get_creds(self) -> dict:
with open(self.path, "r", encoding="utf-8") as f:
return json.load(f)


class MongoDbCredsProvider(CredsProvider):
def __init__(self, collection: pymongo.database.Collection):
self.collection = collection

def get_creds(self) -> dict:
pipeline = [
{
"$sort": {"count": pymongo.DESCENDING}
},
{
"$project": {
"count": False,
"comment": False,
}
}
]

out = self.collection.aggregate(pipeline)

return {elem["short_name"]: elem for elem in out}


def creds_provider_factory(creds_filepath: Path | None) -> CredsProvider:
dotenv = DotEnv(dotenv_path=find_dotenv())

if (mongo_uri := dotenv.get("MONGO_URL")) is not None:
collection = pymongo.MongoClient(mongo_uri).get_database("vplan").get_collection("creds")
logging.info("Using MongoDB as credentials source.")
return MongoDbCredsProvider(collection)
else:
logging.debug("No MONGO_URI found in .env file.")

if creds_filepath.exists():
logging.info(f"Using {creds_filepath!r} as credentials source.")
return FileCredsProvider(creds_filepath)
else:
logging.debug(f"Creds file {creds_filepath!r} does not exist.")

raise FileNotFoundError(f"Could not find a credentials source.")
7 changes: 3 additions & 4 deletions backend/load_plans.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import asyncio
import logging
from pathlib import Path
import json

from backend.creds_provider import creds_provider_factory
from stundenplan24_py import (
IndiwareStundenplanerClient, Hosting, proxies
)
Expand Down Expand Up @@ -55,9 +55,8 @@ async def check_infinite(self, interval: int = 60, *, once: bool = False, ignore

async def get_clients(session: aiohttp.ClientSession | None = None,
proxy_provider: proxies.ProxyProvider | None = None) -> dict[str, PlanCrawler]:
# parse credentials
with open("creds.json", "r", encoding="utf-8") as f:
_creds = json.load(f)
creds_provider = creds_provider_factory(Path("creds.json"))
_creds = creds_provider.get_creds()

clients = {}

Expand Down
6 changes: 3 additions & 3 deletions client/src/Lesson.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
$: only_teacher_absent = lesson.scheduled_teachers?.length !== 0 && lesson.current_teachers?.length === 0 && lesson.takes_place;
$: teachers = (lesson.takes_place && !only_teacher_absent ? lesson.current_teachers : lesson.scheduled_teachers) || [];
$: s_teachers = !arraysEqual(lesson.scheduled_teachers, teachers) ? lesson.scheduled_teachers?.filter(t => !teachers.includes(t)) : [];
$: s_teachers = (!arraysEqual(lesson.scheduled_teachers, teachers) ? lesson.scheduled_teachers?.filter(t => !teachers.includes(t)) : []) || [];
$: forms = (lesson.takes_place ? lesson.current_forms : lesson.scheduled_forms) || [];
$: s_forms = !arraysEqual(lesson.scheduled_forms, forms) ? lesson.scheduled_forms?.filter(f => !forms.includes(f)) : [];
$: s_forms = (!arraysEqual(lesson.scheduled_forms, forms) ? lesson.scheduled_forms?.filter(f => !forms.includes(f)) : []) || []
$: forms_str = lesson.takes_place ? lesson.current_forms_str : lesson.scheduled_forms_str;
$: s_forms_str = s_forms.length !== 0 ? lesson.scheduled_forms_str : "";
$: rooms = (lesson.takes_place ? lesson.current_rooms : lesson.scheduled_rooms) || [];
$: s_rooms = !arraysEqual(lesson.scheduled_rooms, rooms) ? lesson.scheduled_rooms?.filter(r => !rooms.includes(r)) : [];
$: s_rooms = (!arraysEqual(lesson.scheduled_rooms, rooms) ? lesson.scheduled_rooms?.filter(r => !rooms.includes(r)) : []) || [];
$: subject_changed = lesson.subject_changed && lesson.takes_place && !lesson.is_unplanned;
$: teacher_changed = lesson.teacher_changed && lesson.takes_place && !lesson.is_unplanned;
Expand Down

0 comments on commit 5a9e89e

Please sign in to comment.