示例#1
0
class SpuSound(SpuFileSystem):
    def __init__(self, spuFS, base):
        self._spuFS = spuFS
        self._base = base
        self._filepath = None
        self._filename = None
        self._sfilename = None
        self._sound = None
        self._debug_time = SpuDebugTime()
        self._logging = SpuLogging()
        self._logging.set_class('SpuSound')

    def _save_sound(self, path, filename, sound):
        if not self._spuFS.new_binfile(path, filename, sound):
            self._spuFS.mkdir(path)
            if not self._spuFS.new_binfile(path, filename, sound):
                logging.info('[SaveSound] create %s failed' %
                             (path + filename))
                return False
        return True

    def add_sound(self, sound, sound_suffix, *args):
        path = make_timebase_path(self._base)
        (sfilename, filename) = make_random_arg_path(sound_suffix, *args)
        if self._save_sound(path, filename, sound):
            self._filepath = path
            self._sfilename = sfilename
            self._filename = filename
            self._sound = sound
            return True
        return False

    def convert_to_mp3(self):
        self._logging.set_function('convert_to_mp3')
        mp3_filename = self.local_path_no_extname() + '.mp3'
        self._debug_time.start()
        ffmpeg = converter.FFMpeg()
        gen = ffmpeg.convert(self.local_path(), mp3_filename, [])
        sound_time = gen.next()
        t = self._debug_time.end()
        self._logging.perf("file:%s play time:%s" % (mp3_filename, sound_time),
                           t)
        return (mp3_filename, sound_time)

    def local_path(self):
        if not self._filepath and not self._filename:
            return None
        return self._spuFS.full_path(self._filepath, self._filename)

    def local_path_no_extname(self):
        if not self._filepath and not self._filename:
            return None
        return self._spuFS.full_path(self._filepath, self._sfilename)

    def url(self):
        if not self._filepath and not self._filename:
            return None
        return self._spuFS.url(self._filepath, self._filename)
示例#2
0
class SpuDebugTime:
    def __init__(self, classinfo=''):
        self.s_time = None
        self.e_time = None
        self.use_time = 0
        self._logging = SpuLogging(module_name='SpuDebug',
                                   class_name=classinfo,
                                   tag_name='DebugTime')

    # use with
    def __enter__(self):
        self.start()

    def __exit__(self, exc_type, exc_value, exc_tb):
        # use xx.use_time get result
        self.use_time = self.end()
        if exc_tb:
            return False
        return True

    def __str__(self):
        return self.time()

    def set_classinfo(self, classinfo):
        self._logging.set_class(classinfo)

    def set_function(self, function):
        self._logging.set_function(function)

    def start(self):
        self.s_time = time.time()

    def end(self):
        self.e_time = time.time()
        return self.time()

    def point(self, msg=''):
        if msg:
            self._logging.perf(msg, self.end())
        else:
            self._logging.perf(self.end())
        self.start()

    def time(self, s=False):
        r = self.e_time - self.s_time
        r *= 100000
        r = int(r)
        r = float(r) / 100000
        if s:
            return str(r) + 's'
        else:
            return str(r * 1000) + 'ms'
示例#3
0
class SpuFileSystem:
    def __init__(self, cfg):
        self._cfg = cfg
        self.root_dir = cfg.get('root', None)
        self.urlbase = cfg.get('urlbase', None)
        self._debug_time = SpuDebugTime()
        self._logging = SpuLogging()
        self._logging.set_class('SpuFileSystem')
        assert self.root_dir, 'Not Root Path'
        assert self.urlbase, 'Not Url Base'

    def mkdir(self, dir_path):
        full_path = self.full_path(dir_path, '')
        os.makedirs(full_path)

    def rmdir(self, dir_path):
        full_path = self.full_path(dir_path, '')
        os.removedirs(full_path)

    def isfile(self, path, file):
        full_path = self.full_path(path, file)
        return os.path.isfile(full_path)

    def exists(self, path):
        full_path = self.full_path(path, '')
        return os.path.exists(full_path)

    def _new_file(self, dir_path, file_name, open_type, file):
        full_path = self.full_path(dir_path, file_name)
        try:
            fd = open(full_path, open_type)
            fd.write(file)
            fd.close()
        except IOError as m:
            if m.errno == 2:
                return False
        return True

    def new_binfile(self, dir_path, file_name, file):
        self._logging.set_function('new_binfile')
        self._debug_time.start()
        b = self._new_file(dir_path, file_name, 'wb', file)
        t = self._debug_time.end()
        self._logging.perf("file:%s" % dir_path + file_name, t)
        return b

    def get_file_size(self, dir_path, file_name):
        full_path = self.full_path(dir_path, file_name)
        return os.path.getsize(full_path)

    def read_binfile(self, dir_path, file_name):
        self._logging.set_function('read_binfile')
        self._debug_time.start()
        full_path = self.full_path(dir_path, file_name)
        try:
            fd = open(full_path, 'rb')
            file_size = self.get_file_size(dir_path, file_name)
            binfile = fd.read(file_size)
            fd.close()
        except Exception as m:
            self._logging.error('read_binfile error: %s' % m)
            binfile = None
        t = self._debug_time.end()
        self._logging.perf("file:%s" % dir_path + file_name, t)
        return binfile

    def new_textfile(self, dir_path, file_name, file):
        return self._new_file(dir_path, file_name, 'w', file)

    def remove_file(self, dir_path, file_name):
        full_path = self.full_path(dir_path, file_name)
        os.remove(full_path)

    def _merge_path(self, a, b):
        if a[-1] == '/' and b[0] == '/':
            c = a + b[1:]
        elif a[-1] != '/' and b[0] != '/':
            c = ''.join([a, '/', b])
        else:
            c = a + b
        return c

    def full_path(self, dir_path, file_name):
        full_path = self._merge_path(self.root_dir, dir_path)
        if file_name:
            full_path = self._merge_path(full_path, file_name)
        return full_path

    def url(self, dir_path, file_name):
        url = self._merge_path(self.urlbase, dir_path)
        if file_name:
            url = self._merge_path(url, file_name)
        return url