diff options
author | Calum Lind <calumlind+deluge@gmail.com> | 2017-02-24 18:28:33 +0000 |
---|---|---|
committer | Calum Lind <calumlind+deluge@gmail.com> | 2017-03-16 23:20:56 +0000 |
commit | eb38e0ffff42c5862105b9f4138dc615aff57af8 (patch) | |
tree | 110a4de6c6e9a6fd1052ff44b35abaa869cc5bb5 /deluge/core/core.py | |
parent | 321677e05a7d24e3a0c76012b588671a03bdac54 (diff) | |
download | deluge-eb38e0ffff42c5862105b9f4138dc615aff57af8.tar.gz deluge-eb38e0ffff42c5862105b9f4138dc615aff57af8.tar.bz2 deluge-eb38e0ffff42c5862105b9f4138dc615aff57af8.zip |
[Py2to3] Large set of changes for Python 3 compat
- Preparation work for using six or future module for Py2/3 compat. The
code will be written in Python 3 with Python 2 fallbacks.
- Added some Py3 imports with Py2 fallbacks to make it easier to remove
Py2 code in future.
- Replace xrange with range (sort out import as top of files in future).
- Workaround Py2to3 basestring issue with inline if in instances. This means
every usage of basestring is more considered.
- Replace iteritems and itervalues for items and values. There might be a
performance penalty on Py2 so might need to revisit this change.
Diffstat (limited to 'deluge/core/core.py')
-rw-r--r-- | deluge/core/core.py | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/deluge/core/core.py b/deluge/core/core.py index 52cacafdf..5daf1a675 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -25,6 +25,7 @@ import deluge.common import deluge.component as component from deluge import path_chooser_common from deluge._libtorrent import lt +from deluge.common import PY2 from deluge.configmanager import ConfigManager, get_config_dir from deluge.core.alertmanager import AlertManager from deluge.core.authmanager import (AUTH_LEVEL_ADMIN, AUTH_LEVEL_NONE, AUTH_LEVELS_MAPPING, @@ -40,6 +41,12 @@ from deluge.error import AddTorrentError, DelugeError, InvalidPathError, Invalid from deluge.event import NewVersionAvailableEvent, SessionPausedEvent, SessionResumedEvent, TorrentQueueChangedEvent from deluge.httpdownloader import download_file +try: + from urllib.request import urlopen, URLError +except ImportError: + # PY2 fallback + from urllib2 import urlopen, URLError + log = logging.getLogger(__name__) OLD_SESSION_STATUS_KEYS = { @@ -293,7 +300,6 @@ class Core(component.Component): def get_new_release(self): log.debug('get_new_release') - from urllib2 import urlopen, URLError try: self.new_release = urlopen('http://download.deluge-torrent.org/version-1.0').read().strip() except URLError as ex: @@ -596,7 +602,7 @@ class Core(component.Component): status_dict, plugin_keys = args # Ask the plugin manager to fill in the plugin keys if len(plugin_keys) > 0: - for key in status_dict.keys(): + for key in status_dict: status_dict[key].update(self.pluginmanager.get_status(key, plugin_keys)) return status_dict d.addCallback(add_plugin_fields) @@ -635,7 +641,7 @@ class Core(component.Component): def set_config(self, config): """Set the config with values from dictionary""" # Load all the values into the configuration - for key in config.keys(): + for key in config: if self.read_only_config_keys and key in self.read_only_config_keys: continue self.config[key] = config[key] @@ -710,7 +716,7 @@ class Core(component.Component): if 'owner' in options and not self.core.authmanager.has_account(options['owner']): raise DelugeError('Username "%s" is not known.' % options['owner']) - if not isinstance(torrent_ids, (list, tuple)): + if isinstance(torrent_ids, str if not PY2 else basestring): torrent_ids = [torrent_ids] for torrent_id in torrent_ids: |