Пример #1
0
 def download_tracks(self):
     if not self.tree_model:
         return
     save_dir = settings.save_dir
     if not os.access(save_dir, os.W_OK):
         msg = 'No write access to save directory. '
         msg += 'Choose a new directory with write privileges.'
         QMessageBox.information(self, 'No Access!', msg)
         return
     self.downloader = Downloader(self.tree_model, self.update_cb,
                                  self.finished_cb)
     self.actionDownload.setEnabled(False)
     self.actionSettings.setEnabled(False)
     self.actionLoadFiles.setEnabled(False)
     self.downloader.start()
Пример #2
0
    def download_tracks(self):
        settings.num_threads = 1  # until this printer becomes more sophisticated
        if not self.tree_model:
            return
        save_dir = settings.save_dir
        if not os.access(save_dir, os.W_OK):
            msg = 'No write access to save directory. '
            msg += 'Choose a new directory with write privileges.'
            raise IOError(msg)

        self.downloader = Downloader(self.tree_model, self.update_cb,
                                     self.finished_cb)
        print
        print '##############################################'
        self.downloader.start()
        self.downloader.join()
Пример #3
0
 def download_tracks(self):
     self.num_threads = 1
     if not self.tree_model:
         return
     save_dir = settings.save_dir
     if not os.access(save_dir, os.W_OK):
         print 'here'
         msg = 'No write access to save directory. '
         msg += 'Choose a new directory with write privileges.'
         dialog = gtk.MessageDialog(self.mainWindow, buttons=gtk.BUTTONS_OK)
         dialog.set_markup(msg)
         dialog.run()
         dialog.destroy()
         return
     self.downloader = Downloader(self.tree_model, self.update_cb,
                                  self.finished_cb)
     self.actionDownload.set_sensitive(False)
     self.actionSettings.set_sensitive(False)
     self.actionLoadFiles.set_sensitive(False)
     self.downloader.start()
Пример #4
0
 def download_tracks(self):
     if not self.tree_model:
         return
     save_dir = settings.save_dir        
     if not os.access(save_dir, os.W_OK):
         msg = 'No write access to save directory. '
         msg += 'Choose a new directory with write privileges.'
         QMessageBox.information(self, 'No Access!', msg)
         return        
     self.downloader = Downloader(self.tree_model, self.update_cb, 
                                  self.finished_cb)
     self.actionDownload.setEnabled(False)
     self.actionSettings.setEnabled(False)
     self.actionLoadFiles.setEnabled(False)        
     self.downloader.start()     
Пример #5
0
 def download_tracks(self):
     settings.num_threads = 1 # until this printer becomes more sophisticated
     if not self.tree_model:
         return
     save_dir = settings.save_dir        
     if not os.access(save_dir, os.W_OK):
         msg = 'No write access to save directory. '
         msg += 'Choose a new directory with write privileges.'
         raise IOError(msg)        
     
     self.downloader = Downloader(self.tree_model, self.update_cb, 
                                  self.finished_cb)
     print 
     print '##############################################'
     self.downloader.start()
     self.downloader.join()     
Пример #6
0
 def download_tracks(self):
     self.num_threads = 1        
     if not self.tree_model:
         return
     save_dir = settings.save_dir               
     if not os.access(save_dir, os.W_OK):
         print 'here'
         msg = 'No write access to save directory. '
         msg += 'Choose a new directory with write privileges.'
         dialog = gtk.MessageDialog(self.mainWindow, 
                                    buttons=gtk.BUTTONS_OK)
         dialog.set_markup(msg)
         dialog.run()
         dialog.destroy()
         return        
     self.downloader = Downloader(self.tree_model, self.update_cb, 
                                  self.finished_cb)
     self.actionDownload.set_sensitive(False)
     self.actionSettings.set_sensitive(False)
     self.actionLoadFiles.set_sensitive(False)        
     self.downloader.start()            
Пример #7
0
class MainWindow(gobject.GObject):
    def __init__(self, amzs):
        glade_fpath = os.path.join(os.path.split(__file__)[0], '_ui.glade')
        builder = gtk.Builder()
        builder.add_from_file(glade_fpath)

        self.mainWindow = builder.get_object('MainWindow')
        self.mainWindow.show()

        self.albumArtImage = builder.get_object('albumArtImage')
        self.nowDownloadingLabel = builder.get_object('nowDownloadingLabel')
        self.pythonPixbuf = gtk.gdk.pixbuf_new_from_file(python_icon_path)
        self.pymazon_text = '<span weight="bold" size="xx-large">Pymazon! w00t!</span>'
        self.reset_displaybar_info()

        self.settings_dialog = SettingsDialog(builder)

        self.treeView = builder.get_object('treeView')
        self.tree_model = None

        self.pbarRenderer = ProgressRenderer()
        self.colStatus = builder.get_object('colStatus')
        self.colStatus.clear()
        self.colStatus.pack_start(self.pbarRenderer, True)
        self.colStatus.add_attribute(self.pbarRenderer, 'value', 1)
        self.colStatus.add_attribute(self.pbarRenderer, 'text', 2)

        self.connect_signals(builder)

        self.downloader = None
        self.current_album = None
        self.old_albums = []
        if amzs:
            self.load_new_amz_files(amzs)

    def connect_signals(self, builder):
        builder.connect_signals({'on_MainWindow_destroy': self.on_destroy})
        self.actionAbout = builder.get_object('actionAbout')
        self.actionAbout.connect('activate', self.on_actionAbout_activate)
        self.actionLoadFiles = builder.get_object('actionLoadFiles')
        self.actionLoadFiles.connect('activate',
                                     self.on_actionLoadFiles_activate)
        self.actionSettings = builder.get_object('actionSettings')
        self.actionSettings.connect('activate',
                                    self.on_actionSettings_activate)
        self.actionShowDownloads = builder.get_object('actionShowDownloads')
        self.actionShowDownloads.connect('activate',
                                         self.on_actionShowDownloads_activate)
        self.actionDownload = builder.get_object('actionDownload')
        self.actionDownload.connect('activate',
                                    self.on_actionDownload_activate)
        self.actionQuit = builder.get_object('actionQuit')
        self.actionQuit.connect('activate', self.on_destroy)

    def on_actionAbout_activate(self, *args):
        webbrowser.open('http://code.google.com/p/pymazon')

    def on_actionLoadFiles_activate(self, *args):
        self.new_amz()

    def on_actionSettings_activate(self, *args):
        self.settings_dialog.launch()

    def on_actionShowDownloads_activate(self, *args):
        webbrowser.open(settings.save_dir)

    def on_actionDownload_activate(self, *args):
        self.download_tracks()

    def on_destroy(self, *arg):
        if self.downloader:
            self.downloader.kill()
        gtk.main_quit()

    def new_amz(self):
        caption = 'Choose AMZ File'
        filefilter = gtk.FileFilter()
        filefilter.add_pattern('*.amz')
        dialog = gtk.FileChooserDialog(
            caption,
            self.mainWindow,
            buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT, gtk.STOCK_OK,
                     gtk.RESPONSE_ACCEPT))
        dialog.set_select_multiple(True)
        dialog.set_filter(filefilter)
        res = dialog.run()
        files = dialog.get_filenames()
        dialog.destroy()
        if res == gtk.RESPONSE_REJECT:
            return
        if files:
            self.load_new_amz_files(files)

    def load_new_amz_files(self, amz_files):
        parser = AmzParser()
        for f in amz_files:
            parser.parse(f)

        objects = parser.get_parsed_objects()
        self.tree_model = TreeModel(objects)
        self.treeView.set_model(self.tree_model)
        self.treeView.expand_all()

    def download_tracks(self):
        self.num_threads = 1
        if not self.tree_model:
            return
        save_dir = settings.save_dir
        if not os.access(save_dir, os.W_OK):
            print 'here'
            msg = 'No write access to save directory. '
            msg += 'Choose a new directory with write privileges.'
            dialog = gtk.MessageDialog(self.mainWindow, buttons=gtk.BUTTONS_OK)
            dialog.set_markup(msg)
            dialog.run()
            dialog.destroy()
            return
        self.downloader = Downloader(self.tree_model, self.update_cb,
                                     self.finished_cb)
        self.actionDownload.set_sensitive(False)
        self.actionSettings.set_sensitive(False)
        self.actionLoadFiles.set_sensitive(False)
        self.downloader.start()

    def update_cb(self, node):
        if isinstance(node.elem.obj, Album):
            if not node.elem.obj is self.current_album:
                if node.elem.obj not in self.old_albums:
                    self.old_albums.append(node.elem.obj)
                    self.current_album = node.elem.obj
                    self.update_album_info()
                else:
                    pass
        self.tree_model.update_node(node)

    def finished_cb(self):
        self.downloader = None
        self.reset_displaybar_info()
        self.actionDownload.set_sensitive(True)
        self.actionSettings.set_sensitive(True)
        self.actionLoadFiles.set_sensitive(True)

    def reset_displaybar_info(self):
        self.current_album = None
        self.old_albums = []
        self.albumArtImage.set_from_pixbuf(self.pythonPixbuf)
        self.nowDownloadingLabel.set_markup(self.pymazon_text)

    def update_album_info(self):
        self.update_album_art()
        self.update_downloading_text()

    def update_album_art(self):
        img = self.current_album.image
        if img != "":
            loader = gtk.gdk.PixbufLoader()
            loader.write(img)
            loader.close()
            pb = loader.get_pixbuf()
            self.albumArtImage.set_from_pixbuf(pb)
        self.update_downloading_text()

    def update_downloading_text(self):
        name = self.current_album.artist
        title = self.current_album.title
        txt = 'Now dowloading <b>%s</b> by <b>%s</b>' % (title, name)
        self.nowDownloadingLabel.set_markup(txt)
Пример #8
0
class MainWindow(QMainWindow, _ui.Ui_MainWindow):

    updateInfo = pyqtSignal()
    resetInfo = pyqtSignal()

    def __init__(self, amz_files, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)

        loadFileIcon = QIcon.fromTheme('document-new',
                                       QIcon(QPixmap(load_icon_path)))
        downloadIcon = QIcon.fromTheme('go-down',
                                       QIcon(QPixmap(download_icon_path)))
        settingsIcon = QIcon.fromTheme('preferences-other',
                                       QIcon(QPixmap(settings_icon_path)))
        showIcon = QIcon.fromTheme('emblem-downloads',
                                   QIcon(QPixmap(show_icon_path)))
        exitIcon = QIcon.fromTheme('window-close',
                                   QIcon(QPixmap(exit_icon_path)))
        self.pythonPixmap = QPixmap(python_icon_path)
        self.pymazon_text = self.nowDownloadingLabel.text()

        self.actionLoadFiles.setIcon(loadFileIcon)
        self.actionDownload.setIcon(downloadIcon)
        self.actionSettings.setIcon(settingsIcon)
        self.actionShowDownloads.setIcon(showIcon)
        self.actionQuit.setIcon(exitIcon)
        self.albumArtLabel.setPixmap(self.pythonPixmap)

        self.settingsDialog = SettingsDialog(self)

        self.tree_model = None
        self.pbardelegate = ProgressBarDelegate()
        self.treeView.setItemDelegateForColumn(1, self.pbardelegate)

        self.downloader = None
        self.current_album = None
        self.old_albums = []
        self.updateInfo.connect(self.update_album_info)
        self.resetInfo.connect(self.reset_displaybar_info)

        if amz_files:
            self.load_new_amz_files(amz_files)

    @pyqtSlot()
    def on_actionLoadFiles_triggered(self):
        self.new_amz()

    @pyqtSlot()
    def on_actionSettings_triggered(self):
        self.settingsDialog.launch()

    @pyqtSlot()
    def on_actionDownload_triggered(self):
        self.download_tracks()

    @pyqtSlot()
    def on_actionShowDownloads_triggered(self):
        webbrowser.open(settings.save_dir)

    @pyqtSlot()
    def on_actionQuit_triggered(self):
        self.close()

    @pyqtSlot()
    def on_actionAbout_triggered(self):
        webbrowser.open('http://code.google.com/p/pymazon')

    def closeEvent(self, evt):
        if self.downloader:
            self.downloader.kill()
        evt.accept()

    def new_amz(self):
        caption = 'Choose AMZ File'
        filefilter = "Amazon MP3 Download (*.amz)"
        files = [
            str(f) for f in QFileDialog.getOpenFileNames(
                self, caption, '', filefilter)
        ]
        if files:
            self.load_new_amz_files(files)

    def load_new_amz_files(self, amz_files):
        parser = AmzParser()
        for f in amz_files:
            parser.parse(f)
        objects = parser.get_parsed_objects()
        self.tree_model = TreeModel(objects)
        self.treeView.setModel(self.tree_model)
        self.setup_treeview()

    def setup_treeview(self):
        self.treeView.expandAll()
        self.treeView.resizeColumnToContents(0)
        width = self.treeView.columnWidth(0)
        self.treeView.setColumnWidth(1, 100)
        self.treeView.setColumnWidth(0, width + 50)

    def download_tracks(self):
        if not self.tree_model:
            return
        save_dir = settings.save_dir
        if not os.access(save_dir, os.W_OK):
            msg = 'No write access to save directory. '
            msg += 'Choose a new directory with write privileges.'
            QMessageBox.information(self, 'No Access!', msg)
            return
        self.downloader = Downloader(self.tree_model, self.update_cb,
                                     self.finished_cb)
        self.actionDownload.setEnabled(False)
        self.actionSettings.setEnabled(False)
        self.actionLoadFiles.setEnabled(False)
        self.downloader.start()

    def update_cb(self, node):
        if isinstance(node.elem.obj, Album):
            if not node.elem.obj is self.current_album:
                if node.elem.obj not in self.old_albums:
                    self.old_albums.append(node.elem.obj)
                    self.current_album = node.elem.obj
                    self.updateInfo.emit()
                else:
                    pass
        self.tree_model.update_node(node)

    def finished_cb(self):
        self.downloader = None
        self.resetInfo.emit()
        self.actionDownload.setEnabled(True)
        self.actionSettings.setEnabled(True)
        self.actionLoadFiles.setEnabled(True)

    def reset_displaybar_info(self):
        self.current_album = None
        self.old_albums = []
        self.nowDownloadingLabel.setText(self.pymazon_text)
        self.albumArtLabel.setPixmap(self.pythonPixmap)

    def update_album_info(self):
        self.update_album_art()
        self.update_downloading_text()

    def update_album_art(self):
        img = self.current_album.image
        if img != "":
            pm = QPixmap()
            pm.loadFromData(img, None, Qt.AutoColor)
            self.albumArtLabel.setPixmap(pm)

    def update_downloading_text(self):
        name = self.current_album.artist
        title = self.current_album.title
        txt = 'Now dowloading <b>%s</b> by <b>%s</b>' % (title, name)
        self.nowDownloadingLabel.setText(txt)
Пример #9
0
class CommandLineRunner(object):
    def __init__(self, amzs):
        self.tree_model = None
        self.downloader = None        
        self.current_node = None
        self.tab = '    '
        self.load_new_amz_files(amzs)        
        
    def run(self):
        for line in self.get_printable_download_list():
            print line
        print
        print 'Download these items? (Yes/No)'
        res = raw_input()
        if res in ['Yes', 'Y', 'y', 'yes', 'YES']:
            self.download_tracks()
        
    def load_new_amz_files(self, amz_files):
        parser = AmzParser()
        for f in amz_files:
            parser.parse(f)
        objects = parser.get_parsed_objects()        
        self.tree_model = TreeModel()
        self.tree_model.create_tree(objects)
        
    def finished_cb(self):
        finished = pymazon_finished.format(settings.save_dir, 
                                           settings.name_template)
        print
        print finished
    
    def update_cb(self, node):
        if isinstance(node.elem.obj, Track):
            artist = node.parent.elem.data()
            title = node.elem.data()
            status = node.elem.status()[1]
            if not node is self.current_node:
                self.current_node = node                
                print                
            else:
                txt = u'\r{0} {1} - {2}'.format(artist, title, status).encode('UTF-8', 'ignore')                
                sys.stdout.write(txt)
                sys.stdout.flush()                
            
    def download_tracks(self):
        settings.num_threads = 1 # until this printer becomes more sophisticated
        if not self.tree_model:
            return
        save_dir = settings.save_dir        
        if not os.access(save_dir, os.W_OK):
            msg = 'No write access to save directory. '
            msg += 'Choose a new directory with write privileges.'
            raise IOError(msg)        
        
        self.downloader = Downloader(self.tree_model, self.update_cb, 
                                     self.finished_cb)
        print 
        print '##############################################'
        self.downloader.start()
        self.downloader.join()     

    def get_printable_download_list(self):
        if not self.tree_model:
            return
        root_nodes = self.tree_model.get_root_nodes()
        lines = []
        def add_lines(node, ntabs):
            txt = node.elem.data()
            lines.append(ntabs * self.tab + txt)
            for snode in node.subnodes:
                add_lines(snode, ntabs+1)
        for node in root_nodes:
            add_lines(node, 0)
        return lines
Пример #10
0
class MainWindow(QMainWindow, _ui.Ui_MainWindow):
    
    updateInfo = pyqtSignal()
    resetInfo = pyqtSignal()
    
    def __init__(self, amz_files, parent=None):
        QMainWindow.__init__(self, parent)
        self.setupUi(self)
        
        loadFileIcon = QIcon.fromTheme('document-new', QIcon(QPixmap(load_icon_path)))
        downloadIcon = QIcon.fromTheme('go-down', QIcon(QPixmap(download_icon_path)))
        settingsIcon = QIcon.fromTheme('preferences-other', QIcon(QPixmap(settings_icon_path)))
        showIcon = QIcon.fromTheme('emblem-downloads', QIcon(QPixmap(show_icon_path)))
        exitIcon = QIcon.fromTheme('window-close', QIcon(QPixmap(exit_icon_path)))
        self.pythonPixmap = QPixmap(python_icon_path)
        self.pymazon_text = self.nowDownloadingLabel.text()
        
        self.actionLoadFiles.setIcon(loadFileIcon)
        self.actionDownload.setIcon(downloadIcon)
        self.actionSettings.setIcon(settingsIcon)
        self.actionShowDownloads.setIcon(showIcon)
        self.actionQuit.setIcon(exitIcon)
        self.albumArtLabel.setPixmap(self.pythonPixmap)
        
        self.settingsDialog = SettingsDialog(self)
                
        self.tree_model = None        
        self.pbardelegate = ProgressBarDelegate()
        self.treeView.setItemDelegateForColumn(1, self.pbardelegate)
        
        self.downloader = None
        self.current_album = None
        self.old_albums = []
        self.updateInfo.connect(self.update_album_info)
        self.resetInfo.connect(self.reset_displaybar_info) 
        
        if amz_files:
            self.load_new_amz_files(amz_files)               
        
    @pyqtSlot()
    def on_actionLoadFiles_triggered(self):
        self.new_amz()
        
    @pyqtSlot()
    def on_actionSettings_triggered(self):        
        self.settingsDialog.launch()        
        
    @pyqtSlot()
    def on_actionDownload_triggered(self):        
        self.download_tracks()
        
    @pyqtSlot()
    def on_actionShowDownloads_triggered(self):
        webbrowser.open(settings.save_dir)
        
    @pyqtSlot()
    def on_actionQuit_triggered(self):
        self.close()
        
    @pyqtSlot()
    def on_actionAbout_triggered(self):
        webbrowser.open('http://code.google.com/p/pymazon')
    
    def closeEvent(self, evt):
        if self.downloader:
            self.downloader.kill()
        evt.accept()

    def new_amz(self):
        caption = 'Choose AMZ File'
        filefilter = "Amazon MP3 Download (*.amz)"
        files = [str(f) for f in 
                 QFileDialog.getOpenFileNames(self, caption, '', filefilter)]
        if files:
            self.load_new_amz_files(files)
            
    def load_new_amz_files(self, amz_files):
        parser = AmzParser()
        for f in amz_files:
            parser.parse(f)
        objects = parser.get_parsed_objects()        
        self.tree_model = TreeModel(objects)        
        self.treeView.setModel(self.tree_model) 
        self.setup_treeview()      
    
    def setup_treeview(self):       
        self.treeView.expandAll()
        self.treeView.resizeColumnToContents(0)
        width = self.treeView.columnWidth(0)
        self.treeView.setColumnWidth(1, 100)
        self.treeView.setColumnWidth(0, width + 50)
        
    def download_tracks(self):
        if not self.tree_model:
            return
        save_dir = settings.save_dir        
        if not os.access(save_dir, os.W_OK):
            msg = 'No write access to save directory. '
            msg += 'Choose a new directory with write privileges.'
            QMessageBox.information(self, 'No Access!', msg)
            return        
        self.downloader = Downloader(self.tree_model, self.update_cb, 
                                     self.finished_cb)
        self.actionDownload.setEnabled(False)
        self.actionSettings.setEnabled(False)
        self.actionLoadFiles.setEnabled(False)        
        self.downloader.start()     

    def update_cb(self, node):
        if isinstance(node.elem.obj, Album):
            if not node.elem.obj is self.current_album:
                if node.elem.obj not in self.old_albums:
                    self.old_albums.append(node.elem.obj)
                    self.current_album = node.elem.obj
                    self.updateInfo.emit()  
                else:
                    pass                          
        self.tree_model.update_node(node)
        
    def finished_cb(self):        
        self.downloader = None
        self.resetInfo.emit()
        self.actionDownload.setEnabled(True)
        self.actionSettings.setEnabled(True)
        self.actionLoadFiles.setEnabled(True)
        
    def reset_displaybar_info(self):
        self.current_album = None
        self.old_albums = []
        self.nowDownloadingLabel.setText(self.pymazon_text)
        self.albumArtLabel.setPixmap(self.pythonPixmap)
        
    def update_album_info(self):
        self.update_album_art()
        self.update_downloading_text()
        
    def update_album_art(self):
        img = self.current_album.image
        pm = QPixmap()
        pm.loadFromData(img, None, Qt.AutoColor)
        self.albumArtLabel.setPixmap(pm)        
        
    def update_downloading_text(self):
        name = self.current_album.artist
        title = self.current_album.title
        txt = 'Now dowloading <b>%s</b> by <b>%s</b>' % (title, name)
        self.nowDownloadingLabel.setText(txt)
Пример #11
0
class MainWindow(gobject.GObject):    
    def __init__(self, amzs):
        glade_fpath = os.path.join(os.path.split(__file__)[0], '_ui.glade')
        builder = gtk.Builder()
        builder.add_from_file(glade_fpath)            
        
        self.mainWindow = builder.get_object('MainWindow')
        self.mainWindow.show()
          
        self.albumArtImage = builder.get_object('albumArtImage')
        self.nowDownloadingLabel = builder.get_object('nowDownloadingLabel')
        self.pythonPixbuf = gtk.gdk.pixbuf_new_from_file(python_icon_path)
        self.pymazon_text = '<span weight="bold" size="xx-large">Pymazon! w00t!</span>'
        self.reset_displaybar_info()        
        
        self.settings_dialog = SettingsDialog(builder)             

        self.treeView = builder.get_object('treeView')
        self.tree_model = None 
        
        self.pbarRenderer = ProgressRenderer()       
        self.colStatus = builder.get_object('colStatus')
        self.colStatus.clear()
        self.colStatus.pack_start(self.pbarRenderer, True)
        self.colStatus.add_attribute(self.pbarRenderer, 'value', 1)
        self.colStatus.add_attribute(self.pbarRenderer, 'text', 2)
        
        self.connect_signals(builder)
        
        self.downloader = None
        self.current_album = None
        self.old_albums = []
        if amzs:
            self.load_new_amz_files(amzs)
        
    def connect_signals(self, builder):
        builder.connect_signals({'on_MainWindow_destroy': self.on_destroy})
        self.actionAbout = builder.get_object('actionAbout')
        self.actionAbout.connect('activate', self.on_actionAbout_activate)
        self.actionLoadFiles = builder.get_object('actionLoadFiles')
        self.actionLoadFiles.connect('activate', self.on_actionLoadFiles_activate)
        self.actionSettings = builder.get_object('actionSettings')
        self.actionSettings.connect('activate', self.on_actionSettings_activate)
        self.actionShowDownloads = builder.get_object('actionShowDownloads')
        self.actionShowDownloads.connect('activate', self.on_actionShowDownloads_activate)
        self.actionDownload = builder.get_object('actionDownload')
        self.actionDownload.connect('activate', self.on_actionDownload_activate)      
        self.actionQuit = builder.get_object('actionQuit')
        self.actionQuit.connect('activate', self.on_destroy)        
        
    def on_actionAbout_activate(self, *args):
        webbrowser.open('http://code.google.com/p/pymazon')

    def on_actionLoadFiles_activate(self, *args):
        self.new_amz()

    def on_actionSettings_activate(self, *args):
        self.settings_dialog.launch()

    def on_actionShowDownloads_activate(self, *args):
        webbrowser.open(settings.save_dir)

    def on_actionDownload_activate(self, *args):
        self.download_tracks()

    def on_destroy(self, *arg):
        if self.downloader:
            self.downloader.kill()
        gtk.main_quit()    

    def new_amz(self):
        caption = 'Choose AMZ File'
        filefilter = gtk.FileFilter()        
        filefilter.add_pattern('*.amz')        
        dialog = gtk.FileChooserDialog(caption, self.mainWindow,                                       
                                       buttons=(gtk.STOCK_CANCEL,
                                                gtk.RESPONSE_REJECT,
                                                gtk.STOCK_OK,
                                                gtk.RESPONSE_ACCEPT))
        dialog.set_select_multiple(True)
        dialog.set_filter(filefilter)
        res = dialog.run()
        files = dialog.get_filenames()
        dialog.destroy()
        if res == gtk.RESPONSE_REJECT:
            return
        if files:
            self.load_new_amz_files(files)

    def load_new_amz_files(self, amz_files):
        parser = AmzParser()
        for f in amz_files:
            parser.parse(f)
        objects = parser.get_parsed_objects()        
        self.tree_model = TreeModel(objects)        
        self.treeView.set_model(self.tree_model)
        self.treeView.expand_all()

    def download_tracks(self):
        self.num_threads = 1        
        if not self.tree_model:
            return
        save_dir = settings.save_dir               
        if not os.access(save_dir, os.W_OK):
            print 'here'
            msg = 'No write access to save directory. '
            msg += 'Choose a new directory with write privileges.'
            dialog = gtk.MessageDialog(self.mainWindow, 
                                       buttons=gtk.BUTTONS_OK)
            dialog.set_markup(msg)
            dialog.run()
            dialog.destroy()
            return        
        self.downloader = Downloader(self.tree_model, self.update_cb, 
                                     self.finished_cb)
        self.actionDownload.set_sensitive(False)
        self.actionSettings.set_sensitive(False)
        self.actionLoadFiles.set_sensitive(False)        
        self.downloader.start()            

    def update_cb(self, node):
        if isinstance(node.elem.obj, Album):
            if not node.elem.obj is self.current_album:
                if node.elem.obj not in self.old_albums:
                    self.old_albums.append(node.elem.obj)
                    self.current_album = node.elem.obj
                    self.update_album_info()
                else:
                    pass     
        self.tree_model.update_node(node)
    
    def finished_cb(self):
        self.downloader = None
        self.reset_displaybar_info()
        self.actionDownload.set_sensitive(True)
        self.actionSettings.set_sensitive(True)
        self.actionLoadFiles.set_sensitive(True)
        
    def reset_displaybar_info(self):
        self.current_album = None
        self.old_albums = []        
        self.albumArtImage.set_from_pixbuf(self.pythonPixbuf)
        self.nowDownloadingLabel.set_markup(self.pymazon_text)
        
    def update_album_info(self):
        self.update_album_art()
        self.update_downloading_text()
        
    def update_album_art(self):
        img = self.current_album.image
        loader = gtk.gdk.PixbufLoader()
        loader.write(img)
        loader.close()
        pb = loader.get_pixbuf()
        self.albumArtImage.set_from_pixbuf(pb)
        self.update_downloading_text()
        
    def update_downloading_text(self):
        name = self.current_album.artist
        title = self.current_album.title
        txt = 'Now dowloading <b>%s</b> by <b>%s</b>' % (title, name)
        self.nowDownloadingLabel.set_markup(txt)
Пример #12
0
class CommandLineRunner(object):
    def __init__(self, amzs):
        self.tree_model = None
        self.downloader = None
        self.current_node = None
        self.tab = '    '
        self.load_new_amz_files(amzs)

    def run(self):
        for line in self.get_printable_download_list():
            print line
        print
        print 'Download these items? (Yes/No)'
        res = raw_input()
        if res in ['Yes', 'Y', 'y', 'yes', 'YES']:
            self.download_tracks()

    def load_new_amz_files(self, amz_files):
        parser = AmzParser()
        for f in amz_files:
            parser.parse(f)
        objects = parser.get_parsed_objects()
        self.tree_model = TreeModel()
        self.tree_model.create_tree(objects)

    def finished_cb(self):
        finished = pymazon_finished.format(settings.save_dir,
                                           settings.name_template)
        print
        print finished

    def update_cb(self, node):
        if isinstance(node.elem.obj, Track):
            artist = node.parent.elem.data()
            title = node.elem.data()
            status = node.elem.status()[1]
            if not node is self.current_node:
                self.current_node = node
                print
            else:
                txt = u'\r{0} {1} - {2}'.format(artist, title, status).encode(
                    'UTF-8', 'ignore')
                sys.stdout.write(txt)
                sys.stdout.flush()

    def download_tracks(self):
        settings.num_threads = 1  # until this printer becomes more sophisticated
        if not self.tree_model:
            return
        save_dir = settings.save_dir
        if not os.access(save_dir, os.W_OK):
            msg = 'No write access to save directory. '
            msg += 'Choose a new directory with write privileges.'
            raise IOError(msg)

        self.downloader = Downloader(self.tree_model, self.update_cb,
                                     self.finished_cb)
        print
        print '##############################################'
        self.downloader.start()
        self.downloader.join()

    def get_printable_download_list(self):
        if not self.tree_model:
            return
        root_nodes = self.tree_model.get_root_nodes()
        lines = []

        def add_lines(node, ntabs):
            txt = node.elem.data()
            lines.append(ntabs * self.tab + txt)
            for snode in node.subnodes:
                add_lines(snode, ntabs + 1)

        for node in root_nodes:
            add_lines(node, 0)
        return lines