def _create_autocompleter_system(files_only, directories_only, handler_type_cls, get_host_func):
    def local_handler(func):
        class ChildHandler(SCPHandler):
            is_leaf = True

            def __init__(self, shell, path):
                self.path = path
                SCPHandler.__init__(self, shell)

            @property
            def handler_type(self):
                host = get_host_func(self.shell)
                if self.path in ('..', '.', '/') or host.stat(self.path).is_dir:
                    return DirectoryType()
                else:
                    return FileType()

            def __call__(self):
                func(self.shell, self.path)

        class MainHandler(SCPHandler):
            handler_type = handler_type_cls()

            def complete_subhandlers(self, part):
                host = get_host_func(self.shell)
                # Progress bar.
                for f in host.listdir():
                    if f.startswith(part):
                        if files_only and not host.stat(f).is_file:
                            continue
                        if directories_only and not host.stat(f).is_dir:
                            continue

                        yield f, ChildHandler(self.shell, f)

                # Root directory.
                if '/'.startswith(part) and not files_only:
                    yield f, ChildHandler(self.shell, '/')

            def get_subhandler(self, name):
                host = get_host_func(self.shell)

                # First check whether this name appears in the current directory.
                # (avoids stat calls on unknown files.)
                if name in host.listdir():
                    # When this file does not exist, return
                    try:
                        s = host.stat(name)
                        if (files_only and not s.is_file):
                            return
                        if (directories_only and not s.is_dir):
                            return
                    except __HOLE__: # stat on non-existing file.
                        return
                    finally:
                        return ChildHandler(self.shell, name)

                # Root, current and parent directory.
                if name in ('/', '..', '.') and not files_only:
                    return ChildHandler(self.shell, name)

        return MainHandler
    return local_handler
dataset/ETHPy150Open jonathanslenders/python-deployer/deployer/scp_shell.py/_create_autocompleter_system