Skip to content

Home Information - Your home, your data, organized.

Python
2.3 KB
Raw Blame
import logging

from django.apps import apps
from django.urls import include, path, re_path

from hi.apps.common.module_utils import import_module_safe

from . import views

logger = logging.getLogger(__name__)


urlpatterns = [
    path( '',
          views.ServicesIndexView.as_view(),
          name = 'simulator_services' ),

    path( 'tab/<slug:simulator_id>/',
          views.ServiceView.as_view(),
          name = 'simulator_service' ),

    path( 'api/states/',
          views.SimStatesView.as_view(),
          name = 'simulator_api_states' ),

    re_path( r'^entity/add/(?P<simulator_id>[\w_\-\.\:]+)/(?P<class_id>[\w\.\_]+)$',
             views.SimEntityAddView.as_view(),
             name = 'simulator_entity_add' ),

    path( 'entity/edit/<int:sim_entity_id>',
          views.SimEntityEditView.as_view(),
          name = 'simulator_entity_edit' ),

    path( 'entity/delete/<int:sim_entity_id>',
          views.SimEntityDeleteView.as_view(),
          name = 'simulator_entity_delete' ),

    re_path( r'^entity/state/set/(?P<simulator_id>[\w_\-\.\:]+)'
             r'/(?P<sim_entity_id>\d+)/(?P<sim_state_id>[\w\-]+)$',
             views.SimStateSetView.as_view(),
             name = 'simulator_entity_state_set' ),

    re_path( r'^fault-mode/set/(?P<simulator_id>[\w_\-\.\:]+)$',
             views.SetServiceFaultModeView.as_view(),
             name = 'simulator_fault_mode_set' ),
]


def discover_service_urls():
    """ Add urls (if any) from all simulated integration services """
    discovered_url_modules = dict()
    for app_config in apps.get_app_configs():
        if not app_config.name.startswith( 'hi.simulator.services.' ):
            continue
        module_name = f'{app_config.name}.urls'
        short_name = app_config.name.split('.')[-1]
        try:
            urls_module = import_module_safe( module_name = module_name )
            if not urls_module:
                continue
            discovered_url_modules[short_name] = urls_module
        except Exception:
            logger.exception( f'Problem importing URL module: {module_name}' )
            pass
        continue
    return discovered_url_modules


for short_name, urls_module in discover_service_urls().items():
    urlpatterns.append(
        re_path( f"{short_name}/", include( urls_module ))
    )
    continue