<commit_before>import dronekit
import time
import os
import threading
import houston.sandbox
from houston.mission import Mission
from typing import Optional


class NoConnectionError(BaseException):
    """
    Thrown when there is no connection to the vehicle running inside a sandbox.
    """
    def __init__(self):
        super().__init__("No connection to vehicle inside sandbox.")


class Sandbox(houston.sandbox.Sandbox):
    def __init__(self, system: 'ardu.BaseSystem') -> None:
        super().__init__(system)
        self.__connection = None
        self.__sitl_thread = None

    @property
    def connection(self,
                   raise_exception: bool = True
                   ) -> Optional[dronekit.Vehicle]:
        """
        Uses dronekit to return a connection to the system running inside
        this sandbox.

        Raises:
            NoConnectionError: if there is no connection to the system running
                inside this sandbox.
        """
        if self.__connection is None and raise_exception:
            raise NoConnectionError()
        return self.__connection

    def _launch_sitl(self, binary_name: str = "ardurover", model_name: str = "rover", param_file: str = "", verbose: bool = True) -> None:
        """
        Launches the SITL inside the sandbox and blocks until its execution
        has finished.
        """
        # TOOD: use param_files
        home = "-35.362938,149.165085,584,270"
        cmd = 'build/sitl/bin/{} --model "{}" --speedup "{}" --home "{}"'
        cmd = cmd.format(binary_name, model_name, self.system.speedup, home)

        if not verbose:
            self.bugzoo.command(cmd, stdout=False, stderr=False, block=False)
            return

        execution_response = self.bugzoo.command(cmd, stdout=True, stderr=True, block=False)
        for line in execution_response.output:
            line = line.decode(sys.stdout.encoding).rstrip('\n')
            print(line, flush=True)

    def _start(self,
               mission: Mission,
               binary_name: str,
               model_name: str,
               param_file: str,
               verbose: bool = True
               ) -> None:
        """
        Launches the SITL inside this sandbox, and establishes a connection to
        the vehicle running inside the simulation. Blocks until SITL is launched
        and a connection is established.
        """
        # launch SITL
        self.__sitl_thread = threading.Thread(target=self._launch_sitl,
                                              args=(binary_name, model_name,
                                              param_file, verbose))
        self.__sitl_thread.daemon = True
        self.__sitl_thread.start()

        # establish connection
        # TODO fix connection issue
        protocol = 'tcp'
        port = 5760
        url = "{}:{}:{}".format(protocol, str(self.bugzoo.ip_address), port)
        time.sleep(10)
        dummy_connection = mavutil.mavlink_connection(url)
        time.sleep(10)
        dummy_connection.close()
        time.sleep(5)
        self.__connection = dronekit.connect(url, wait_ready=True)

        # wait until vehicle is ready to test
        self.__connection.wait_ready('autopilot_version')<commit_msg>More imports<commit_after>import dronekit
import time
import os
import sys
import threading
import houston.sandbox
from houston.mission import Mission
from typing import Optional


class NoConnectionError(BaseException):
    """
    Thrown when there is no connection to the vehicle running inside a sandbox.
    """
    def __init__(self):
        super().__init__("No connection to vehicle inside sandbox.")


class Sandbox(houston.sandbox.Sandbox):
    def __init__(self, system: 'ardu.BaseSystem') -> None:
        super().__init__(system)
        self.__connection = None
        self.__sitl_thread = None

    @property
    def connection(self,
                   raise_exception: bool = True
                   ) -> Optional[dronekit.Vehicle]:
        """
        Uses dronekit to return a connection to the system running inside
        this sandbox.

        Raises:
            NoConnectionError: if there is no connection to the system running
                inside this sandbox.
        """
        if self.__connection is None and raise_exception:
            raise NoConnectionError()
        return self.__connection

    def _launch_sitl(self, binary_name: str = "ardurover", model_name: str = "rover", param_file: str = "", verbose: bool = True) -> None:
        """
        Launches the SITL inside the sandbox and blocks until its execution
        has finished.
        """
        # TOOD: use param_files
        home = "-35.362938,149.165085,584,270"
        cmd = 'build/sitl/bin/{} --model "{}" --speedup "{}" --home "{}"'
        cmd = cmd.format(binary_name, model_name, self.system.speedup, home)

        if not verbose:
            self.bugzoo.command(cmd, stdout=False, stderr=False, block=False)
            return

        execution_response = self.bugzoo.command(cmd, stdout=True, stderr=True, block=False)
        for line in execution_response.output:
            line = line.decode(sys.stdout.encoding).rstrip('\n')
            print(line, flush=True)

    def _start(self,
               mission: Mission,
               binary_name: str,
               model_name: str,
               param_file: str,
               verbose: bool = True
               ) -> None:
        """
        Launches the SITL inside this sandbox, and establishes a connection to
        the vehicle running inside the simulation. Blocks until SITL is launched
        and a connection is established.
        """
        # launch SITL
        self.__sitl_thread = threading.Thread(target=self._launch_sitl,
                                              args=(binary_name, model_name,
                                              param_file, verbose))
        self.__sitl_thread.daemon = True
        self.__sitl_thread.start()

        # establish connection
        # TODO fix connection issue
        from pymavlink import mavutil
        protocol = 'tcp'
        port = 5760
        url = "{}:{}:{}".format(protocol, str(self.bugzoo.ip_address), port)
        time.sleep(10)
        dummy_connection = mavutil.mavlink_connection(url)
        time.sleep(10)
        dummy_connection.close()
        time.sleep(5)
        self.__connection = dronekit.connect(url, wait_ready=True)

        # wait until vehicle is ready to test
        self.__connection.wait_ready('autopilot_version')
{'fraction_non_alphanumeric': 0.09029896278218426, 'fraction_numerical': 0.009762050030506406, 'mean_word_length': 2.069756554307116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6308493', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1741, 'n_words': 492}