Lars Noack
8255ad5264
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
from typing import Type, Generator, Set, Dict, List
|
|
from collections import defaultdict
|
|
|
|
from ._encyclopaedia_metallum import EncyclopaediaMetallum
|
|
from ._musify import Musify
|
|
from ._youtube import YouTube
|
|
from ._youtube_music import YoutubeMusic
|
|
from ._bandcamp import Bandcamp
|
|
from ._genius import Genius
|
|
from ._abstract import Page, INDEPENDENT_DB_OBJECTS
|
|
|
|
|
|
_registered_pages: Dict[Type[Page], Set[Page]] = defaultdict(set)
|
|
|
|
|
|
def get_pages(*page_types: List[Type[Page]]) -> Generator[Page, None, None]:
|
|
if len(page_types) == 0:
|
|
page_types = _registered_pages.keys()
|
|
|
|
for page_type in page_types:
|
|
yield from _registered_pages[page_type]
|
|
|
|
|
|
def register_page(page_type: Type[Page], **kwargs):
|
|
if page_type in _registered_pages:
|
|
return
|
|
|
|
_registered_pages[page_type].add(page_type(**kwargs))
|
|
|
|
|
|
def deregister_page(page_type: Type[Page]):
|
|
if page_type not in _registered_pages:
|
|
return
|
|
|
|
for p in _registered_pages[page_type]:
|
|
p.__del__()
|
|
del _registered_pages[page_type]
|
|
|
|
def scan_for_pages():
|
|
# assuming the wanted pages are the leaf classes of the interface
|
|
leaf_classes = []
|
|
|
|
_class_list = [Page]
|
|
while len(_class_list):
|
|
_class = _class_list.pop()
|
|
_class_subclasses = _class.__subclasses__()
|
|
|
|
if len(_class_subclasses) == 0:
|
|
if _class.REGISTER:
|
|
leaf_classes.append(_class)
|
|
else:
|
|
_class_list.extend(_class_subclasses)
|
|
|
|
print(leaf_classes)
|
|
for leaf_class in leaf_classes:
|
|
register_page(leaf_class)
|