📄 btmaketorrentgui.py
字号:
if Read is None: Read = self.utility.makerconfig.Read self.startnow.SetValue(Read('startnow', "boolean")) self.makehash_md5.SetValue(Read('makehash_md5', "boolean")) self.makehash_crc32.SetValue(Read('makehash_crc32', "boolean")) self.makehash_sha1.SetValue(Read('makehash_sha1', "boolean")) self.savetor[Read('savetorrent', "int")].SetValue(True) self.piece_length.SetSelection(Read('piece_size', "int")) self.savetordeftext.SetValue(Read('savetordeffolder')) def saveConfig(self, event = None): self.utility.makerconfig.Write('startnow', self.startnow.GetValue(), "boolean") self.utility.makerconfig.Write('makehash_md5', self.makehash_md5.GetValue(), "boolean") self.utility.makerconfig.Write('makehash_crc32', self.makehash_crc32.GetValue(), "boolean") self.utility.makerconfig.Write('makehash_sha1', self.makehash_sha1.GetValue(), "boolean") self.utility.makerconfig.Write('savetordeffolder', self.savetordeftext.GetValue()) for i in range(3): if self.savetor[i].GetValue(): self.utility.makerconfig.Write('savetorrent', i) break self.utility.makerconfig.Write('piece_size', self.piece_length.GetSelection()) def selectDir(self, event = None): dlg = wx.DirDialog(self.dialog, self.utility.lang.get('selectdir'), style = wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON) if dlg.ShowModal() == wx.ID_OK: self.dirCtl.SetValue(dlg.GetPath()) dlg.Destroy() def onBrowseDir(self, event = None): dlg = wx.DirDialog(self.dialog, self.utility.lang.get('choosetordeffolder'), style = wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON) if dlg.ShowModal() == wx.ID_OK: self.savetordeftext.SetValue(dlg.GetPath()) dlg.Destroy() def selectFile(self, event = None): dlg = wx.FileDialog(self.dialog, self.utility.lang.get('choosefiletouse'), '', '', self.utility.lang.get('allfileswildcard') + ' (*.*)|*.*', wx.OPEN) if dlg.ShowModal() == wx.ID_OK: self.dirCtl.SetValue(dlg.GetPath()) dlg.Destroy() def selectdrop(self, x): list = x.m_files self.dirCtl.SetValue(x[0]) def getParams(self): params = {} self.targeted = [] params['piece_size_pow2'] = self.piece_length_list[self.piece_length.GetSelection()] gethash = {} if self.makehash_md5.GetValue(): gethash['md5'] = True if self.makehash_crc32.GetValue(): gethash['crc32'] = True if self.makehash_sha1.GetValue(): gethash['sha1'] = True params['gethash'] = gethash## for i in range(3): if self.savetor[i].GetValue(): break if i == 0: defdestfolder = self.savetordeftext.GetValue() # # Check if default download folder is not a file and create it if necessary if exists(defdestfolder): if not isdir(defdestfolder): dlg = wx.MessageDialog(self, message = self.utility.lang.get('notadir') + '\n' + \ self.utility.lang.get('savedtofolderwithsource'), caption = self.utility.lang.get('error'), style = wx.OK | wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy() defdestfolder = "" else: try: os.makedirs(defdestfolder) except: dlg = wx.MessageDialog(self, message = self.utility.lang.get('invalidwinname') + '\n'+ \ self.utility.lang.get('savedtofolderwithsource'), caption = self.utility.lang.get('error'), style = wx.OK | wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy() defdestfolder = "" # params['target'] = defdestfolder self.targeted = defdestfolder elif i == 2: dl = wx.DirDialog(self, style = wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON) result = dl.ShowModal() dl.Destroy() if result != wx.ID_OK: return params['target'] = dl.GetPath() self.targeted = dl.GetPath() else: self.targeted = "" return params def getTargeted(self): targeted = self.targeted return targeted################################################################## Class: TorrentMaker## Creates the dialog for making a torrent#################################################################class TorrentMaker(wx.Frame): def __init__(self, parent): self.parent = parent self.utility = self.parent.utility title = self.utility.lang.get('btfilemakertitle') wx.Frame.__init__(self, None, -1, title) if sys.platform == 'win32': self.SetIcon(self.utility.icon) panel = wx.Panel(self, -1) sizer = wx.BoxSizer(wx.VERTICAL) self.notebook = wx.Notebook(panel, -1) self.fileInfoPanel = FileInfoPanel(self.notebook, self) self.notebook.AddPage(self.fileInfoPanel, self.utility.lang.get('fileinfo')) self.trackerInfoPanel = TrackerInfoPanel(self.notebook, self) self.notebook.AddPage(self.trackerInfoPanel, self.utility.lang.get('trackerinfo')) self.miscInfoPanel = MiscInfoPanel(self.notebook, self) self.notebook.AddPage(self.miscInfoPanel, self.utility.lang.get('miscinfo')) sizer.Add(self.notebook, 1, wx.EXPAND|wx.ALL, 5) btnbox = wx.BoxSizer(wx.HORIZONTAL) b3 = wx.Button(panel, -1, self.utility.lang.get('saveasdefaultconfig')) btnbox.Add(b3, 0, wx.EXPAND) b2 = wx.Button(panel, -1, self.utility.lang.get('maketorrent')) btnbox.Add(b2, 0, wx.EXPAND|wx.LEFT|wx.RIGHT, 10) b4 = wx.Button(panel, -1, self.utility.lang.get('close')) btnbox.Add(b4, 0, wx.EXPAND) sizer.Add(btnbox, 0, wx.ALIGN_CENTER|wx.ALL, 10) wx.EVT_BUTTON(panel, b2.GetId(), self.complete) wx.EVT_BUTTON(panel, b3.GetId(), self.saveConfig) wx.EVT_BUTTON(panel, b4.GetId(), self.closeWin) panel.SetSizerAndFit(sizer) self.Fit() self.Show() def closeWin(self, event = None): self.utility.actions[ACTION_MAKETORRENT].torrentmaker = None savetordeffolder = self.fileInfoPanel.savetordeftext.GetValue() self.utility.makerconfig.Write('savetordeffolder', savetordeffolder) self.utility.makerconfig.Write('announcehistory', self.trackerInfoPanel.announcehistory, "bencode-list") self.Destroy() def saveConfig(self, event = None): self.fileInfoPanel.saveConfig() self.trackerInfoPanel.saveConfig() self.miscInfoPanel.saveConfig() self.utility.makerconfig.Flush() def complete(self, event = None): filename = self.fileInfoPanel.dirCtl.GetValue() if filename == '': dlg = wx.MessageDialog(self, message = self.utility.lang.get('youmustselectfileordir'), caption = self.utility.lang.get('error'), style = wx.OK | wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy() return params = self.fileInfoPanel.getParams() params = union(params, self.trackerInfoPanel.getParams()) params = union(params, self.miscInfoPanel.getParams()) try: CompleteDir(self, filename, params['announce'], params) except: oldstdout = sys.stdout sys.stdout = sys.stderr print_exc() sys.stdout = oldstdout################################################################## Class: CompleteDir## Creating torrents for one or more files#################################################################class CompleteDir: def __init__(self, parent, d, a, params): self.d = d self.a = a self.params = params self.parent = parent self.utility = self.parent.utility self.flag = Event() self.separatetorrents = False # See if we need to get md5sums for each file if 'gethash' in params: self.gethash = params['gethash'] else: self.gethash = None # Can remove it from params before we pass things on if 'gethash' in params: del params['gethash'] if isdir(d): self.choicemade = Event() frame = wx.Frame(None, -1, self.utility.lang.get('btmaketorrenttitle'), size = (1, 1)) self.frame = frame panel = wx.Panel(frame, -1) gridSizer = wx.FlexGridSizer(cols = 1, vgap = 8, hgap = 8) gridSizer.AddGrowableRow(1) gridSizer.Add(wx.StaticText(panel, -1, self.utility.lang.get('dirnotice')), 0, wx.ALIGN_CENTER) gridSizer.Add(wx.StaticText(panel, -1, '')) b = wx.FlexGridSizer(cols = 3, hgap = 10) yesbut = wx.Button(panel, -1, self.utility.lang.get('yes')) def saidyes(e, self = self): self.frame.Destroy() self.separatetorrents = True self.begin() wx.EVT_BUTTON(frame, yesbut.GetId(), saidyes) b.Add(yesbut, 0) nobut = wx.Button(panel, -1, self.utility.lang.get('no')) def saidno(e, self = self): self.frame.Destroy() self.begin() wx.EVT_BUTTON(frame, nobut.GetId(), saidno) b.Add(nobut, 0) cancelbut = wx.Button(panel, -1, self.utility.lang.get('cancel')) def canceled(e, self = self): self.frame.Destroy() wx.EVT_BUTTON(frame, cancelbut.GetId(), canceled) b.Add(cancelbut, 0) gridSizer.Add(b, 0, wx.ALIGN_CENTER) border = wx.BoxSizer(wx.HORIZONTAL) border.Add(gridSizer, 1, wx.EXPAND | wx.ALL, 4) panel.SetSizer(border) panel.SetAutoLayout(True) frame.Show() border.Fit(panel) frame.Fit() else: self.begin() def begin(self): if self.separatetorrents: frame = wx.Frame(None, -1, self.utility.lang.get('btmakedirtitle'), size = wx.Size(550, 250)) else: frame = wx.Frame(None, -1, self.utility.lang.get('btmaketorrenttitle'), size = wx.Size(550, 250)) self.frame = frame panel = wx.Panel(frame, -1) gridSizer = wx.FlexGridSizer(cols = 1, vgap = 15, hgap = 8) if self.separatetorrents: self.currentLabel = wx.StaticText(panel, -1, self.utility.lang.get('checkfilesize')) else: self.currentLabel = wx.StaticText(panel, -1, self.utility.lang.get('building') + self.d + '.torrent') gridSizer.Add(self.currentLabel, 0, wx.EXPAND) self.gauge = wx.Gauge(panel, -1, range = 1000, style = wx.GA_SMOOTH) gridSizer.Add(self.gauge, 0, wx.EXPAND) gridSizer.Add((10, 10), 1, wx.EXPAND) self.button = wx.Button(panel, -1, self.utility.lang.get('cancel')) gridSizer.Add(self.button, 0, wx.ALIGN_CENTER) gridSizer.AddGrowableRow(2) gridSizer.AddGrowableCol(0) g2 = wx.FlexGridSizer(cols = 1, vgap = 15, hgap = 8) g2.Add(gridSizer, 1, wx.EXPAND | wx.ALL, 25) g2.AddGrowableRow(0) g2.AddGrowableCol(0) panel.SetSizer(g2) panel.SetAutoLayout(True) wx.EVT_BUTTON(frame, self.button.GetId(), self.done) wx.EVT_CLOSE(frame, self.done) EVT_INVOKE(frame, self.onInvoke) frame.Show(True) Thread(target = self.complete).start() def complete(self): try: if self.separatetorrents: completedir(self.d, self.a, self.params, self.flag, self.valCallback, self.fileCallback, gethash = self.gethash) else: make_meta_file(self.d, self.a, self.params, self.flag, self.valCallback, progress_percent = 1, gethash = self.gethash) if not self.flag.isSet(): self.currentLabel.SetLabel(self.utility.lang.get('Done')) self.gauge.SetValue(1000) self.button.SetLabel(self.utility.lang.get('close')) self.frame.Refresh() except (OSError, IOError), e: self.currentLabel.SetLabel(self.utility.lang.get('error')) self.button.SetLabel(self.utility.lang.get('close')) dlg = wx.MessageDialog(None, message = self.utility.lang.get('error') + ' - ' + str(e), caption = self.utility.lang.get('error'), style = wx.OK | wx.ICON_ERROR) dlg.ShowModal() dlg.Destroy() if self.parent.fileInfoPanel.startnow.GetValue(): targeted = self.parent.fileInfoPanel.getTargeted() if not targeted: copy2(normpath(self.d) + '.torrent', os.path.join(self.utility.getConfigPath(), "torrent")) else: try: copy2(join(targeted, split(normpath(self.d))[1]) + '.torrent', os.path.join(self.utility.getConfigPath(), "torrent")) except: pass def valCallback(self, amount): self.invokeLater(self.onVal, [amount]) def onVal(self, amount): self.gauge.SetValue(int(amount * 1000)) def fileCallback(self, f): self.invokeLater(self.onFile, [f]) def onFile(self, f): self.currentLabel.SetLabel(self.utility.lang.get('building') + join(self.d, f) + '.torrent') def onInvoke(self, event): if not self.flag.isSet(): event.func(*event.args, **event.kwargs) def invokeLater(self, func, args = None, kwargs = None): if args is None: args = [] if kwargs is None: kwargs = {} if not self.flag.isSet(): wx.PostEvent(self.frame, InvokeEvent(func, args, kwargs)) def done(self, event): self.flag.set() self.frame.Destroy() if self.parent.fileInfoPanel.startnow.GetValue(): self.tmtordest = split(normpath(self.d))[1] + '.torrent' torrentsrc = os.path.join(self.utility.getConfigPath(), "torrent", self.tmtordest) self.utility.queue.addtorrents.AddTorrentFromFile(torrentsrc, dest = self.d)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -