예제 #1
0
def test_exit_with_error_missing_interface(capsys):
    with pytest.raises(SystemExit) as wrapped_error:
        exit_with_error(ErrorCode.MissingInterface, ['one', ['two']])
    captured = capsys.readouterr()

    assert wrapped_error.value.code == ErrorCode.MissingInterface.value
    assert captured.out == 'Configured interface \'one\' was not found on this host.\nAvailable interfaces are: two.\nExiting\n'
예제 #2
0
def test_exit_with_error_interface_address(capsys):
    with pytest.raises(SystemExit) as wrapped_error:
        exit_with_error(ErrorCode.InterfaceAddress, 'details')
    captured = capsys.readouterr()

    assert wrapped_error.value.code == ErrorCode.InterfaceAddress.value
    assert captured.out == 'Configured interface \'details\' does not have an IPv4 address\nExiting\n'
예제 #3
0
def test_exit_with_error_empty_key(capsys):
    with pytest.raises(SystemExit) as wrapped_error:
        exit_with_error(ErrorCode.EmptyKey, 'details')
    captured = capsys.readouterr()

    assert wrapped_error.value.code == ErrorCode.EmptyKey.value
    assert captured.out == '\'details\' key must contain at least one item\nExiting\n'
예제 #4
0
def test_exit_with_error_camera_connect(capsys):
    with pytest.raises(SystemExit) as wrapped_error:
        exit_with_error(ErrorCode.CameraConnect, 'details')
    captured = capsys.readouterr()

    assert wrapped_error.value.code == ErrorCode.CameraConnect.value
    assert captured.out == 'Camera \'details\' could not be opened.\nExiting\n'
예제 #5
0
def test_exit_with_error_missing_config(capsys):
    with pytest.raises(SystemExit) as wrapped_error:
        exit_with_error(ErrorCode.MissingConfig, 'details')
    captured = capsys.readouterr()

    assert wrapped_error.value.code == ErrorCode.MissingConfig.value
    assert captured.out == 'A required config file could not be found: details\nExiting\n'
예제 #6
0
def test_exit_with_error_missing_key(capsys):
    with pytest.raises(SystemExit) as wrapped_error:
        exit_with_error(ErrorCode.MissingKey, 'details')
    captured = capsys.readouterr()

    assert wrapped_error.value.code == ErrorCode.MissingKey.value
    assert captured.out == '\'details\' key does not exist in the config\nExiting\n'
예제 #7
0
    def setup_max_subjects(self):
        """
    Defines the maximum tracked subjects from config
    """

        if 'max_subjects' in self.config:
            self.max_subjects = self.config['max_subjects']
        else:
            exit_with_error(ErrorCode.MissingKey, 'max_subjects')
예제 #8
0
    def setup_calibration(self):
        """
    Creates the calibration object from config
    """

        if 'calibration' in self.config:
            self.calibration = Calibration(self.config['calibration'])
        else:
            exit_with_error(ErrorCode.MissingKey, 'calibration')
예제 #9
0
    def setup_room(self):
        """
    Defines the room from config
    """

        if 'room' in self.config:
            room_json = self.config['room']
            self.room = Room(room_json['x'], room_json['y'], room_json['z'])
        else:
            exit_with_error(ErrorCode.MissingKey, 'room')
예제 #10
0
    def setup_fixtures(self):
        """
    Creates the fixture structure from config
    """

        if 'fixtures' in self.config:
            if len(self.config['fixtures']) < 1:
                exit_with_error(ErrorCode.EmptyKey, 'fixtures')

            for fixture_id, fixture_config in self.config['fixtures'].items():
                fixture = Fixture(fixture_config, fixture_id, self.stop_flags)
                addr = fixture.address
                universe = self.universes.get_universe(addr['net'],
                                                       addr['subnet'],
                                                       addr['universe'])
                if universe is None:
                    universe = Universe(addr['net'], addr['subnet'],
                                        addr['universe'])
                    self.universes.add_universe(universe)
                universe.add_fixture(fixture)
        else:
            exit_with_error(ErrorCode.MissingKey, 'fixtures')
예제 #11
0
    def init_config(self):
        """
    Initialises all backend services in a repeatable fashion, allowing Spotted
    to be partially restarted when config changes
    """

        try:
            self.config = json.load(open('spotted/config/config.json'))
            load_personalities('spotted/config/personalities.json')
        except FileNotFoundError as error:
            exit_with_error(ErrorCode.MissingConfig, error)

        self.current_state = dict()
        self.stop_flags = {'artnet': False, 'camera': False, 'fixture': False}

        self.setup_calibration()
        self.cameras = []
        if not self.skip_cameras:
            self.setup_cameras()
        self.setup_room()
        self.universes = Universes()
        self.pois = []
        self.setup_fixtures()
        self.setup_max_subjects()
예제 #12
0
    def setup_cameras(self):
        """
    Defines the cameras from config
    """

        if 'cameras' in self.config:
            if len(self.config['cameras']) < 1:
                exit_with_error(ErrorCode.EmptyKey, 'cameras')

            for camera_id, camera in self.config['cameras'].items():
                cam = Camera(camera, camera_id, self.calibration,
                             self.stop_flags)
                self.cameras.append(cam)
                self.config['cameras'][camera_id][
                    'initial_point'] = cam.initial_point.as_dict()
        else:
            exit_with_error(ErrorCode.MissingKey, 'cameras')

        for camera in self.cameras:
            if not camera.capture.isOpened():
                exit_with_error(ErrorCode.CameraConnect, camera.cam_id)
예제 #13
0
    def setup_interface(self):
        """
    Validates configured interfaces against available system interfaces and
    returns the first IPv4 address of the given interface
    """

        host_interfaces = netifaces.interfaces()

        if 'network_interface' in self.config:
            if self.config['network_interface'] in host_interfaces:
                addresses = netifaces.ifaddresses(
                    self.config['network_interface'])
                if netifaces.AF_INET in addresses:
                    address = addresses[netifaces.AF_INET][0]
                    self.ip_address = address['addr']
                    self.broadcast_address = address['broadcast']
                else:
                    exit_with_error(ErrorCode.InterfaceAddress,
                                    self.config['network_interface'])
            else:
                details = (self.config['network_interface'], host_interfaces)
                exit_with_error(ErrorCode.MissingInterface, details)
        else:
            exit_with_error(ErrorCode.MissingKey, 'network_interface')