Exemplo n.º 1
0
def create_recording_file_parser(spec):

    # Get parser name.
    classes = extension_manager.instance.get_extensions(
        'Recording File Parser')
    name = spec.get('name')
    if name is None:
        raise CommandExecutionError(
            'Recording file parser spec does not include parser name.')

    # Get parser class.
    cls = classes.get(name)
    if cls is None:
        raise CommandExecutionError(
            'Unrecognized recording file parser extension "{}".'.format(name))

    # Get stations.
    stations = [s for s in Station.objects.all()]

    # Get station name aliases.
    station_name_aliases = _get_station_name_aliases(spec)

    # Create parser.
    parser = cls(stations, station_name_aliases)

    return parser
Exemplo n.º 2
0
def _set_recording_file_channel_info(file):
    
    mic_outputs = _get_recorder_mic_outputs(file.recorder, file.start_time)
    
    if file.recorder_channel_nums is None:
        # file name did not indicate recorder channel numbers
        
        if len(mic_outputs) != file.num_channels:
            # number of connected mic outputs does not match number
            # of file channels
            
            raise CommandExecutionError((
                'Could not infer recorder channel numbers for '
                'recording file "{}".').format(file.path))
            
        else:
            # number of connected mic outputs matches number of file
            # channels
            
            # We assume that recorder inputs map to file channel numbers
            # in increasing order.
            file.recorder_channel_nums = tuple(sorted(mic_outputs.keys()))
            
    
    file.mic_outputs = tuple(
        _get_mic_output(mic_outputs, i, file.path)
        for i in file.recorder_channel_nums)
 def begin_exports(self):
     
     try:
         self._file = h5py.File(self._output_file_path, 'w')
     except OSError as e:
         raise CommandExecutionError(str(e))
     
     self._clip_manager = clip_manager.instance
Exemplo n.º 4
0
    def begin_exports(self):

        try:
            os_utils.create_directory(self._output_dir_path)
        except OSError as e:
            raise CommandExecutionError(str(e))

        self._clip_manager = clip_manager.instance
Exemplo n.º 5
0
def _get_mic_output(mic_outputs, channel_num, file_path):
    
    try:
        return mic_outputs[channel_num]
    
    except KeyError:
        raise CommandExecutionError((
            'Could not find microphone output connected to recorder input '
            '{} for recording file "{}".').format(channel_num, file_path))
Exemplo n.º 6
0
def _get_recorder(file):

    end_time = signal_utils.get_end_time(file.start_time, file.length,
                                         file.sample_rate)

    station_recorders = file.station.get_station_devices(
        'Audio Recorder', file.start_time, end_time)

    if len(station_recorders) == 0:
        raise CommandExecutionError(
            f'Could not find recorder for recording file "{file.path}".')

    elif len(station_recorders) > 1:
        raise CommandExecutionError(
            f'Found more than one possible recorder for file "{file.path}".')

    else:
        return station_recorders[0].device
Exemplo n.º 7
0
 def begin_exports(self):
     
     try:
         self._file = h5py.File(self._output_file_path, 'w')
     except OSError as e:
         raise CommandExecutionError(str(e))
     
     # Always create the "clips" group, even if it will be empty.
     self._file.create_group('/clips')
     
     self._clip_manager = clip_manager.instance
Exemplo n.º 8
0
    def _handle_bad_recording_file_path(self, file_path, condition, manager):

        dir_paths = manager.recording_dir_paths
        
        if len(dir_paths) == 1:
            s = 'the recording directory "{}"'.format(dir_paths[0])
        else:
            path_list = str(list(dir_paths))
            s = 'any of the recording directories {}'.format(path_list)
            
        raise CommandExecutionError(
            'Recording file "{}" {} {}.'.format(file_path, condition, s))
    def _get_station_recordings(self, station_name, start_date, end_date):

        try:
            station = Station.objects.get(name=station_name)
        except Station.DoesNotExist:
            raise CommandExecutionError(
                f'Unrecognized station "{station_name}".')

        time_interval = station.get_night_interval_utc(start_date, end_date)

        return Recording.objects.filter(station=station,
                                        start_time__range=time_interval)
Exemplo n.º 10
0
    def _handle_bad_recording_file_path(self, file_path, condition, manager):

        dir_paths = manager.recording_dir_paths

        if len(dir_paths) == 1:
            s = f'the recording directory "{dir_paths[0]}"'
        else:
            path_list = str(list(dir_paths))
            s = f'any of the recording directories {path_list}'

        raise CommandExecutionError(
            f'Recording file "{file_path}" {condition} {s}.')
Exemplo n.º 11
0
    def _parse_recording_file(self, file_path):

        try:
            file = self.file_parser.parse_file(str(file_path))

        except ValueError as e:
            raise CommandExecutionError(
                f'Error parsing recording file "{file_path}": {str(e)}')

        if file.recorder is None:
            file.recorder = _get_recorder(file)

        return file
Exemplo n.º 12
0
    def _get_recording_file_path_info(self, file_path):

        if file_path.is_absolute():

            if not file_path.exists():
                raise CommandExecutionError(
                    f'Purported recording file "{file_path}" does not exist.')

            return self._get_absolute_path_info(file_path)

        else:
            # path is relative

            return self._get_relative_path_info(file_path)
    def execute(self, job_info):
        
        self._job = Job.objects.get(id=job_info.job_id)
        self._logger = logging.getLogger()
        
        dir_path = archive_paths.deferred_action_dir_path
        
        if not dir_path.exists():
            self._logger.info((
                'There are no deferred actions to execute, since '
                'the directory "{}" does not exist.').format(dir_path))
            
        elif not dir_path.is_dir():
            raise CommandExecutionError(
                'The path "{}" exists but is not a directory.'.format(
                    dir_path))
            
        else:

            file_paths = sorted(dir_path.glob('*.pkl'))
            num_files = len(file_paths)
            
            self._logger.info((
                'Executing deferred actions from {} files of directory '
                '"{}"...').format(num_files, dir_path))
                
            try:
                with transaction.atomic():
                    for i, file_path in enumerate(file_paths):
                        self._logger.info((
                            'Executing actions from file {} of {} - '
                            '"{}"...').format(
                                i + 1, num_files, file_path.name))
                        self._execute_deferred_actions(file_path)
              
            except Exception:
                self._logger.error(
                    'Execution of deferred actions failed with an '
                    'exception. Archive database has been restored '
                    'to its state before the job began. See below '
                    'for exception traceback.')
                raise
            
            # If we get here, the execution of all of the deferred
            # actions succeeded and we can move the action files
            # to the `Executed` directory.
            self._move_deferred_action_files(file_paths)
              
        return True
Exemplo n.º 14
0
 def _get_recording_file_paths(self, file_path):
     
     if file_path.is_absolute():
         
         if not file_path.exists():
             raise CommandExecutionError(
                 'Purported recording file "{}" does not exist.')
             
         rel_path = self._get_relative_path(file_path)
         return rel_path, file_path
         
     else:
         # path is relative
         
         abs_path = self._get_absolute_path(file_path)
         return file_path, abs_path
    def _get_absolute_file_path(self, rel_path):

        manager = recording_manager.instance

        try:
            return manager.get_absolute_recording_file_path(rel_path)

        except ValueError:

            dir_paths = manager.recording_dir_paths

            if len(dir_paths) == 1:
                s = f'the recording directory "{dir_paths[0]}"'
            else:
                path_list = str(list(dir_paths))
                s = f'any of the recording directories {path_list}'

            raise CommandExecutionError(
                f'Recording file "{rel_path}" could not be found in {s}.')
Exemplo n.º 16
0
    def _get_station_recordings(self, station_name, start_date, end_date):

        # TODO: Test behavior for an unrecognized station name.
        # I tried this on 2016-08-23 and got results that did not
        # make sense to me. An exception was raised, but it appeared
        # to be  raised from within code that followed the except clause
        # in the `execute` method above (the code logged the sequence of
        # recordings returned by the `_get_recordings` method) rather
        # than from within that clause, and the error message that I
        # expected to be logged by that clause did not appear in the log.

        try:
            station = Station.objects.get(name=station_name)
        except Station.DoesNotExist:
            raise CommandExecutionError(
                'Unrecognized station "{}".'.format(station_name))

        time_interval = station.get_night_interval_utc(start_date, end_date)

        return Recording.objects.filter(station=station,
                                        start_time__range=time_interval)
 def end_exports(self):
     table = '\n'.join(self._lines) + '\n'
     try:
         os_utils.write_file(self._output_file_path, table)
     except OSError as e:
         raise CommandExecutionError(str(e))