home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / serpentine / converting.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  13.4 KB  |  397 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''"
  5. This module is used to convert audio data to local WAV files. These files
  6. are cached in the local filesystem and can be used by the recording module.
  7. '''
  8. import tempfile
  9. import os
  10. import gst
  11. from types import StringType
  12. import urlutil
  13. import operations
  14. import audio
  15.  
  16. class GetMusic(operations.MeasurableOperation, operations.OperationListener):
  17.     '''"
  18.     A Measurable Operation that finishes when the filename is available in the 
  19.     music pool.
  20.     '''
  21.     
  22.     def __init__(self, pool, music):
  23.         operations.MeasurableOperation.__init__(self)
  24.         self._GetMusic__music = music
  25.         self._GetMusic__pool = pool
  26.         self._GetMusic__oper = None
  27.  
  28.     
  29.     def getProgress(self):
  30.         if self._GetMusic__oper is not None:
  31.             return self._GetMusic__oper.progress
  32.         
  33.         return 0.0
  34.  
  35.     progress = property(getProgress)
  36.     running = property((lambda self: if self._GetMusic__oper:
  37. passself._GetMusic__oper.running))
  38.     can_stop = property((lambda self: if self._GetMusic__oper:
  39. passself._GetMusic__oper.can_stop))
  40.     music = property((lambda self: self._GetMusic__music))
  41.     pool = property((lambda self: self._GetMusic__pool))
  42.     
  43.     def start(self):
  44.         if self._GetMusic__pool.is_available(self._GetMusic__music):
  45.             self._send_finished_event(operations.SUCCESSFUL)
  46.             self._GetMusic__oper = None
  47.         else:
  48.             
  49.             try:
  50.                 self._GetMusic__oper = self._GetMusic__pool.fetch_music(self._GetMusic__music)
  51.                 self._GetMusic__oper.listeners.append(self)
  52.                 self._GetMusic__oper.start()
  53.             except Exception:
  54.                 e = None
  55.                 import traceback
  56.                 traceback.print_exc()
  57.                 self._send_finished_event(operations.ERROR, error = e)
  58.  
  59.  
  60.     
  61.     def on_finished(self, event):
  62.         self._send_finished_event(event.id, event.error)
  63.         self._GetMusic__oper = None
  64.  
  65.     
  66.     def stop(self):
  67.         self._GetMusic__oper.stop()
  68.  
  69.  
  70.  
  71. class MusicPool:
  72.     '''
  73.     A music pool is basically a place where you put your music to convert it
  74.     to local WAV files which will then be used to create audio CDs.
  75.     '''
  76.     
  77.     def __init__(self):
  78.         raise NotImplementedError
  79.  
  80.     
  81.     def fetch_music(self, music):
  82.         """
  83.         Returns a operations.MeasurableOperation that correspondes the user
  84.         can use to monitor it's progress.
  85.         """
  86.         raise NotImplementedError
  87.  
  88.     
  89.     def is_available(self, music):
  90.         '''
  91.         Returns if a certain music is on cache.
  92.         '''
  93.         raise NotImplementedError
  94.  
  95.     
  96.     def get_filename(self, music):
  97.         '''
  98.         Returns the filename associated with a certain music in cache. The user
  99.         must be sure that the file is in cache.
  100.         '''
  101.         raise NotImplementedError
  102.  
  103.     
  104.     def clear(self):
  105.         """
  106.         Clears the pool of it's elements.
  107.         """
  108.         raise NotImplementedError
  109.  
  110.  
  111.  
  112. class GstCacheEntry:
  113.     
  114.     def __init__(self, filename, is_temp):
  115.         self.is_temp = is_temp
  116.         self.filename = filename
  117.  
  118.  
  119.  
  120. class GstSourceToWavListener(operations.OperationListener):
  121.     
  122.     def __init__(self, parent, filename, music):
  123.         self._GstSourceToWavListener__filename = filename
  124.         self._GstSourceToWavListener__music = music
  125.         self._GstSourceToWavListener__parent = parent
  126.  
  127.     
  128.     def on_finished(self, event):
  129.         if event.id == operations.SUCCESSFUL:
  130.             self._GstSourceToWavListener__parent.cache[self._GstSourceToWavListener__parent.unique_music_id(self._GstSourceToWavListener__music)] = GstCacheEntry(self._GstSourceToWavListener__filename, True)
  131.         else:
  132.             os.unlink(self._GstSourceToWavListener__filename)
  133.  
  134.  
  135.  
  136. class GstMusicPool(MusicPool):
  137.     
  138.     def __init__(self):
  139.         self._GstMusicPool__cache = { }
  140.         self._GstMusicPool__temp_dir = None
  141.  
  142.     cache = property((lambda self: self._GstMusicPool__cache))
  143.     
  144.     def setTemporaryDir(self, temp_dir):
  145.         self._GstMusicPool__temp_dir = temp_dir
  146.  
  147.     
  148.     def getTemporaryDir(self):
  149.         return self._GstMusicPool__temp_dir
  150.  
  151.     temporaryDir = property(getTemporaryDir, setTemporaryDir)
  152.     
  153.     def is_available(self, music):
  154.         return self.cache.has_key(self.unique_music_id(music))
  155.  
  156.     
  157.     def get_filename(self, music):
  158.         return self.cache[self.unique_music_id(music)].filename
  159.  
  160.     
  161.     def get_source(self, music):
  162.         raise NotImplementedError
  163.  
  164.     
  165.     def fetch_music(self, music):
  166.         '''
  167.         Can throw a OSError exception in case of the provided temporary dir being
  168.         invalid.
  169.         '''
  170.         source = self.get_source()
  171.         (handle, filename) = tempfile.mkstemp(suffix = '.wav', dir = self.temporaryDir)
  172.         os.close(handle)
  173.         our_listener = GstSourceToWavListener(self, filename, music)
  174.         oper = audio.convert_to_wav(source, music, filename)
  175.         oper.listeners.append(our_listener)
  176.         return oper
  177.  
  178.     
  179.     def clear(self):
  180.         for key in self.cache:
  181.             entry = self.cache[key]
  182.             if entry.is_temp:
  183.                 os.unlink(entry.filename)
  184.                 continue
  185.         
  186.         self._GstMusicPool__cache = { }
  187.  
  188.     
  189.     def __del__(self):
  190.         self.clear()
  191.  
  192.     
  193.     def unique_music_id(self, music):
  194.         pass
  195.  
  196.  
  197.  
  198. class GvfsMusicPool(GstMusicPool):
  199.     use_gnomevfs = True
  200.     
  201.     def unique_music_id(self, uri):
  202.         """
  203.         Provides a way of uniquely identifying URI's, in case of user sends:
  204.         file:///foo%20bar
  205.         file:///foo bar
  206.         /foo bar
  207.         Returns always a URL, even if the string was a file path.
  208.         """
  209.         return urlutil.normalize(uri)
  210.  
  211.     
  212.     def is_wav(self, filename):
  213.         is_pcm = audio.is_wav_pcm(self.get_source(), filename)
  214.         return operations.syncOperation(is_pcm).id == operations.SUCCESSFUL
  215.  
  216.     
  217.     def is_local(self, filename):
  218.         return urlutil.is_local(filename)
  219.  
  220.     
  221.     def is_available(self, music):
  222.         on_cache = GstMusicPool.is_available(self, music)
  223.         unique_id = self.unique_music_id(music)
  224.         if not on_cache and self.is_local(music) and self.is_wav(unique_id):
  225.             filename = urlutil.get_path(unique_id)
  226.             self.cache[unique_id] = GstCacheEntry(filename, False)
  227.             on_cache = True
  228.         
  229.         return on_cache
  230.  
  231.     
  232.     def get_source(self):
  233.         if self.use_gnomevfs:
  234.             return audio.GVFS_SRC
  235.         else:
  236.             return audio.FILE_SRC
  237.  
  238.  
  239.  
  240. class FetchMusicListPriv(operations.OperationListener):
  241.     
  242.     def __init__(self, parent, music_list):
  243.         self.music_list = music_list
  244.         self.parent = parent
  245.  
  246.     
  247.     def get_music_from_uri(self, uri):
  248.         for m in self.music_list:
  249.             if m['location'] == uri:
  250.                 return m
  251.                 continue
  252.         
  253.  
  254.     
  255.     def on_finished(self, evt):
  256.         if isinstance(evt.source, operations.OperationsQueue):
  257.             self.parent._propagate(evt)
  258.             return None
  259.         
  260.         if evt.id != operations.SUCCESSFUL:
  261.             return None
  262.         
  263.         uri = evt.source.music
  264.         pool = evt.source.pool
  265.         filename = pool.get_filename(uri)
  266.         m = self.get_music_from_uri(uri)
  267.         if m:
  268.             m['cache_location'] = filename
  269.         
  270.  
  271.     
  272.     def before_operation_starts(self, event, operation):
  273.         e = operations.Event(self.parent)
  274.         m = self.get_music_from_uri(operation.music)
  275.         for l in self.parent.listeners:
  276.             if isinstance(l, FetchMusicListListener):
  277.                 l.before_music_fetched(e, m)
  278.                 continue
  279.         
  280.  
  281.  
  282.  
  283. class FetchMusicListListener(operations.OperationListener):
  284.     
  285.     def before_music_fetched(self, event, music):
  286.         pass
  287.  
  288.  
  289.  
  290. class FetchMusicList(operations.MeasurableOperation):
  291.     """
  292.     Fetches a music list which contains the field 'uri' and replaces it by a
  293.     local filename located inside the pool. When the filename is fetched it
  294.     updates the 'filename' field inside each music entry of the music list.
  295.     """
  296.     
  297.     def __init__(self, music_list, pool):
  298.         operations.MeasurableOperation.__init__(self)
  299.         self._FetchMusicList__music_list = music_list
  300.         self._FetchMusicList__queue = operations.OperationsQueue()
  301.         self._FetchMusicList__pool = pool
  302.         self._FetchMusicList__listener = FetchMusicListPriv(self, music_list)
  303.         self._FetchMusicList__queue.listeners.append(self._FetchMusicList__listener)
  304.  
  305.     
  306.     def getProgress(self):
  307.         return self._FetchMusicList__queue.progress
  308.  
  309.     progress = property(getProgress)
  310.     running = property((lambda self: self._FetchMusicList__queue.running))
  311.     
  312.     def start(self):
  313.         for m in self._FetchMusicList__music_list:
  314.             get = GetMusic(self._FetchMusicList__pool, m['location'])
  315.             get.listeners.append(self._FetchMusicList__listener)
  316.             self._FetchMusicList__queue.append(get)
  317.         
  318.         self._FetchMusicList__queue.start()
  319.  
  320.     can_stop = property((lambda self: self._FetchMusicList__queue.can_stop))
  321.     
  322.     def stop(self):
  323.         self._FetchMusicList__queue.stop()
  324.  
  325.  
  326. if __name__ == '__main__':
  327.     import sys
  328.     import gtk
  329.     import gobject
  330.     import gtkutil
  331.     from os import path
  332.     
  333.     def quit():
  334.         return False
  335.  
  336.     
  337.     def pp(oper):
  338.         p = oper.get_progress()
  339.         print p
  340.         if p == 1:
  341.             gtk.main_quit()
  342.         
  343.         return True
  344.  
  345.     
  346.     class MyListener(FetchMusicListListener):
  347.         
  348.         def __init__(self, prog, oper):
  349.             FetchMusicListListener.__init__(self)
  350.             self.music = None
  351.             self.prog = prog
  352.             self.oper = oper
  353.  
  354.         
  355.         def before_music_fetched(self, evt, music):
  356.             print music
  357.             prog_txt = 'Converting '
  358.             prog_txt += path.basename(urlutil.get_path(music['location']))
  359.             self.prog.sub_progress_text = prog_txt
  360.             self.prog.progress_fraction = self.oper.progress
  361.             if self.music:
  362.                 print 'Fetched', self.music['cache_location']
  363.             
  364.             print 'Fetching', music['location']
  365.             self.music = music
  366.  
  367.         
  368.         def on_finished(self, e):
  369.             if self.music:
  370.                 print 'Fetched', self.music
  371.             
  372.             print self.oper.progress
  373.             gtk.main_quit()
  374.  
  375.         
  376.         def tick(self):
  377.             self.prog.progress_fraction = self.oper.progress
  378.             return True
  379.  
  380.  
  381.     prog = gtkutil.HigProgress()
  382.     prog.primary_text = 'Recording Audio Disc'
  383.     prog.secondary_text = 'Selected files are to be written to a CD or DVD disc. This operation may take a long time, depending on data size and write speed.'
  384.     prog.show()
  385.     pool = GvfsMusicPool()
  386.     music_list = [
  387.         {
  388.             'location': path.abspath(sys.argv[1]) }]
  389.     fetcher = FetchMusicList(music_list, pool)
  390.     l = MyListener(prog, fetcher)
  391.     fetcher.listeners.append(l)
  392.     gobject.timeout_add(200, l.tick)
  393.     fetcher.start()
  394.     gtk.main()
  395.     pool.clear()
  396.  
  397.