def test_format_from_output(mocker, b2a_tmp_path, in_path, format): out_file = os.path.join(b2a_tmp_path, 'out.' + format) mocker.patch('sys.argv', ['exec', in_path, '-o', out_file]) main() format_mime = {'mp3': 'audio/mpeg', 'wav': 'audio/x-wav'} assert (filetype.audio(out_file).mime == format_mime[format])
def uploadSketchWav(request): if request.method == 'POST': try: form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): wavfile = request.FILES['file'] if isinstance(filetype.audio(wavfile), audio.Wav): return readWav(request, wavfile) else: raise TypeError( f"Mime type must equal {audio.Wav.mime}. Requires a Wav file instead" ) except Exception as conversionexception: return render(request, 'smileland/midisketch.html', {'errormessage': str(conversionexception)}) raise Http404
def test_format_argument(mocker, b2a_tmp_path, format, in_path): out_path = os.path.join(b2a_tmp_path, 'out.{}'.format(format)) mocker.patch('sys.argv', ['exec', in_path, '-o', out_path]) mocker.spy(m2.beats2audio, 'create_beats_audio') main() expected_args = (click_times, out_path, format, defaults.CLICK_GAIN, 0, defaults.SAMPLE_RATE) format_mime = {'mp3': 'audio/mpeg', 'wav': 'audio/x-wav'} m2.beats2audio.create_beats_audio.assert_called_once() call_args = m2.beats2audio.create_beats_audio.call_args assert (call_args[0][0] == expected_args[0]).all() assert (call_args[0][1:] == expected_args[1:]) assert (filetype.audio(out_path).mime == format_mime[format])
def get_file_type(self): """Determine type of file. Check extension before bytes, to correctly identify documents and PDF files. All other types are detected by bytes. """ extension = splitext(self.component_file.name)[1][1:] for type_code, type_data in self.COMPONENT_TYPE_DATA.items(): if extension in type_data.get('extensions', set()): return type_code file_obj = self.component_file.open() if filetype.image(file_obj): return self.TYPE_IMAGE elif filetype.video(file_obj): return self.TYPE_VIDEO elif filetype.audio(file_obj): return self.TYPE_AUDIO elif filetype.archive(file_obj): return self.TYPE_ARCHIVE else: return self.TYPE_OTHER
def reset(self, inFilename, outFilename): audiotype = filetype.audio(inFilename) if isinstance(audiotype, audio.Wav): self.correctAudioType(inFilename, outFilename) else: self.wrongAudioType(audiotype)