[GTKUI] Refactor adding list of trackers

This commit is contained in:
Calum Lind 2016-11-07 23:31:43 +00:00
parent 267d331fac
commit 6424333c35
3 changed files with 79 additions and 39 deletions

View File

@ -25,6 +25,7 @@ from deluge.ui.client import client
from deluge.ui.common import TorrentInfo from deluge.ui.common import TorrentInfo
from deluge.ui.gtkui.common import listview_replace_treestore, reparent_iter from deluge.ui.gtkui.common import listview_replace_treestore, reparent_iter
from deluge.ui.gtkui.dialogs import ErrorDialog from deluge.ui.gtkui.dialogs import ErrorDialog
from deluge.ui.gtkui.edittrackersdialog import trackers_tiers_from_text
from deluge.ui.gtkui.path_chooser import PathChooser from deluge.ui.gtkui.path_chooser import PathChooser
from deluge.ui.gtkui.torrentview_data_funcs import cell_data_size from deluge.ui.gtkui.torrentview_data_funcs import cell_data_size
@ -665,13 +666,12 @@ class AddTorrentDialog(component.Component):
response = dialog.run() response = dialog.run()
infohash = entry.get_text().strip() infohash = entry.get_text().strip()
if response == gtk.RESPONSE_OK and deluge.common.is_infohash(infohash): if response == gtk.RESPONSE_OK and deluge.common.is_infohash(infohash):
trackers = [] # Create a list of trackers from the textview buffer
b = textview.get_buffer() tview_buf = textview.get_buffer()
lines = b.get_text(b.get_start_iter(), b.get_end_iter()).strip().split('\n') trackers_text = tview_buf.get_text(*tview_buf.get_bounds())
log.debug('lines: %s', lines) log.debug('Create torrent tracker lines: %s', trackers_text)
for l in lines: trackers = list(trackers_tiers_from_text(trackers_text).keys())
if deluge.common.is_url(l):
trackers.append(l)
# Convert the information to a magnet uri, this is just easier to # Convert the information to a magnet uri, this is just easier to
# handle this way. # handle this way.
log.debug('trackers: %s', trackers) log.debug('trackers: %s', trackers)

View File

@ -21,6 +21,7 @@ import deluge.component as component
from deluge.common import get_path_size, is_url, resource_filename from deluge.common import get_path_size, is_url, resource_filename
from deluge.configmanager import ConfigManager from deluge.configmanager import ConfigManager
from deluge.ui.client import client from deluge.ui.client import client
from deluge.ui.gtkui.edittrackersdialog import last_tier_trackers_from_liststore, trackers_tiers_from_text
from deluge.ui.gtkui.torrentview_data_funcs import cell_data_size from deluge.ui.gtkui.torrentview_data_funcs import cell_data_size
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -292,12 +293,13 @@ class CreateTorrentDialog(object):
tracker = trackers[0][0] tracker = trackers[0][0]
# Get a list of webseeds # Get a list of webseeds
textview_buf = self.builder.get_object('textview_webseeds').get_buffer()
lines = textview_buf.get_text(*textview_buf.get_bounds()).strip().split('\n')
webseeds = [] webseeds = []
b = self.builder.get_object('textview_webseeds').get_buffer() for line in lines:
lines = b.get_text(b.get_start_iter(), b.get_end_iter()).strip().split('\n') line = line.replace('\\', '/') # Fix any mistyped urls.
for l in lines: if is_url(line):
if is_url(l): webseeds.append(line)
webseeds.append(l)
# Get the piece length in bytes # Get the piece length in bytes
combo = self.builder.get_object('combo_piece_size') combo = self.builder.get_object('combo_piece_size')
piece_length = self.parse_piece_size_text(combo.get_model()[combo.get_active()][0]) piece_length = self.parse_piece_size_text(combo.get_model()[combo.get_active()][0])
@ -426,24 +428,16 @@ class CreateTorrentDialog(object):
if response == gtk.RESPONSE_OK: if response == gtk.RESPONSE_OK:
# Create a list of trackers from the textview buffer # Create a list of trackers from the textview buffer
trackers = [] textview_buf = textview.get_buffer()
b = textview.get_buffer() trackers_text = textview_buf.get_text(*textview_buf.get_bounds())
lines = b.get_text(b.get_start_iter(), b.get_end_iter()).strip().split('\n') log.debug('Create torrent tracker lines: %s', trackers_text)
self.config['createtorrent.trackers'] = lines self.config['createtorrent.trackers'] = trackers_text.split('/n')
log.debug('lines: %s', lines)
for l in lines:
l = l.replace('\\', '/') # Fix mistyped urls.
if is_url(l):
trackers.append(l)
# We are going to add these trackers to the highest tier + 1 # Append trackers liststore with unique trackers and tiers starting from last tier number.
tier = 0 last_tier, orig_trackers = last_tier_trackers_from_liststore(self.trackers_liststore)
for row in self.trackers_liststore: for tracker, tier in trackers_tiers_from_text(trackers_text).items():
if row[0] > tier: if tracker not in orig_trackers:
tier = row[0] self.trackers_liststore.append([tier + last_tier, tracker])
for tracker in trackers:
self.trackers_liststore.append([tier, tracker])
dialog.destroy() dialog.destroy()

View File

@ -24,6 +24,58 @@ from deluge.ui.gtkui.common import get_deluge_icon
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def last_tier_trackers_from_liststore(trackers_liststore):
"""Create a list of tracker from existing liststore and find last tier number.
Args:
tracker_liststore (gtk.ListStore): A gtk.ListStore with [tier (int), tracker (str)] rows.
Returns:
tuple(int, list): A tuple containing last tier number and list of trackers.
"""
last_tier = 0
trackers_from_liststore = []
for tier, tracker in trackers_liststore:
trackers_from_liststore.append(tracker)
if tier >= last_tier:
last_tier = tier + 1
return last_tier, trackers_from_liststore
def trackers_tiers_from_text(text_str=''):
"""Create a list of trackers from text.
Any duplicate trackers are removed.
Args:
text_input (str): A block of text with tracker separated by newlines.
Returns:
list: The list of trackers.
Notes:
Trackers should be separated by newlines and empty line denotes start of new tier.
"""
trackers = {}
tier = 0
lines = text_str.strip().split('\n')
for line in lines:
if not line:
tier += 1
continue
line = line.replace('\\', '/') # Fix any mistyped urls.
if is_url(line) and line not in trackers:
trackers[line] = tier
return trackers
class EditTrackersDialog(object): class EditTrackersDialog(object):
def __init__(self, torrent_id, parent=None): def __init__(self, torrent_id, parent=None):
self.torrent_id = torrent_id self.torrent_id = torrent_id
@ -215,16 +267,10 @@ class EditTrackersDialog(object):
log.debug('on_button_add_ok_clicked') log.debug('on_button_add_ok_clicked')
# Create a list of trackers from the textview widget # Create a list of trackers from the textview widget
textview = self.builder.get_object('textview_trackers') textview_buf = self.builder.get_object('textview_trackers').get_buffer()
trackers = [] trackers_text = textview_buf.get_text(*textview_buf.get_bounds())
b = textview.get_buffer()
lines = b.get_text(b.get_start_iter(), b.get_end_iter()).strip().split('\n')
for l in lines:
l = l.replace('\\', '/') # Fix mistyped urls.
if is_url(l):
trackers.append(l)
for tracker in trackers: for tracker in trackers_tiers_from_text(trackers_text):
# Figure out what tier number to use.. it's going to be the highest+1 # Figure out what tier number to use.. it's going to be the highest+1
# Also check for duplicates # Also check for duplicates
# Check if there are any entries # Check if there are any entries
@ -244,7 +290,7 @@ class EditTrackersDialog(object):
self.add_tracker(highest_tier + 1, tracker) self.add_tracker(highest_tier + 1, tracker)
# Clear the entry widget and hide the dialog # Clear the entry widget and hide the dialog
textview.get_buffer().set_text('') textview_buf.set_text('')
self.add_tracker_dialog.hide() self.add_tracker_dialog.hide()
def on_button_add_cancel_clicked(self, widget): def on_button_add_cancel_clicked(self, widget):