⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 files.py

📁 ABC-win32-v3.1 一个P2P软源代码
💻 PY
📖 第 1 页 / 共 2 页
字号:
        destination = self.getProcDest()
        
        if destination is None:
            return
      
        # Remove File
        ##################################################        
        done = False
        firsttime = True
        while not done:
            #Wait thread a little bit for returning resource
            ##################################################
            sleep(0.5)
            try:
                if self.isFile():
                    #remove file
                    if os.access(destination, os.F_OK):
                        os.remove(destination)
                else:                  
                    # Only delete files from this torrent
                    # (should be safer this way)
                    subdirs = 0
                    for x in self.torrent.info['files']:
                        filename = destination
                        subdirs = max(subdirs, len(x['path']) - 1)
                        for i in x['path']:
                            filename = os.path.join(filename, i)
                        if os.access(filename, os.F_OK):
                            os.remove(filename)
                    
                    self.utility.RemoveEmptyDir(destination, (subdirs > 0))
                done = True
            except:
                #retry >_<;
                if firsttime:
                    firsttime = False
                    sleep(0.1)
                else:
                    done = True
                    
                    data = StringIO()
                    print_exc(file = data)
                    
                    dialog = wx.MessageDialog(None, self.utility.lang.get('errordeletefile') + "\n" + data.getvalue(), self.utility.lang.get('error'), wx.ICON_ERROR)
                    dialog.ShowModal()
                    dialog.Destroy()
                    
    def getDest(self):
        return self.dest
        
    # Specify where to save the torrent
    def getDestination(self, forceasklocation = False, caller = ""):
        # Set destination location that will be used in next set destination dialog

        # No default directory (or default directory can't be found)
        defaultfolder = self.utility.config.Read('defaultfolder')
        if not os.access(defaultfolder, os.F_OK):
            try:
                os.makedirs(defaultfolder)
            except:
                forceasklocation = True
                
        if ((not self.utility.config.Read('setdefaultfolder', "boolean") or forceasklocation)
            and (caller != "web")):
            success, dest = self.torrent.dialogs.setDestination()
            if not success:
                try:
                    os.remove(dest)
                except:
                    pass
            else:
                if not 'length' in self.torrent.info:     #multi file torrent
                    self.dest = os.path.join(dest, self.filename)
                else:   #1 file for this torrent
                    self.dest = dest
        else:
            self.dest = os.path.join(self.utility.config.Read('defaultfolder'), self.filename)
            
    def getProcDest(self, pathonly = False, checkexists = True):
        # Set it to self.dest (should be fine for files)
        dest = self.dest
        
        # In the case of a multifile torrent, see where we're saving
        if not self.isFile():
            ## see if we're saving to a subdirectory or not
            existing = 0
            if os.path.exists(dest):
                if not os.path.isdir(dest):
                    dest = None
                if os.listdir(dest):  # if it's not empty
                    for x in self.torrent.info['files']:
                        if os.path.exists(os.path.join(dest, x['path'][0])):
                            existing = 1
                    if not existing:
                        dest = os.path.join(dest, self.filename)
        elif pathonly:
            # Strip out just the path for a regular torrent
            dest = os.path.dirname(self.dest)
                        
        if checkexists and dest is not None and not os.access(dest, os.F_OK):
            return None
                        
        return dest
    
    # Used for getting the path for a file in a multi-file torrent
    def getSingleFileDest(self, index = 0, pathonly = False, checkexists = True):
        if self.isFile():
            return self.getProcDest(pathonly, checkexists)
        
        # This isn't a valid file
        if index > len(self.torrent.info['files']):
            return None
            
        fileinfo = self.torrent.info['files'][index]
        dest = self.getProcDest(pathonly = True, checkexists = False)
        for item in fileinfo['path']:
            dest = os.path.join(dest, item)
                        
        if pathonly:
            dest = os.path.dirname(dest)
                   
        if checkexists and dest is not None and not os.access(dest, os.F_OK):
            return None
            
        return dest
        
    def isFile(self):
        return 'length' in self.torrent.info
        
    #
    # Get the total size of all files in the torrent
    #
    # If realsize is True, only return the total size
    # of files that aren't set to "download never"
    #
    def getSize(self, realsize = False):
        if self.isFile():   #1 file for this torrent
            file_length = self.torrent.info['length']
        else:   # Directory torrent
            file_length = 0
            count = 0
            for x in self.torrent.info['files']:
                # If returning the real size, don't include files
                # set to "download never"
                if not realsize or self.filepriorities[count] != -1:
                    file_length += x['length']
                count += 1
        
        return file_length        
        
    def updateRealSize(self):
        self.realsize = self.getSize(realsize = True)
        
        self.torrent.updateColumns([COL_SIZE])
    
    # Set the priorities for all of the files in a multi-file torrent    
    def setFilePriorities(self, priority_array = None):
        if priority_array is not None:
            self.filepriorities = priority_array
            self.torrent.torrentconfig.writeFilePriorities()
            self.updateRealSize()
            self.updateFileProgress()
        
        engine = self.torrent.connection.engine
        if len(self.filepriorities) > 1 and engine is not None and engine.dow is not None:
            engine.dow.fileselector.set_priorities(self.filepriorities)
            
    def getFilePrioritiesAsString(self):
        notdefault = False
        text = ""
        if len(self.filepriorities) > 1:
            for entry in self.filepriorities:
                if entry != 1:
                    notdefault = True
                text += ('%d,' % entry)
            # Remove the trailing ","
            text = text[:-1]

        return notdefault, text
        
    def updateProgress(self):
        # update the download progress
        if self.torrent.status.isActive():
            engine = self.torrent.connection.engine
            self.downsize = engine.downsize['old'] + engine.downsize['new']
            self.upsize = engine.upsize['old'] + engine.upsize['new']
            
            if self.torrent.status.isActive(checking = False, pause = False):
                self.progress = engine.progress
                
                if self.isFile():
                    details = self.torrent.dialogs.details
                    if details is not None:
                        details.fileInfoPanel.updateColumns([FILEINFO_PROGRESS])
                    
                
    def updateFileProgress(self, statistics = None):
        if self.isFile():
            return
        
        # Clear progress for all files that are set to never download
        for i in range(len(self.filepriorities)):
            priority = self.filepriorities[i]
            if priority == -1:
                self.fileprogress[i] = ''
                
        if statistics is not None and statistics.filelistupdated.isSet():
            for i in range(len(statistics.filecomplete)):
                progress = None
                
                if self.filepriorities[i] == -1:
                    # Not download this file
                    progress = ''
                elif statistics.fileinplace[i]:
                    # File is done
                    progress = self.utility.lang.get('done')
                elif statistics.filecomplete[i]:
                    # File is at complete, but not done
                    progress = "100%"
                else:
                    # File isn't complete yet
                    frac = statistics.fileamtdone[i]
                    if frac:
                        progress = '%d%%' % (frac*100)
                    else:
                        progress = ''
                if progress is None:
                    progress = ''
                    
                self.fileprogress[i] = progress
                
            statistics.filelistupdated.clear()
        
        details = self.torrent.dialogs.details
        if details is not None:
            details.fileInfoPanel.updateColumns([FILEINFO_SIZE, FILEINFO_PROGRESS])

    #
    # See how much more space is allocated to this torrent
    #
    def getSpaceAllocated(self):
        allocated = 0L
        if self.isFile():
            if os.path.exists(self.dest):
                allocated = os.path.getsize(self.dest)
        else:
            count = 0
            for f in self.torrent.info['files']:
                # Don't include space taken by disabled files
                if self.filepriorities[count] != -1:
                    filename = self.getProcDest()
                    for item in f['path']:
                        filename = os.path.join(filename, item)
                    if os.path.exists(filename):
                        allocated += os.path.getsize(filename)
                count += 1
                    
        return allocated
    
    #
    # See how much space is needed by this torrent
    #
    def getSpaceNeeded(self, realsize = True):
        # Shouldn't need any more space if the file is complete
        if self.torrent.status.completed:
            return 0L
        
        # See how much space the torrent needs vs. how much is already allocated
        space = self.getSize(realsize = realsize) - self.getSpaceAllocated() 
        if space < 0:
            space = 0L
        return space
        
        

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -