validators.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 
00005 import wave
00006 import StringIO
00007 from functools import wraps
00008 from exceptions import ParameterException
00009 from exceptions import UnsupportedLanguageException
00010 from exceptions import InvalidAudioDataException
00011 
00012 
00013 def accepts(**types):
00014     """ check function argument types
00015 
00016     @param types: parameter types (kwdict)
00017     @type  types: dict
00018     @raise ParameterException: parameter type exception
00019     """
00020     def check_accepts(f):
00021         @wraps(f)
00022         def wrapper(*args, **kwds):
00023             for cnt, val in enumerate(args):
00024                 value_type = f.func_code.co_varnames[cnt]
00025                 if value_type in types and \
00026                         not isinstance(val, types[value_type]):
00027                     raise ParameterException(
00028                         "argument '%s'=%r does not match %s" % (
00029                             value_type,
00030                             val,
00031                             types[value_type]
00032                         )
00033                     )
00034                     del types[f.func_code.co_varnames[cnt]]
00035 
00036             for key, val in kwds.iteritems():
00037                 if key in types and not isinstance(val, types[key]):
00038                     raise ParameterException(
00039                         "argument '%s'=%r does not match %s" % (
00040                             key,
00041                             val,
00042                             types[key]
00043                         )
00044                     )
00045             return f(*args, **kwds)
00046         return wrapper
00047     return check_accepts
00048 
00049 
00050 def check_wave_data(data, framerate, nchannel, sampwidth, maxlength):
00051     """ check wave file error.
00052 
00053     @param data: parameter data
00054     @type  data: str
00055     @param framerate: wave file framerate
00056     @type  framerate: int
00057     @param nchannel: wave file channel
00058     @type  nchannel: int
00059     @param sampwidth: sampling width (1=>8bit, 2=>16bit, 4=>32bit)
00060     @type  sampwidth: int
00061     @param maxlength: max wave length [ms]
00062     @type  maxlength: int
00063     @raises InvalidAudioDataException
00064     """
00065     try:
00066         wav_buffer = StringIO.StringIO(data)
00067         wav_file = wave.open(wav_buffer, 'rb')
00068         framerate_ = wav_file.getframerate()
00069         sampwidth_ = wav_file.getsampwidth()
00070         channels_ = wav_file.getnchannels()
00071         nframes_ = wav_file.getnframes()
00072 
00073         read_data = wav_file.readframes(nframes_)
00074         read_data_size = len(read_data)
00075         header_data_size = sampwidth_ * channels_ * nframes_
00076         wave_length_ = int(
00077             float(wav_file.getnframes()) / wav_file.getframerate() * 1000.0
00078         )
00079 
00080         if framerate_ != framerate:
00081             msg = 'the wav file frame rate MUST be %d. curr: %d' % (
00082                 framerate,
00083                 framerate_
00084             )
00085             raise InvalidAudioDataException(msg)
00086 
00087         if sampwidth_ != sampwidth:
00088             msg = 'the wav file samplewidth MUST be %dbit. curr: %d' % (
00089                 sampwidth*8,
00090                 sampwidth_*8
00091             )
00092             raise InvalidAudioDataException(msg)
00093 
00094         if channels_ != nchannel:
00095             msg = 'the wav file channels MUST be %d. curr: %d' % (
00096                 nchannel,
00097                 channels_
00098             )
00099             raise InvalidAudioDataException(msg)
00100 
00101         if header_data_size != read_data_size:
00102             msg = 'the wav file is broken. data_size(by header): '\
00103                   '%d data_size(by data): %d' % (
00104                         header_data_size,
00105                         read_data_size
00106                     )
00107             raise InvalidAudioDataException(msg)
00108 
00109         if wave_length_ > maxlength:
00110             msg = 'the playing time of the wav file is too long. '\
00111                   'max %d[ms] curr: %d[ms]' % (
00112                         maxlength,
00113                         wave_length_
00114                     )
00115             raise InvalidAudioDataException(msg)
00116 
00117         if wave_length_ == 0:
00118             msg = 'the playing time of the wav file is zero.'
00119             raise InvalidAudioDataException(msg)
00120 
00121     except wave.Error as err:
00122         msg = 'the input audio data MUST be wav format. '\
00123               'Exception: %s' % str(err)
00124         raise InvalidAudioDataException(msg)
00125 
00126 
00127 def check_language(language, supported_languages):
00128     """ check supported language
00129 
00130     @param param_name: check parameter name
00131     @type  param_name: str
00132     @param languages: supported languages in the client
00133     @type  languages: list
00134     @raises UnsupportedLanguageException
00135     """
00136     # check supported language
00137     if language not in supported_languages:
00138         msg = '%s is unsupported language. supported languages are %s' % (
00139             language,
00140             supported_languages
00141         )
00142         raise UnsupportedLanguageException(msg)


rospeex_core
Author(s): Komei Sugiura
autogenerated on Thu Apr 20 2017 03:08:53