From 5607bb3d619aa85ac174880ea4acdc5474b0b78c Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Thu, 27 Oct 2016 23:01:56 +0100 Subject: [PATCH] [GTKUI] Move Tab data funcs to new common file --- deluge/ui/gtkui/details_tab.py | 13 +---- deluge/ui/gtkui/piecesbar.py | 4 +- deluge/ui/gtkui/status_tab.py | 59 +------------------- deluge/ui/gtkui/tab_data_funcs.py | 92 +++++++++++++++++++++++++++++++ deluge/ui/gtkui/trackers_tab.py | 18 +----- 5 files changed, 100 insertions(+), 86 deletions(-) create mode 100644 deluge/ui/gtkui/tab_data_funcs.py diff --git a/deluge/ui/gtkui/details_tab.py b/deluge/ui/gtkui/details_tab.py index 798fc6659..10d69d3ff 100644 --- a/deluge/ui/gtkui/details_tab.py +++ b/deluge/ui/gtkui/details_tab.py @@ -12,23 +12,12 @@ from xml.sax.saxutils import escape as xml_escape import deluge.component as component from deluge.common import fdate, fsize, is_url +from deluge.ui.gtkui.tab_data_funcs import fdate_or_dash, fpieces_num_size from deluge.ui.gtkui.torrentdetails import Tab log = logging.getLogger(__name__) -def fpieces_num_size(num_pieces, piece_size): - return "%s (%s)" % (num_pieces, fsize(piece_size, precision=0)) - - -def fdate_or_dash(value): - """Display value as date, eg 05/05/08 or dash""" - if value > 0.0: - return fdate(value) - else: - return "-" - - class DetailsTab(Tab): def __init__(self): Tab.__init__(self) diff --git a/deluge/ui/gtkui/piecesbar.py b/deluge/ui/gtkui/piecesbar.py index a9399d6aa..f4868a1b9 100644 --- a/deluge/ui/gtkui/piecesbar.py +++ b/deluge/ui/gtkui/piecesbar.py @@ -17,8 +17,8 @@ import pango import pangocairo from cairo import FORMAT_ARGB32, Context, ImageSurface -import deluge.ui.gtkui.status_tab from deluge.configmanager import ConfigManager +from deluge.ui.gtkui.tab_data_funcs import fpcnt log = logging.getLogger(__name__) @@ -170,7 +170,7 @@ class PiecesBar(gtk.DrawingArea): if self.__text: text = self.__text else: - text = deluge.ui.gtkui.status_tab.fpcnt(self.__fraction, self.__state, self.__message) + text = fpcnt(self.__fraction, self.__state, self.__message) log.trace("PiecesBar text %r", text) pl.set_text(text) diff --git a/deluge/ui/gtkui/status_tab.py b/deluge/ui/gtkui/status_tab.py index f44b5fc00..c9134d087 100644 --- a/deluge/ui/gtkui/status_tab.py +++ b/deluge/ui/gtkui/status_tab.py @@ -12,69 +12,16 @@ from __future__ import division import logging import deluge.component as component -from deluge.common import fdate, fpeer, fsize, fspeed, ftime +from deluge.common import fpeer from deluge.configmanager import ConfigManager from deluge.ui.gtkui.piecesbar import PiecesBar +from deluge.ui.gtkui.tab_data_funcs import (fdate_or_never, flast_active, fpcnt, fratio, fseed_rank_or_dash, fspeed_max, + ftime_or_dash, ftotal_sized) from deluge.ui.gtkui.torrentdetails import Tab log = logging.getLogger(__name__) -def ftotal_sized(first, second): - return "%s (%s)" % (fsize(first, shortform=True), fsize(second, shortform=True)) - - -def fratio(value): - return ("%.3f" % value).rstrip('0').rstrip('.') if value > 0 else "∞" - - -def fpcnt(value, state, message): - textstr = _(state) - if state not in ("Error", "Seeding") and value < 100: - textstr = ('%s %.2f' % (textstr, value)).rstrip('0').rstrip('.') + '%' - elif state == "Error": - textstr = _("%s: %s") % (textstr, message) - return textstr - - -def fspeed_max(value, max_value=-1): - value = fspeed(value, shortform=True) - return "%s (%s %s)" % (value, max_value, _("K/s")) if max_value > -1 else value - - -def fdate_or_never(value): - """Display value as date, eg 05/05/08 or Never""" - return fdate(value, date_only=True) if value > 0 else _("Never") - - -def ftime_or_dash(value): - """Display value as time, eg 2h 30m or dash""" - return ftime(value) if value > 0 else "-" - - -def fseed_rank_or_dash(seed_rank, seeding_time): - """Display value if seeding otherwise dash""" - - if seeding_time > 0: - if seed_rank >= 1000: - return "%ik" % (seed_rank // 1000) - else: - return str(seed_rank) - else: - return "-" - - -def flast_active(time_since_download, time_since_upload): - """The last time the torrent was active as time e.g. 2h 30m or dash""" - - try: - last_time_since = min((x for x in (time_since_download, time_since_upload) if x != -1)) - except ValueError: - return "-" - else: - return ftime(last_time_since) - - class StatusTab(Tab): def __init__(self): Tab.__init__(self) diff --git a/deluge/ui/gtkui/tab_data_funcs.py b/deluge/ui/gtkui/tab_data_funcs.py new file mode 100644 index 000000000..ef87de4ed --- /dev/null +++ b/deluge/ui/gtkui/tab_data_funcs.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2007, 2008 Andrew Resch +# +# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with +# the additional special exception to link portions of this program with the OpenSSL library. +# See LICENSE for more details. +# + +from deluge.common import fdate, fsize, fspeed, ftime + + +def ftotal_sized(first, second): + return "%s (%s)" % (fsize(first, shortform=True), fsize(second, shortform=True)) + + +def fratio(value): + return ("%.3f" % value).rstrip('0').rstrip('.') if value > 0 else "∞" + + +def fpcnt(value, state, message): + textstr = _(state) + if state not in ("Error", "Seeding") and value < 100: + textstr = ('%s %.2f' % (textstr, value)).rstrip('0').rstrip('.') + '%' + elif state == "Error": + textstr = _("%s: %s") % (textstr, message) + return textstr + + +def fspeed_max(value, max_value=-1): + value = fspeed(value, shortform=True) + return "%s (%s %s)" % (value, max_value, _("K/s")) if max_value > -1 else value + + +def fdate_or_never(value): + """Display value as date, eg 05/05/08 or Never""" + return fdate(value, date_only=True) if value > 0 else _("Never") + + +def fdate_or_dash(value): + """Display value as date, eg 05/05/08 or dash""" + if value > 0.0: + return fdate(value) + else: + return "-" + + +def ftime_or_dash(value): + """Display value as time, eg 2h 30m or dash""" + return ftime(value) if value > 0 else "-" + + +def fseed_rank_or_dash(seed_rank, seeding_time): + """Display value if seeding otherwise dash""" + + if seeding_time > 0: + if seed_rank >= 1000: + return "%ik" % (seed_rank // 1000) + else: + return str(seed_rank) + else: + return "-" + + +def flast_active(time_since_download, time_since_upload): + """The last time the torrent was active as time e.g. 2h 30m or dash""" + + try: + last_time_since = min((x for x in (time_since_download, time_since_upload) if x != -1)) + except ValueError: + return "-" + else: + return ftime(last_time_since) + + +def fpieces_num_size(num_pieces, piece_size): + return "%s (%s)" % (num_pieces, fsize(piece_size, precision=0)) + + +def fcount(value): + return "%s" % len(value) + + +def ftranslate(text): + if text: + text = _(text) + return text + + +def fyes_no(value): + """Return Yes or No to bool value""" + return _("Yes") if value else _("No") diff --git a/deluge/ui/gtkui/trackers_tab.py b/deluge/ui/gtkui/trackers_tab.py index 97875aa80..36b4b9732 100644 --- a/deluge/ui/gtkui/trackers_tab.py +++ b/deluge/ui/gtkui/trackers_tab.py @@ -11,26 +11,12 @@ import logging import deluge.component as component from deluge.common import ftime +from deluge.ui.gtkui.tab_data_funcs import fcount, ftranslate, fyes_no from deluge.ui.gtkui.torrentdetails import Tab log = logging.getLogger(__name__) -def fcount(value): - return "%s" % len(value) - - -def ftranslate(text): - if text: - text = _(text) - return text - - -def str_yes_no(value): - """Return Yes or No to bool value""" - return _("Yes") if value else _("No") - - class TrackersTab(Tab): def __init__(self): Tab.__init__(self) @@ -47,7 +33,7 @@ class TrackersTab(Tab): (builder.get_object("summary_tracker"), None, ("tracker_host",)), (builder.get_object("summary_tracker_status"), ftranslate, ("tracker_status",)), (builder.get_object("summary_tracker_total"), fcount, ("trackers",)), - (builder.get_object("summary_private"), str_yes_no, ("private",)), + (builder.get_object("summary_private"), fyes_no, ("private",)), ] self.status_keys = [status for widget in self.label_widgets for status in widget[2]]