raster pushed a commit to branch master. http://git.enlightenment.org/apps/epour.git/commit/?id=c7264e9ed5eb1725d926cbc76324bfbb9bfc6da9
commit c7264e9ed5eb1725d926cbc76324bfbb9bfc6da9 Author: rafspiny <rafsp...@gmail.com> Date: Sun May 23 19:40:45 2021 +0100 Boost compatibility with recent libtorrent versions. Summary: Since libtorrent 1.2.0.0, several things have changed: * Session are handled differently. Must use get_settings() instead of calling settings() * Also set_settings() has been removed. Now apply_settings() must be used. * The way proxies are stored has changed too. Everything has been made backwords compatible. Plus, I took the liberty of getting rid of the cgi module. cgi.escape() as been deprecated since Python 3.2 So I replaced it with the html module, as recommended from the Python documentation: - https://docs.python.org/3.7/library/cgi.html Fixes #T8885 Reviewers: DaveMDS, kuuko, ProhtMeyhet Subscribers: ali.alzyod Differential Revision: https://phab.enlightenment.org/D12276 --- epour/gui/Preferences.py | 84 +++++++++++++++++++++++------------------------ epour/gui/TorrentProps.py | 6 ++-- epour/gui/__init__.py | 4 +-- epour/session.py | 35 ++++++++++++++++---- 4 files changed, 75 insertions(+), 54 deletions(-) diff --git a/epour/gui/Preferences.py b/epour/gui/Preferences.py index 801837d..dff66fc 100644 --- a/epour/gui/Preferences.py +++ b/epour/gui/Preferences.py @@ -20,7 +20,7 @@ # import os -import cgi +import html import logging import libtorrent as lt @@ -45,6 +45,7 @@ from efl.evas import Rectangle, EVAS_HINT_EXPAND, EVAS_HINT_FILL from .Widgets import UnitSpinner, Error, Information, ActSWithLabel, FsButton, \ RangeSpinners +from ..session import lt_version_post_breaking_change, get_session_settings, save_settings log = logging.getLogger("epour.preferences") @@ -278,34 +279,24 @@ class PreferencesProxy(PreferencesDialog): proxies = [ [_("Proxy for torrent peer connections"), - session.peer_proxy, session.set_peer_proxy], + session.peer_proxy, session.set_peer_proxy, session], [_("Proxy for torrent web seed connections"), - session.web_seed_proxy, session.set_web_seed_proxy], + session.web_seed_proxy, session.set_web_seed_proxy, session], [_("Proxy for tracker connections"), - session.tracker_proxy, session.set_tracker_proxy], + session.tracker_proxy, session.set_tracker_proxy, session], [_("Proxy for DHT connections"), - session.dht_proxy, session.set_dht_proxy], + session.dht_proxy, session.set_dht_proxy, session], ] - for title, rfunc, wfunc in proxies: - pg = ProxyGroup(self, title, rfunc, wfunc) + for title, rfunc, wfunc, session in proxies: + pg = ProxyGroup(self, title, rfunc, wfunc, session) pg.show() self.box.pack_end(pg) class ProxyGroup(Frame): - proxy_types = { - lt.proxy_type.none.name: lt.proxy_type.none, - lt.proxy_type.socks4.name: lt.proxy_type.socks4, - lt.proxy_type.socks5.name: lt.proxy_type.socks5, - lt.proxy_type.socks5_pw.name: lt.proxy_type.socks5_pw, - lt.proxy_type.http.name: lt.proxy_type.http, - lt.proxy_type.http_pw.name: lt.proxy_type.http_pw, - lt.proxy_type.i2p_proxy.name: lt.proxy_type.i2p_proxy, - } - - def __init__(self, parent, title, rfunc, wfunc): + def __init__(self, parent, title, rfunc, wfunc, session): Frame.__init__( self, parent, size_hint_weight=EXPAND_HORIZ, size_hint_align=FILL_HORIZ, text=title @@ -322,9 +313,16 @@ class ProxyGroup(Frame): l = Label(self, text=_("Proxy type"), size_hint_align=ALIGN_LEFT) l.show() ptype = Hoversel(parent, size_hint_align=FILL_HORIZ) - ptype.text = str(lt.proxy_type.values[ps.type]) - for n in lt.proxy_type.names.keys(): - ptype.item_add(n, callback=lambda x, y, z=n: ptype.text_set(z)) + + if lt_version_post_breaking_change: + ptype.text = str(ps.type) + for n in lt.proxy_type_t.values: + label = str(lt.proxy_type_t.values[n]) + ptype.item_add(label, callback=lambda x, y, z=label: ptype.text_set(z)) + else: + ptype.text = str(lt.proxy_type.values[ps.type]) + for n in lt.proxy_type.names.keys(): + ptype.item_add(n, callback=lambda x, y, z=n: ptype.text_set(z)) ptype.show() t.pack(l, 0, 0, 1, 1) t.pack(ptype, 1, 0, 1, 1) @@ -399,17 +397,25 @@ class ProxyGroup(Frame): def save_conf(self, btn, wfunc, entries): ptype, phost, pport, puser, ppass, phostlu, ppeer = entries - p = lt.proxy_settings() + if lt_version_post_breaking_change: + if ptype.text not in lt.proxy_type_t.proxy_type.names: + raise ValueError("Value %s is not valid.", ptype.text) + p = lt.proxy_type_t.proxy_settings() + p.type = lt.proxy_type_t.proxy_type.names[ptype.text] + ptype_name = ptype.text + else: + p = lt.proxy_settings() + p.type = lt.proxy_type.names[ptype.text] + ptype_name = ptype p.hostname = phost.entry.encode("utf-8") p.username = puser.entry.encode("utf-8") p.password = ppass.entry.encode("utf-8") - p.type = lt.proxy_type.names[ptype.text] p.port = int(pport.value) p.proxy_hostnames = phostlu.state p.proxy_peer_connections = ppeer.state - Information(self.top_widget, _("%s settings saved") % (ptype)) + Information(self.top_widget, _("%s settings saved") % (ptype_name)) wfunc(p) @@ -491,7 +497,7 @@ class PreferencesSession(PreferencesDialog): #elm_conf = Configuration() #scale = elm_conf.scale - s = session.settings() + s = get_session_settings(session) t = Table( self, padding=(5, 5), homogeneous=True, size_hint_align=FILL_BOTH @@ -507,22 +513,15 @@ class PreferencesSession(PreferencesDialog): import time t1 = time.time() - for k in dir(s): + for k in s.keys(): if k.startswith("__"): continue try: if k == "peer_tos" or k == "outgoing_ports": # XXX: these don't have a C++ -> Python equivalent. continue - a = getattr(s, k) - if isinstance(a, lt.disk_cache_algo_t): - w = Spinner(t) - w.size_hint_align = FILL_HORIZ - w.min_max = 0, len(lt.disk_cache_algo_t.values) - 1 - for name, val in lt.disk_cache_algo_t.names.items(): - w.special_value_add(val, name) - w.value = a - elif k == "suggest_mode": + a = s.get(k) + if k == "suggest_mode": w = Spinner(t) w.size_hint_align = FILL_HORIZ w.min_max = 0, len(lt.suggest_mode_t.values) - 1 @@ -583,14 +582,14 @@ class PreferencesSession(PreferencesDialog): w.size_hint_weight = EXPAND_HORIZ w.single_line = True w.editable = False - w.entry = cgi.escape(a) + w.entry = html.escape(a) else: log.debug("Using string for %s type %s", k, type(a)) w = Entry(t) w.size_hint_align = FILL_HORIZ w.size_hint_weight = EXPAND_HORIZ w.single_line = True - w.entry = cgi.escape(a) + w.entry = html.escape(a) w.part_text_set("guide", "Enter here") l = Label(t) l.text = k.replace("_", " ").capitalize() @@ -616,13 +615,11 @@ class PreferencesSession(PreferencesDialog): self.box.pack_end(save_btn) def apply_settings(self, btn, widgets, session): - s = lt.session_settings() + s = get_session_settings(session) for k, w in widgets.items(): - if k == "disk_cache_algorithm": - v = lt.disk_cache_algo_t(w.value) - elif isinstance(w, Spinner): + if isinstance(w, Spinner): v = int(w.value) elif isinstance(w, Slider): v = w.value @@ -633,9 +630,10 @@ class PreferencesSession(PreferencesDialog): else: v = None - setattr(s, k, v) + s[k] = v + + save_settings(session, s) - session.set_settings(s) Information(self, "Session settings saved.") diff --git a/epour/gui/TorrentProps.py b/epour/gui/TorrentProps.py index ac19cd6..17866c7 100644 --- a/epour/gui/TorrentProps.py +++ b/epour/gui/TorrentProps.py @@ -22,7 +22,7 @@ if "long" not in dir(__builtins__): long = int -import cgi +import html import sys import os import pipes @@ -141,7 +141,7 @@ class TorrentProps(DialogWindow): self, size_hint_align=FILL_HORIZ, line_wrap=ELM_WRAP_CHAR, ellipsis=True, scale=2.0 ) - tname.text = "{}".format(cgi.escape(h.name())) + tname.text = "{}".format(html.escape(h.name())) tname.show() box.pack_end(tname) @@ -385,7 +385,7 @@ class TorrentInfo(Box): table.pack(l, 0, i, 1, 1) l.show() v = Label(table, ellipsis=True, size_hint_align=ALIGN_LEFT) - v.text = cgi.escape(str(w)) + v.text = html.escape(str(w)) table.pack(v, 1, i, 1, 1) v.show() i += 1 diff --git a/epour/gui/__init__.py b/epour/gui/__init__.py index 0eb3aec..548e7ce 100644 --- a/epour/gui/__init__.py +++ b/epour/gui/__init__.py @@ -21,7 +21,7 @@ import os import sys -import cgi +import html import logging from datetime import timedelta, datetime import gettext @@ -285,7 +285,7 @@ class MainInterface(object): def torrent_finished_cb(a): msg = _("Torrent {} has finished downloading.").format( - cgi.escape(a.handle.name()) + html.escape(a.handle.name()) ) log.info(msg) diff --git a/epour/session.py b/epour/session.py index fb82d74..0bca051 100644 --- a/epour/session.py +++ b/epour/session.py @@ -38,6 +38,7 @@ from efl.ecore import Timer from xdg.BaseDirectory import save_data_path, load_data_paths lt_version = LooseVersion(lt.version) +lt_version_post_breaking_change = lt_version >= LooseVersion("1.2.0.0") log = logging.getLogger("epour.session") @@ -92,6 +93,28 @@ def lt_compatibility_convert(params): params[k] = v.to_bytes() +def get_session_settings(session: lt.session) -> dict: + """ + Returns a dictionary containing all the settings pairs (key, value), regardless of the version of libtorrent. + """ + if lt_version_post_breaking_change: + settings = session.get_settings() + else: + settings = session.settings().__dict__ + + return settings + + +def save_settings(session: lt.session, settings: dict) -> None: + """ + Save the settings on a session object. It uses the right API according to the libtorrent version. + """ + if lt_version_post_breaking_change: + session.apply_settings(settings) + else: + session.set_settings(settings) + + class Session(lt.session): def __init__(self, conf, shutdown_cb): @@ -239,15 +262,15 @@ class Session(lt.session): log.info("Session restored from disk.") break - settings = self.settings() - from epour import __version__ as version version += ".0" ver_s = "Epour/{} libtorrent/{}".format(version, lt.version) - settings.user_agent = ver_s + + settings = get_session_settings(self) + settings["user_agent"] = ver_s log.debug("User agent: %s", ver_s) - self.set_settings(settings) + save_settings(self, settings) def save_state(self): state = lt.session.save_state(self) @@ -300,7 +323,7 @@ class Session(lt.session): params_dict["ti"] = ti if ti: - if lt_version < LooseVersion("1.2.0.0"): + if not lt_version_post_breaking_change: try: data = read_resume_data(info_hash) except Exception: @@ -316,7 +339,7 @@ class Session(lt.session): else: params.trackers = list(set(params.trackers)) - if lt_version < LooseVersion("1.2.0.0"): + if not lt_version_post_breaking_change: if params is None: log.warn("Falling back to < lt 1.2 compatibility handling.") lt_compatibility_convert(params_dict) --