📄 abcdetailframe.py
字号:
import sys
import wx
import re
import os
import binascii
from threading import Thread
from time import time, localtime, strftime
from webbrowser import open_new
from traceback import print_exc
from cStringIO import StringIO
from ABC.GUI.list import ManagedList
from Utility.constants import * #IGNORE:W0611
################################################################
#
# Class: TorrentInfoPanel
#
# Displays BitTorrent-related information, such as trackers,
# etc.
#
################################################################
class TorrentInfoPanel(wx.Panel):
def __init__(self, parent, dialog):
wx.Panel.__init__(self, parent, -1)
self.dialog = dialog
self.utility = dialog.utility
self.torrent = dialog.torrent
metainfo = dialog.metainfo
self.fileList = None
self.refresh_detail = False
announce = metainfo.get('announce', None)
announce_list = metainfo.get('announce-list', None)
http_seeds = metainfo.get('httpseeds', None)
info = metainfo['info']
piece_length = info['piece length']
colSizer = wx.BoxSizer(wx.VERTICAL)
detailSizer = wx.FlexGridSizer(cols = 2, vgap = 6, hgap = 10)
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('infohash')), 0, wx.ALIGN_CENTER_VERTICAL)
detailSizer.Add(wx.TextCtrl(self, -1, self.torrent.infohash, style = wx.TE_READONLY), 1, wx.EXPAND)
num_pieces = int((self.torrent.files.getSize() + piece_length - 1)/piece_length)
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('pieces')))
if num_pieces > 1:
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('str2') % (num_pieces, self.comma_format(piece_length))))
else:
detailSizer.Add(wx.StaticText(self, -1, '1'))
if 'encoding' in metainfo and metainfo['encoding'].strip():
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('encoding')))
detailSizer.Add(wx.StaticText(self, -1, metainfo['encoding']))
if announce_list is None:
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('announceurl')), 0, wx.ALIGN_CENTER_VERTICAL)
detailSizer.Add(wx.TextCtrl(self, -1, announce, style = wx.TE_READONLY), 1, wx.EXPAND)
else:
detailSizer.Add(wx.StaticText(self, -1, ''))
self.trackerList = trackerList = wx.ListCtrl(self, -1, size = (-1, 100), style = wx.LC_REPORT)
trackerList.SetAutoLayout(True)
trackerList.InsertColumn(0, "")
trackerList.InsertColumn(1, self.utility.lang.get('announceurls'))
for tier in range(len(announce_list)):
for t in range(len(announce_list[tier])):
i = wx.ListItem()
trackerList.InsertItem(i)
if announce is not None:
for l in [1, 2]:
i = wx.ListItem()
trackerList.InsertItem(i)
x = 0
for tier in range(len(announce_list)):
for t in range(len(announce_list[tier])):
if t == 0:
trackerList.SetStringItem(x, 0, self.utility.lang.get('tier') + str(tier)+':')
trackerList.SetStringItem(x, 1, announce_list[tier][t])
x += 1
if announce is not None:
trackerList.SetStringItem(x+1, 0, self.utility.lang.get('single'))
trackerList.SetStringItem(x+1, 1, announce)
trackerList.SetColumnWidth(0, wx.LIST_AUTOSIZE)
trackerList.SetColumnWidth(1, wx.LIST_AUTOSIZE)
detailSizer.Add(trackerList, 1, wx.EXPAND)
self.trackerList.Bind(wx.EVT_LIST_ITEM_RIGHT_CLICK, self.onAnnounceListRightClick)
if announce is None and announce_list is not None:
announce = announce_list[0][0]
if announce is not None:
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('likelytracker')))
p = re.compile('(.*/)[^/]+')
self.turl = p.sub (r'\1', announce)
trackerUrl = wx.StaticText(self, -1, self.turl)
trackerUrl.SetForegroundColour('Blue')
detailSizer.Add(trackerUrl)
trackerUrl.Bind(wx.EVT_LEFT_DOWN, self.trackerurl, trackerUrl)
if http_seeds is not None:
httpseedtext = ""
for httpseed in http_seeds:
httpseedtext += httpseed + "\n"
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('httpseeds')))
detailSizer.Add(wx.TextCtrl(self, -1, httpseedtext, size = (-1, 100), style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_DONTWRAP|wx.TE_READONLY), 1, wx.EXPAND)
if 'creation date' in metainfo:
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('creationdate')))
try:
datetext = strftime('%x %X', localtime(metainfo['creation date']))
except:
try:
datetext = metainfo['creation date']
except:
datetext = '<cannot read date>'
detailSizer.Add(wx.StaticText(self, -1, datetext))
if 'created by' in metainfo and metainfo['created by'].strip():
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('createdby')))
detailSizer.Add(wx.StaticText(self, -1, metainfo['created by']))
if 'comment' in metainfo and metainfo['comment'].strip():
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('comment')))
detailSizer.Add(wx.TextCtrl(self, -1, metainfo['comment'], style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_DONTWRAP|wx.TE_READONLY), 1, wx.EXPAND)
detailSizer.AddGrowableCol(1)
colSizer.Add(detailSizer, 0, wx.EXPAND|wx.ALL, 5)
self.SetSizer(colSizer)
self.SetAutoLayout(True)
def onAnnCopy(self, event = None):
# Copy the selected announce url to the clipboard
if wx.TheClipboard.Open():
data = wx.TextDataObject(self.trackerList.GetItem(self.trackerList.GetNextItem(-1, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED), 1).GetText())
wx.TheClipboard.SetData(data)
wx.TheClipboard.Close()
# Popup menu for tracker list
def onAnnounceListRightClick(self, event):
menu = wx.Menu()
self.utility.makePopup(menu, self.onAnnCopy, 'copybtn')
self.PopupMenu(menu, event.GetPosition() + self.trackerList.GetPosition())
def comma_format(self, s):
r = str(s)
for i in range(len(r)-3, 0, -3):
r = r[:i]+','+r[i:]
return(r)
def trackerurl(self, event = None):
if self.turl is not None:
try:
Thread(target = open_new(self.turl)).start()
except:
pass
################################################################
#
# Class: FileInfoPanel
#
# Displays information about the file(s) within a torrent
#
################################################################
class FileInfoPanel(wx.Panel):
def __init__(self, parent, dialog):
wx.Panel.__init__(self, parent, -1)
self.dialog = dialog
self.utility = dialog.utility
self.torrent = dialog.torrent
metainfo = dialog.metainfo
self.refresh_detail = False
info = metainfo['info']
colSizer = wx.BoxSizer(wx.VERTICAL)
detailSizer = wx.FlexGridSizer(cols = 2, vgap = 6, hgap = 10)
#Folder File
detailSizer = wx.FlexGridSizer(cols = 2, vgap = 6, hgap = 10)
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('destination')), 0, wx.ALIGN_CENTER_VERTICAL)
self.opendirbtn = wx.Button(self, -1, self.torrent.files.getProcDest(pathonly = True, checkexists = False), style = wx.BU_EXACTFIT)
detailSizer.Add(self.opendirbtn, 0, wx.ALIGN_CENTER_VERTICAL)
self.opendirbtn.Bind(wx.EVT_LEFT_DOWN, self.onDestButtonClick)
self.opendirbtn.Bind(wx.EVT_RIGHT_DOWN, self.onDestButtonClick)
if 'length' in info:
#Single File
numfiles = 1
name = self.utility.lang.get('filesize')
else:
#Folder File
numfiles = len(info['files'])
name = self.utility.lang.get('archivesize')
detailSizer.Add(wx.StaticText(self, -1, name + ' : '))
detailSizer.Add(wx.StaticText(self, -1, self.utility.lang.get('str1') % (self.utility.size_format(self.torrent.files.getSize()), self.comma_format(self.torrent.files.getSize()))))
colSizer.Add(detailSizer, 0, wx.ALL, 5)
self.fileList = fileList = FileInfoList(self)
for i in range(numfiles):
x = wx.ListItem()
fileList.InsertItem(x)
x = 0
self.updateColumns()
colSizer.Add(fileList, 1, wx.EXPAND|wx.ALL, 5)
self.SetSizer(colSizer)
self.SetAutoLayout(True)
if self.torrent.status.isActive():
self.torrent.connection.engine.dow.filedatflag.set()
def onDestButtonClick(self, event):
# Popup menu for tracker list
menu = wx.Menu()
self.utility.makePopup(menu, self.torrent.files.onOpenDest, 'ropendest')
self.utility.makePopup(menu, self.onCopyPath, 'rcopypath')
self.utility.makePopup(menu, self.onChangeDest, 'rchangedownloaddest')
self.PopupMenu(menu, event.GetPosition() + self.opendirbtn.GetPosition())
def onChangeDest(self, event = None):
self.torrent.dialogs.changeDest(parent = self)
def onCopyPath(self, event = None):
# Get the filenames
text = self.torrent.files.getSingleFileDest(0, pathonly = True, checkexists = False)
# Copy the text to the clipboard
if wx.TheClipboard.Open():
data = wx.TextDataObject(text)
wx.TheClipboard.SetData(data)
wx.TheClipboard.Close()
def updateColumns(self, columnlist = None, force = False):
priorities = self.torrent.files.filepriorities
info = self.dialog.metainfo['info']
fileList = self.fileList
if self.torrent.files.isFile():
filesinfo = [info]
else:
filesinfo = info['files']
x = 0
try:
for tempfile in filesinfo:
for colid, rank in fileList.columns.active:
if columnlist is None or colid in columnlist:
fileList.SetStringItem(x, rank, self.getFileColumnText(colid, tempfile, x))
p = priorities[x]
item = self.fileList.GetItem(x)
item.SetTextColour(self.fileList.prioritycolors[p+1])
fileList.SetItem(item)
x += 1
except wx.PyDeadObjectError:
pass
def getFileColumnText(self, colid, tempfile, index = 0):
text = None
if colid == FILEINFO_FILENAME:
if self.torrent.files.isFile():
text = os.path.split(self.torrent.files.getProcDest(pathonly = False, checkexists = False))[1]
else:
path = ' '
for item in tempfile['path']:
if (path != ''):
path = path + "/"
path = path + item
text = path
elif colid == FILEINFO_SIZE:
if self.torrent.files.isFile() or self.torrent.files.filepriorities[index] != -1:
text = self.comma_format(tempfile['length']) + ' ' + self.utility.lang.get('Byte')
elif colid == FILEINFO_PROGRESS:
if self.torrent.files.isFile():
if not self.torrent.status.isCheckingOrAllocating():
if self.torrent.status.completed:
text = self.utility.lang.get('done')
else:
text = self.torrent.getColumnText(COL_PROGRESS)
else:
text = self.torrent.files.fileprogress[index]
elif colid == FILEINFO_MD5:
if 'md5sum' in tempfile:
text = str(tempfile['md5sum'])
elif colid == FILEINFO_CRC32:
if 'crc32' in tempfile:
text = str(tempfile['crc32'])
elif colid == FILEINFO_SHA1:
if 'sha1' in tempfile:
text = binascii.b2a_hex(tempfile['sha1'])
elif colid == FILEINFO_ED2K:
if 'ed2k' in tempfile:
text = binascii.b2a_hex(tempfile['ed2k'])
if text is None:
text = ""
return text
def comma_format(self, s):
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -