📄 abctorrent.py
字号:
import sys
import wx
import os
from cStringIO import StringIO
from sha import sha
from time import strftime, localtime, time
from traceback import print_exc
from BitTornado.bencode import bencode
from ABC.Torrent.files import TorrentFiles
from ABC.Torrent.connectmanager import TorrentConnections
from ABC.Torrent.actions import TorrentActions
from ABC.Torrent.config import TorrentConfig
from ABC.Torrent.dialogs import TorrentDialogs
from ABC.Torrent.status import TorrentStatus
from Utility.constants import * #IGNORE:W0611
################################################################
#
# Class: ABCTorrent
#
# Stores information about a torrent and keeps track of its
# status
#
################################################################
class ABCTorrent:
def __init__(self, queue, src = None, dest = None, forceasklocation = False, caller = ""):
self.queue = queue
self.utility = self.queue.utility
self.list = self.utility.list
self.listindex = len(self.utility.torrents["all"])
self.src = src
self.status = TorrentStatus(self)
self.actions = TorrentActions(self)
self.dialogs = TorrentDialogs(self)
self.connection = TorrentConnections(self)
#########
self.metainfo = self.getResponse()
if self.metainfo is None:
return
# Get infohash first before doing anything else
self.infohash = sha(bencode(self.metainfo['info'])).hexdigest()
self.torrentconfig = TorrentConfig(self)
# Check for valid windows filename
if sys.platform == 'win32':
fixedname = self.utility.fixWindowsName(self.metainfo['info']['name'])
if fixedname:
self.metainfo['info']['name'] = fixedname
self.info = self.metainfo['info']
self.title = None
# Initialize values to defaults
self.files = TorrentFiles(self)
# Setup the destination
self.files.setupDest(dest, forceasklocation, caller)
if self.files.dest is None:
return
#########
# Priority "Normal"
priorities = [ self.utility.lang.get('highest'),
self.utility.lang.get('high'),
self.utility.lang.get('normal'),
self.utility.lang.get('low'),
self.utility.lang.get('lowest') ]
currentprio = self.utility.config.Read('defaultpriority', "int")
if currentprio < 0:
currentprio = 0
elif currentprio >= len(priorities):
currentprio = len(priorities) - 1
self.prio = currentprio
self.color = { 'text': None,
'bgcolor': None }
# Done flag
self.messages = { "current": "",
"log": [],
"timer": None }
self.checkedonce = False
self.totalpeers = "?"
self.totalseeds = "?"
#
# Tasks to perform when first starting adding this torrent to the display
#
def postInitTasks(self):
self.utility.torrents["all"].append(self)
self.utility.torrents["inactive"][self] = 1
# Read extra information about the torrent
self.torrentconfig.readAll()
# Add a new item to the list
self.list.InsertStringItem(self.listindex, "")
# Allow updates
self.status.dontupdate = False
# Add Status info in List
self.updateColumns(force = True)
self.updateColor()
# Update the size to reflect torrents with pieces set to "download never"
self.files.updateRealSize()
# Do a quick check to see if it's finished
self.status.isDoneUploading()
#
# As opposed to getColumnText,
# this will get numbers in their raw form for doing comparisons
#
# default is used when getting values for sorting comparisons
# (this way an empty string can be treated as less than 0.0)
#
def getColumnValue(self, colid = None, default = 0.0):
if colid is None:
colid = COL_TITLE
value = None
activetorrent = self.status.isActive(checking = False, pause = False)
try:
if colid == COL_PROGRESS: # Progress
progress = self.files.progress
if self.status.isActive(pause = False):
progress = self.connection.engine.progress
value = progress
elif colid == COL_PRIO: # Priority
value = self.prio
elif colid == COL_ETA: # ETA
if activetorrent:
if self.status.completed:
if self.connection.getSeedOption('uploadoption') == '0':
value = 999999999999999
else:
value = self.connection.seedingtimeleft
elif self.connection.engine.eta is not None:
value = self.connection.engine.eta
elif colid == COL_SIZE: # Size
value = self.files.floattotalsize
elif colid == COL_DLSPEED: # DL Speed
if activetorrent and not self.status.completed:
if self.connection.engine.hasConnections:
value = self.connection.engine.rate['down']
else:
value = 0.0
elif colid == COL_ULSPEED: # UL Speed
if activetorrent:
if self.connection.engine.hasConnections:
value = self.connection.engine.rate['up']
else:
value = 0.0
elif colid == COL_RATIO: # %U/D Size
if self.files.downsize == 0.0 :
ratio = ((self.files.upsize/self.files.floattotalsize) * 100)
else:
ratio = ((self.files.upsize/self.files.downsize) * 100)
value = ratio
elif colid == COL_SEEDS: # #Connected Seed
if activetorrent:
value = self.connection.engine.numseeds
elif colid == COL_PEERS: # #Connected Peer
if activetorrent:
value = self.connection.engine.numpeers
elif colid == COL_COPIES: # #Seeing Copies
if (activetorrent
and self.connection.engine.numcopies is not None):
value = float(0.001*int(1000*self.connection.engine.numcopies))
elif colid == COL_PEERPROGRESS: # Peer Avg Progress
if (self.connection.engine is not None
and self.connection.engine.peeravg is not None):
value = self.connection.engine.peeravg
elif colid == COL_DLSIZE: # Download Size
value = self.files.downsize
elif colid == COL_ULSIZE: # Upload Size
value = self.files.upsize
elif colid == COL_TOTALSPEED: # Total Speed
if activetorrent:
value = self.connection.engine.totalspeed
elif colid == COL_SEEDTIME: # Seeding time
value = self.connection.seedingtime
elif colid == COL_CONNECTIONS: # Connections
if activetorrent:
value = self.connection.engine.numconnections
elif colid == COL_SEEDOPTION: # Seeding option
option = int(self.connection.getSeedOption('uploadoption'))
if option == 0:
# Unlimited
value = 0.0
elif option == 1:
text = "1." + str(self.connection.getTargetSeedingTime())
value = float(text)
elif option == 2:
text = "1." + str(self.connection.getSeedOption('uploadratio'))
value = float(text)
else:
value = self.getColumnText(colid)
except:
value = self.getColumnText(colid)
if value is None or value == "":
return default
return value
#
# Get the text representation of a given column's data
# (used for display)
#
def getColumnText(self, colid):
text = None
activetorrent = self.status.isActive(checking = False, pause = False)
try:
if colid == COL_TITLE: # Title
if self.title is None:
text = self.files.filename
else:
text = self.title
elif colid == COL_PROGRESS: # Progress
progress = self.files.progress
if self.status.isActive(pause = False):
progress = self.connection.engine.progress
# Truncate the progress value rather than round down
# (will show 99.9% for incomplete torrents rather than 100.0%)
progress = int(progress * 10)/10.0
text = ('%.1f' % progress) + "%"
elif colid == COL_BTSTATUS: # BT Status
text = self.status.getStatusText()
elif colid == COL_PRIO: # Priority
priorities = [ self.utility.lang.get('highest'),
self.utility.lang.get('high'),
self.utility.lang.get('normal'),
self.utility.lang.get('low'),
self.utility.lang.get('lowest') ]
text = priorities[self.prio]
elif colid == COL_ETA and activetorrent: # ETA
value = None
if self.status.completed:
if self.connection.getSeedOption('uploadoption') == "0":
text = "(oo)"
else:
value = self.connection.seedingtimeleft
text = "(" + self.utility.eta_value(value) + ")"
elif self.connection.engine.eta is not None:
value = self.connection.engine.eta
text = self.utility.eta_value(value)
elif colid == COL_SIZE: # Size
# Some file pieces are set to "download never"
if self.files.floattotalsize != self.files.realsize:
label = self.utility.size_format(self.files.floattotalsize, textonly = True)
realsizetext = self.utility.size_format(self.files.realsize, truncate = 1, stopearly = label, applylabel = False)
totalsizetext = self.utility.size_format(self.files.floattotalsize, truncate = 1)
text = realsizetext + "/" + totalsizetext
else:
text = self.utility.size_format(self.files.floattotalsize)
elif (colid == COL_DLSPEED
and activetorrent
and not self.status.completed): # DL Speed
if self.connection.engine.hasConnections:
value = self.connection.engine.rate['down']
else:
value = 0.0
text = self.utility.speed_format(value)
elif colid == COL_ULSPEED and activetorrent: # UL Speed
if self.connection.engine.hasConnections:
value = self.connection.engine.rate['up']
else:
value = 0.0
text = self.utility.speed_format(value)
elif colid == COL_RATIO: # %U/D Size
if self.files.downsize == 0.0 :
ratio = ((self.files.upsize/self.files.floattotalsize) * 100)
else:
ratio = ((self.files.upsize/self.files.downsize) * 100)
text = '%.1f' % (ratio) + "%"
elif colid == COL_MESSAGE: # Error Message
text = self.messages["current"]
# If the error message is a system traceback, write an error
if text.find("Traceback") != -1:
sys.stderr.write(text + "\n")
elif colid == COL_SEEDS: # #Connected Seed
seeds = "0"
if activetorrent:
seeds = ('%d' % self.connection.engine.numseeds)
text = seeds + " (" + str(self.totalseeds) + ")"
elif colid == COL_PEERS: # #Connected Peer
peers = "0"
if activetorrent:
peers = ('%d' % self.connection.engine.numpeers)
text = peers + " (" + str(self.totalpeers) + ")"
elif (colid == COL_COPIES
and activetorrent
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -