Skip to content

Commit e95682b

Browse files
committed
improved start_app with some match/case stuff
1 parent 6f48e7b commit e95682b

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

appdaemon/app_management.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -355,17 +355,25 @@ async def start_app(self, app_name: str):
355355
Args:
356356
app (str): Name of the app to start
357357
"""
358-
if self.app_config[app_name].disable:
359-
self.logger.debug(f"Skip starting disabled app: '{app_name}'")
360-
return
358+
match self.app_config.root.get(app_name):
359+
case AppConfig() as app_cfg:
360+
if app_cfg.disable:
361+
self.logger.debug(f"Skip starting disabled app: '{app_name}'")
362+
return
363+
case GlobalModule():
364+
self.logger.warning('Global modules cannot be started')
365+
return
366+
case None:
367+
self.logger.error('App %s not found in app_config', app_name)
368+
return
361369

362370
# first we check if running already
363371
if self.is_app_running(app_name):
364372
self.logger.warning(f"Cannot start app {app_name}, as it is already running")
365373
return
366374

367375
# assert dependencies
368-
dependencies = self.app_config.root[app_name].dependencies
376+
dependencies = app_cfg.dependencies
369377
for dep_name in dependencies:
370378
rel_path = self.app_rel_path(app_name)
371379
exc_args = (
@@ -374,19 +382,27 @@ async def start_app(self, app_name: str):
374382
dep_name,
375383
dependencies
376384
)
377-
if (dep_cfg := self.app_config.root.get(dep_name)):
378-
match dep_cfg:
379-
case AppConfig():
380-
# There is a valid app configuration for this dependency
381-
if not (obj := self.objects.get(dep_name)) or not obj.running:
382-
# If the object isn't in the self.objects dict or it's there, but not running
385+
match self.app_config.root.get(dep_name):
386+
case AppConfig():
387+
# There is a valid app configuration for this dependency
388+
match self.objects.get(dep_name):
389+
case ManagedObject(type="app") as obj:
390+
# There is an app being managed that matches the dependency
391+
if not obj.running:
392+
# But it's not running, so raise an exception
393+
raise ade.DependencyNotRunning(*exc_args)
394+
case _:
395+
# TODO: make this a different exception
383396
raise ade.DependencyNotRunning(*exc_args)
384-
case GlobalModule():
385-
module = dep_cfg.module_name
386-
if module not in sys.modules:
387-
raise ade.GlobalNotLoaded(*exc_args)
388-
else:
389-
raise ade.AppDependencyError(*exc_args)
397+
case GlobalModule() as dep_cfg:
398+
# The dependency is a legacy global module
399+
module = dep_cfg.module_name
400+
if module not in sys.modules:
401+
# The module hasn't been loaded, so raise an exception
402+
raise ade.GlobalNotLoaded(*exc_args)
403+
case _:
404+
# There was no valid configuration for the dependency
405+
raise ade.AppDependencyError(*exc_args)
390406

391407
try:
392408
await self.initialize_app(app_name)

0 commit comments

Comments
 (0)