11# -*- coding: utf-8 -*-
2- # (c) 2014-2020 The mqttwarn developers
2+ # (c) 2014-2021 The mqttwarn developers
33from builtins import object
44from past .builtins import cmp
55from builtins import chr
@@ -532,9 +532,15 @@ def load_services(services):
532532
533533 module = cf .g ('config:' + service , 'module' , service )
534534
535+ # Load external service from file.
536+ modulefile_candidates = []
535537 if module .endswith (".py" ):
536- modulefile = module
538+ # Add two candidates: a) Use the file as given and b) treat the file as relative to
539+ # the directory of the configuration file. That retains backward compatibility.
540+ modulefile_candidates .append (module )
541+ modulefile_candidates .append (os .path .join (cf .configuration_path , module ))
537542
543+ # Load external service with module specification.
538544 elif '.' in module :
539545 logger .debug ('Trying to load service "{}" from module "{}"' .format (service , module ))
540546 try :
@@ -544,15 +550,19 @@ def load_services(services):
544550 except Exception as ex :
545551 logger .exception ('Unable to load service "{}" from module "{}": {}' .format (service , module , ex ))
546552
553+ # Load built-in service module.
547554 else :
548- modulefile = resource_filename ('mqttwarn.services' , module + '.py' )
555+ modulefile_candidates = [ resource_filename ('mqttwarn.services' , module + '.py' ) ]
549556
550- logger .debug ('Trying to load service "{}" from file "{}"' .format (service , modulefile ))
551- try :
552- service_plugins [service ]['module' ] = load_module_from_file (modulefile )
553- logger .info ('Successfully loaded service "{}"' .format (service ))
554- except Exception as ex :
555- logger .exception ('Unable to load service "{}" from file "{}": {}' .format (service , modulefile , ex ))
557+ for modulefile in modulefile_candidates :
558+ if not os .path .isfile (modulefile ):
559+ continue
560+ logger .debug ('Trying to load service "{}" from file "{}"' .format (service , modulefile ))
561+ try :
562+ service_plugins [service ]['module' ] = load_module_from_file (modulefile )
563+ logger .info ('Successfully loaded service "{}"' .format (service ))
564+ except Exception as ex :
565+ logger .exception ('Unable to load service "{}" from file "{}": {}' .format (service , modulefile , ex ))
556566
557567
558568def connect ():
0 commit comments