Exemple #1
0
 def _test_fill_file_paths(self, files: list) -> list:
     if not os.path.isdir(self.cfg.path['test']):
         self.log(
             'Test path not exist: \'{}\''.format(self.cfg.path['test']),
             logger.WARN)
         return []
     if '*' in files:
         # all wav
         return self.cfg.get_all_testfile()
     allow, wrong_name, not_found = [], [], []
     unique = set()
     for file in files:
         if not utils.is_valid_base_filename(file):
             wrong_name.append(file)
             continue
         file = self._test_filename_normalization(file)
         if file in unique:
             continue
         unique.add(file)
         if not os.path.isfile(os.path.join(self.cfg.path['test'], file)):
             not_found.append(file)
         else:
             allow.append(file)
     if wrong_name:
         wrong_name = ', '.join([repr(name) for name in wrong_name])
         self.log('Wrong file names: {}'.format(wrong_name))
     if not_found:
         not_found = ', '.join([name for name in not_found])
         self.log('Files not found: {}'.format(not_found),
                  logger.DEBUG if allow else logger.WARN)
     return allow
Exemple #2
0
    def test_record(self, file: str, limit: int, *_, **__):
        if not utils.is_valid_base_filename(file):
            self.log('Pass record, wrong file name: {}'.format(repr(file)),
                     logger.WARN)
            return
        if not os.path.isdir(self.cfg.path['test']):
            try:
                os.makedirs(self.cfg.path['test'])
            except OSError as e:
                self.log(
                    'os.makedirs error {}: {}'.format(self.cfg.path['test'],
                                                      e), logger.ERROR)
                return
        file = self._test_filename_normalization(file)
        save_to = os.path.join(self.cfg.path['test'], file)

        self.log('Start recording {} sec audio after signal...'.format(limit),
                 logger.INFO)
        err = self.own.voice_record(hello=None,
                                    save_to=save_to,
                                    convert_rate=16000,
                                    convert_width=2,
                                    limit=limit)
        if err is None:
            self.log('Recording complete and save to {}'.format(file),
                     logger.INFO)
        else:
            self.log('An error occurred while recording: {}'.format(err),
                     logger.WARN)
        self.own.play(self.cfg.path['dong'])
Exemple #3
0
 def _check_file(self, filename: str) -> tuple:
     if self.work:
         return F('Демон еще работает'), None
     if not utils.is_valid_base_filename(filename):
         return F('Некорректное имя файла: {}', repr(filename)), None
     path = os.path.join(self.cfg.path['backups'], filename)
     if not os.path.isfile(path):
         return F('Файл не найден: {}', path), None
     try:
         self._get_zip_timestamp(path)
     except Exception as e:
         return F('Архив поврежден: {}: {}', filename, e), None
     return None, path
Exemple #4
0
 def _api_backup_restore(self, _, data):
     if not data:
         raise InternalException(msg='Empty filename')
     if not is_valid_base_filename(data):
         raise InternalException(2, 'Wrong filename')
     files = self.own.backup_list()
     if not files:
         raise InternalException(3, 'No backups')
     if data == 'last':
         filename, timestamp = files[0]
     else:
         filename, timestamp = next(
             (item for item in files if item[0] == data), (None, None))
         if not filename:
             raise InternalException(4, 'File no found: {}'.format(data))
     self.own.backup_restore(filename)
     return {'filename': filename, 'timestamp': timestamp}
Exemple #5
0
 def is_model_name(self, filename: str) -> bool:
     return utils.is_valid_base_filename(filename) and \
            pathlib.Path(filename).suffix.lstrip('.').lower() in self.MODELS_SUPPORT
Exemple #6
0
 def is_testfile_name(self, filename: str) -> bool:
     return utils.is_valid_base_filename(filename) and \
            os.path.splitext(filename)[1].lower() == self.path['test_ext']