Skip to content

Commit

Permalink
Use a registry abc instead of concrete class
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavcs committed Jan 11, 2021
1 parent 75ed305 commit f1274c2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
11 changes: 5 additions & 6 deletions lib/galaxy/tours/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
from pydantic import parse_obj_as

from galaxy import util
from galaxy.tours.schema import (
TourDetails,
TourList,
)
from galaxy.tours import abc
from galaxy.tours.schema import TourList


log = logging.getLogger(__name__)
Expand All @@ -31,12 +29,13 @@ def load_steps(contents_dict):
step['title'] = title_default


@abc.ToursRegistry.register
class ToursRegistry:
def __init__(self, tour_directories):
self.tour_directories = util.config_directories_from_setting(tour_directories)
self.load_tours()

def tours_by_id_with_description(self) -> TourList:
def tours_by_id_with_description(self):
tours = []
for k in self.tours.keys():
tourdata = {
Expand Down Expand Up @@ -70,7 +69,7 @@ def reload_tour(self, path):
if self._is_yaml(filename):
self._load_tour_from_path(path)

def tour_contents(self, tour_id) -> TourDetails:
def tour_contents(self, tour_id):
# Extra format translation could happen here (like the previous intro_to_tour)
# For now just return the loaded contents.
return self.tours.get(tour_id, None)
Expand Down
21 changes: 21 additions & 0 deletions lib/galaxy/tours/abc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from abc import ABC, abstractmethod

from galaxy.tours.schema import (
TourDetails,
TourList,
)


class ToursRegistry(ABC):

@abstractmethod
def tours_by_id_with_description(self) -> TourList:
"""Return list of tours."""

@abstractmethod
def tour_contents(self, tour_id: str) -> TourDetails:
"""Return tour details."""

@abstractmethod
def load_tour(self, tour_id: str) -> TourDetails:
"""Reload tour and return tour details."""
5 changes: 5 additions & 0 deletions lib/galaxy/webapps/galaxy/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from galaxy.managers.session import GalaxySessionManager
from galaxy.managers.users import UserManager
from galaxy.model import User
from galaxy.tours.abc import ToursRegistry
from galaxy.web.framework.decorators import require_admin_message
from galaxy.work.context import SessionRequestContext

Expand Down Expand Up @@ -85,3 +86,7 @@ def get_admin_user(trans: SessionRequestContext = Depends(get_trans)):
if not trans.user_is_admin:
raise AdminRequiredException(require_admin_message(trans.app.config, trans.user))
return trans.user


def get_tours_registry(app: UniverseApplication = Depends(get_app)) -> ToursRegistry:
return app.tour_registry
12 changes: 6 additions & 6 deletions lib/galaxy/webapps/galaxy/api/tours.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from fastapi_utils.cbv import cbv
from fastapi_utils.inferring_router import InferringRouter as APIRouter

from galaxy.app import UniverseApplication
from galaxy.tours.abc import ToursRegistry
from galaxy.tours.schema import (
TourDetails,
TourList,
Expand All @@ -20,7 +20,7 @@
from galaxy.webapps.base.controller import BaseAPIController
from . import (
get_admin_user,
get_app,
get_tours_registry,
)

log = logging.getLogger(__name__)
Expand All @@ -31,22 +31,22 @@

@cbv(router)
class FastAPITours:
app: UniverseApplication = Depends(get_app)
registry: ToursRegistry = Depends(get_tours_registry)

@router.get('/api/tours')
def index(self) -> TourList:
"""Return list of available tours."""
return self.app.tour_registry.tours_by_id_with_description()
return self.registry.tours_by_id_with_description()

@router.get('/api/tours/{tour_id}')
def show(self, tour_id: str) -> TourDetails:
"""Return a tour definition."""
return self.app.tour_registry.tour_contents(tour_id)
return self.registry.tour_contents(tour_id)

@router.post('/api/tours/{tour_id}', dependencies=[Depends(get_admin_user)])
def update_tour(self, tour_id: str) -> TourDetails:
"""Return a tour definition."""
return self.app.tour_registry.load_tour(tour_id)
return self.registry.load_tour(tour_id)


class ToursController(BaseAPIController):
Expand Down

0 comments on commit f1274c2

Please sign in to comment.