📄 utility.py
字号:
import wx
import sys
import os
import socket
import random
from Lang.lang import Lang
from threading import Event, Semaphore
from time import sleep
#from traceback import print_exc
#from cStringIO import StringIO
from wx.lib import masked
from BitTornado.ConfigDir import ConfigDir
from BitTornado.bencode import bdecode
from BitTornado.download_bt1 import defaults as BTDefaults
from BitTornado.parseargs import parseargs
from BitTornado.zurllib import urlopen
from ABC.Actions.actions import makeActionList
if (sys.platform == 'win32'):
from Utility.regchecker import RegChecker
from Utility.configreader import ConfigReader
from Utility.compat import convertINI, moveOldConfigFiles
from Utility.constants import * #IGNORE:W0611
################################################################
#
# Class: Utility
#
# Generic "glue" class that contains commonly used helper
# functions and helps to keep track of objects
#
################################################################
class Utility:
def __init__(self, abcpath):
self.abcpath = abcpath
# Find the directory to save config files, etc.
self.setupConfigPath()
moveOldConfigFiles(self)
self.setupConfig()
# Setup language files
self.lang = Lang(self)
# Convert old INI file
convertINI(self)
# Make torrent directory (if needed)
self.MakeTorrentDir()
self.setupWebConfig()
self.setupTorrentMakerConfig()
self.setupTorrentList()
self.torrents = { "all": [],
"active": {},
"inactive": {},
"pause": {},
"seeding": {},
"downloading": {} }
self.accessflag = Event()
self.accessflag.set()
self.invalidwinfilenamechar = ''
for i in range(32):
self.invalidwinfilenamechar += chr(i)
self.invalidwinfilenamechar += '"*/:<>?\\|'
self.FILESEM = Semaphore(1)
if (sys.platform == 'win32'):
self.regchecker = RegChecker(self)
self.lastdir = { "save" : self.config.Read('defaultfolder'),
"open" : "",
"log": "" }
# Is ABC in the process of shutting down?
self.abcquitting = False
# self.abcdonequitting = False
# Keep track of the last tab that was being viewed
self.lasttab = { "advanced" : 0,
"preferences" : 0 }
self.languages = {}
# Keep track of all the "ManagedList" objects in use
self.lists = {}
def setupConfigPath(self):
configdir = ConfigDir()
self.dir_root = configdir.dir_root
def getConfigPath(self):
return self.dir_root
def setupConfig(self):
defaults = {
'defrentorwithdest': '1',
'minport': str(random.randint(10000, 60000)),
'maxport': '50000',
'maxupload': '5',
'maxuploadrate': '0',
'maxdownloadrate': '0',
'maxseeduploadrate': '0',
'numsimdownload': '2',
'uploadoption': '2',
'uploadtimeh': '0',
'uploadtimem': '30',
'uploadratio': '100',
'removetorrent': '0',
'setdefaultfolder': '0',
'defaultfolder': 'c:\\',
'defaultmovedir': 'c:\\',
'mintray': '0',
'trigwhenfinishseed': '1',
'confirmonclose': '1',
'kickban': '1',
'notsameip': '1',
'ipv6': '0',
'ipv6_binds_v4': '1',
'min_peers': '20',
'max_initiate': '40',
'alloc_type': 'normal',
'alloc_rate': '2',
'max_files_open': '50',
'max_connections': '0',
'lock_files': '1',
'lock_while_reading': '0',
'double_check': '1',
'triple_check': '0',
'timeouttracker': '15',
'timeoutdownload': '30',
'timeoutupload': '1',
'scrape': '0',
'defaultpriority': '2',
'failbehavior': '0',
'language_file': 'english.lang',
'urm': '0',
'urmupthreshold': '10',
'urmdelay': '60',
'stripedlist': '0',
# 'mode': '1',
'window_width': '710',
'window_height': '400',
'detailwindow_width': '610',
'detailwindow_height': '500',
'prefwindow_width': '530',
'prefwindow_height': '400',
'prefwindow_split': '130',
'column4_rank': '0', # Title
'column4_width': '150',
'column5_rank': '1', # Progress
'column5_width': '160',
'column6_rank': '2', # BT Status
'column6_width': '100',
'column7_rank': '8', # Priority
'column7_width': '50',
'column8_rank': '5', # ETA
'column8_width': '85',
'column9_rank': '6', # Size
'column9_width': '75',
'column10_rank': '3', # DL Speed
'column10_width': '65',
'column11_rank': '4', # UL Speed
'column11_width': '60',
'column12_rank': '7', # %U/D Size
'column12_width': '150',
'column13_rank': '9', # Error Message
'column13_width': '60',
'column14_rank': '-1', # #Connected Seed
'column14_width': '60',
'column15_rank': '-1', # #Connected Peer
'column15_width': '60',
'column16_rank': '-1', # #Seeing Copies
'column16_width': '60',
'column17_rank': '-1', # Peer Avg Progress
'column17_width': '60',
'column18_rank': '-1', # Download Size
'column18_width': '75',
'column19_rank': '-1', # Upload Size
'column19_width': '75',
'column20_rank': '-1', # Total Speed
'column20_width': '80',
'column21_rank': '-1', # Torrent Name
'column21_width': '150',
'column22_rank': '-1', # Destination
'column22_width': '150',
'column23_rank': '-1', # Seeding Time
'column23_width': '85',
'column24_rank': '-1', # Connections
'column24_width': '60',
'column25_rank': '-1', # Seeding Option
'column25_width': '80',
'fastresume': '1',
'randomport': '1',
'savecolumnwidth': '1',
# 'forcenewdir': '1',
'upnp_nat_access': '0',
'buffer_write' : '4',
'buffer_read' : '1',
'auto_flush' : '0',
'associate' : '1',
'movecompleted': '0',
'spew0_rank': '0', # Optimistic Unchoke
'spew0_width': '24',
'spew1_rank': '1', # IP
'spew1_width': '132',
'spew2_rank': '2', # Local / Remote
'spew2_width': '24',
'spew3_rank': '3', # Upload Rate
'spew3_width': '72',
'spew4_rank': '4', # Interested
'spew4_width': '24',
'spew5_rank': '5', # Choking
'spew5_width': '24',
'spew6_rank': '6', # Download Rate
'spew6_width': '72',
'spew7_rank': '7', # Interesting
'spew7_width': '24',
'spew8_rank': '8', # Choked
'spew8_width': '24',
'spew9_rank': '9', # Snubbed
'spew9_width': '24',
'spew10_rank': '10', # Downloaded
'spew10_width': '84',
'spew11_rank': '11', # Uploaded
'spew11_width': '84',
'spew12_rank': '12', # Peer Progress
'spew12_width': '72',
'spew13_rank': '-1', # Peer Download Speed
'spew13_width': '72',
'fileinfo0_rank': '0', # Filename
'fileinfo0_width': '300',
'fileinfo1_rank': '1', # Size
'fileinfo1_width': '100',
'fileinfo2_rank': '2', # Progress
'fileinfo2_width': '60',
'fileinfo3_rank': '3', # MD5 Hash
'fileinfo3_width': '200',
'fileinfo4_rank': '-1', # CRC32 Hash
'fileinfo4_width': '200',
'fileinfo5_rank': '-1', # SHA1 Hash
'fileinfo5_width': '200',
'fileinfo6_rank': '-1', # ED2K Hash
'fileinfo6_width': '200',
'color_startup': '000000000',
'color_disconnected': '100100100',
'color_noconnections': '200000000',
'color_noincoming': '150150000',
'color_nocomplete': '000000150',
'color_good': '000150000',
'color_stripe': '245245245',
'display_interval': '0.8',
'listfont': '',
'diskfullthreshold': '0',
# 'showmenuicons': '1',
'icons_toolbarbottom': [ACTION_MOVEUP,
ACTION_MOVEDOWN,
ACTION_MOVETOP,
ACTION_MOVEBOTTOM,
-1,
ACTION_CLEARCOMPLETED,
-1,
ACTION_PAUSEALL,
ACTION_STOPALL,
ACTION_UNSTOPALL,
-1],
'icons_toolbartop': [ACTION_ADDTORRENT,
ACTION_ADDTORRENTNONDEFAULT,
ACTION_ADDTORRENTURL,
-1,
ACTION_RESUME,
ACTION_PAUSE,
ACTION_STOP,
ACTION_QUEUE,
ACTION_REMOVE,
-1,
ACTION_SCRAPE,
ACTION_DETAILS],
'menu_listrightclick': [ACTION_RESUME,
ACTION_STOP,
ACTION_PAUSE,
ACTION_QUEUE,
ACTION_HASHCHECK,
-1,
ACTION_REMOVE,
ACTION_REMOVEFILE,
ACTION_EXPORTMENU,
ACTION_CLEARMESSAGE,
-1,
ACTION_LOCALUPLOAD,
ACTION_CHANGEPRIO,
-1,
ACTION_OPENFILEDEST,
ACTION_OPENDEST,
ACTION_CHANGEDEST,
-1,
ACTION_SCRAPE,
ACTION_DETAILS],
# 'skipcheck': '0'
}
configfilepath = os.path.join(self.getConfigPath(), "abc.conf")
self.config = ConfigReader(configfilepath, "ABC", defaults)
# self.config = ConfigReader(configfilepath, "ABC")
# self.config.defaults = defaults
def setupWebConfig(self):
defaults = {
'webID': 'yourkeyword',
'webIP': '127.0.0.1',
'webport': '56667',
'webautostart': '0',
'allow_query': '1',
'allow_delete': '1',
'allow_clearcompleted': '1',
'allow_add': '1',
'allow_setparam': '0',
'allow_getparam': '0',
'allow_queue': '1',
'allow_pause': '1',
'allow_stop': '1',
'allow_resume': '1',
'allow_setprio': '1',
}
webconfigfilepath = os.path.join(self.getConfigPath(), "webservice.conf")
self.webconfig = ConfigReader(webconfigfilepath, "ABC/Webservice", defaults)
def setupTorrentMakerConfig(self):
defaults = {
'piece_size': '0',
'comment': '',
'created_by': '',
'announcedefault': '',
'announcehistory': '',
'announce-list': '',
'httpseeds': '',
'makehash_md5': '0',
'makehash_crc32': '0',
'makehash_sha1': '0',
'startnow': '0',
'savetorrent': '2'
}
torrentmakerconfigfilepath = os.path.join(self.getConfigPath(), "maker.conf")
self.makerconfig = ConfigReader(torrentmakerconfigfilepath, "ABC/TorrentMaker", defaults)
def setupTorrentList(self):
torrentfilepath = os.path.join(self.getConfigPath(), "torrent.list")
self.torrentconfig = ConfigReader(torrentfilepath, "list0")
# Initialization that has to be done after the wx.App object
# has been created
def postAppInit(self):
try:
self.icon = wx.Icon(os.path.join(self.getPath(), 'icon_abc.ico'), wx.BITMAP_TYPE_ICO)
except:
pass
makeActionList(self)
def getLastDir(self, operation = "save"):
lastdir = self.lastdir[operation]
if operation == "save":
if not os.access(lastdir, os.F_OK):
lastdir = self.config.Read('defaultfolder')
if not os.access(lastdir, os.F_OK):
lastdir = ""
return lastdir
def getPath(self):
return self.abcpath
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -