# Module `lib.tools.starter`

Allows automatic component start without editing python scripts. This searches for startup.py files in lib directory and runs the 'startup' function

Expand source code

    # Distributed under Pycameresp License
    # Copyright (c) 2023 Remi BERTHOLET
    """ Allows automatic component start without editing python scripts.
    This searches for __startup__.py files in lib directory and runs the 'startup' function """
    import tools.logger
    import tools.filesystem
    import tools.tasking

    starter_kwargs = None
    class Starter:
            """ Automatic component start without editing python script """
            @staticmethod
            async def task(**kwargs):
                    """ Starter of components """
                    global starter_kwargs
                    directories,filenames = await tools.filesystem.ascandir("lib","__startup__.py",True)
                    for startup in filenames:
                            startup = startup.replace("lib/","")
                            startup = startup.replace(".py","")
                            startup = startup.replace("/",".")

                            try:
                                    # pylint:disable=consider-using-f-string
                                    exec("import %s"%startup)
                                    starter_kwargs = kwargs
                                    exec("%s.startup(**starter_kwargs)"%startup)
                                    starter_kwargs = None
                                    tools.logger.syslog("Start component %s"%startup[:-12])
                            except Exception as err:
                                    tools.logger.syslog(err)

            @staticmethod
            def start(**kwargs):
                    """ Start module started """
                    tools.tasking.Tasks.create_task(Starter.task(**kwargs))

## Classes

`class Starter`

  Automatic component start without editing python script

  Expand source code

      class Starter:
              """ Automatic component start without editing python script """
              @staticmethod
              async def task(**kwargs):
                      """ Starter of components """
                      global starter_kwargs
                      directories,filenames = await tools.filesystem.ascandir("lib","__startup__.py",True)
                      for startup in filenames:
                              startup = startup.replace("lib/","")
                              startup = startup.replace(".py","")
                              startup = startup.replace("/",".")

                              try:
                                      # pylint:disable=consider-using-f-string
                                      exec("import %s"%startup)
                                      starter_kwargs = kwargs
                                      exec("%s.startup(**starter_kwargs)"%startup)
                                      starter_kwargs = None
                                      tools.logger.syslog("Start component %s"%startup[:-12])
                              except Exception as err:
                                      tools.logger.syslog(err)

              @staticmethod
              def start(**kwargs):
                      """ Start module started """
                      tools.tasking.Tasks.create_task(Starter.task(**kwargs))

  ### Static methods

  `def start(**kwargs)`

    Start module started

    Expand source code

        @staticmethod
        def start(**kwargs):
                """ Start module started """
                tools.tasking.Tasks.create_task(Starter.task(**kwargs))

  `async def task(**kwargs)`

    Starter of components

    Expand source code

        @staticmethod
        async def task(**kwargs):
                """ Starter of components """
                global starter_kwargs
                directories,filenames = await tools.filesystem.ascandir("lib","__startup__.py",True)
                for startup in filenames:
                        startup = startup.replace("lib/","")
                        startup = startup.replace(".py","")
                        startup = startup.replace("/",".")

                        try:
                                # pylint:disable=consider-using-f-string
                                exec("import %s"%startup)
                                starter_kwargs = kwargs
                                exec("%s.startup(**starter_kwargs)"%startup)
                                starter_kwargs = None
                                tools.logger.syslog("Start component %s"%startup[:-12])
                        except Exception as err:
                                tools.logger.syslog(err)