From 8b50f3cdbdec4f2fa622aca9d0e4b45e5e4bf3aa Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Sun, 16 Oct 2016 18:18:47 -0700 Subject: [PATCH] [Py2to3] Clean-up the use of keys() on dictionary objects. To make the code more Python 3 compatible, I've made a few changes to how we handle keys() or iterkeys() calls on dictionaries. All functionality should remain the same. * Remove the use of .keys() or .iterkeys() when iterating through a dictionary. * Remove the use of .keys() when checking if key exists in dictionary. * Replace dict.keys() with list(dict) to obtain a list of dictionary keys. In Python 3 dict.keys() returns a dict_keys object, not a list. --- deluge/component.py | 10 +++++----- deluge/configmanager.py | 2 +- deluge/core/core.py | 4 ++-- deluge/core/filtermanager.py | 4 ++-- deluge/core/pluginmanager.py | 4 ++-- deluge/core/rpcserver.py | 2 +- deluge/core/torrent.py | 4 ++-- deluge/core/torrentmanager.py | 12 ++++++------ deluge/pluginmanagerbase.py | 8 ++++---- .../plugins/AutoAdd/deluge/plugins/autoadd/core.py | 10 +++++----- .../Blocklist/deluge/plugins/blocklist/core.py | 2 +- .../Extractor/deluge/plugins/extractor/core.py | 2 +- deluge/plugins/Label/deluge/plugins/label/core.py | 12 ++++++------ .../Label/deluge/plugins/label/gtkui/sidebar_menu.py | 2 +- .../deluge/plugins/notifications/common.py | 4 ++-- .../deluge/plugins/notifications/core.py | 4 ++-- .../Scheduler/deluge/plugins/scheduler/core.py | 2 +- deluge/plugins/Stats/deluge/plugins/stats/core.py | 2 +- deluge/plugins/Stats/deluge/plugins/stats/gtkui.py | 2 +- deluge/plugins/WebUi/deluge/plugins/webui/core.py | 2 +- deluge/scripts/create_plugin.py | 2 +- deluge/tests/test_json_api.py | 2 +- deluge/tests/test_sessionproxy.py | 6 +++--- deluge/ui/Win32IconImagePlugin.py | 2 +- deluge/ui/baseargparser.py | 2 +- deluge/ui/client.py | 2 +- deluge/ui/common.py | 6 +++--- deluge/ui/console/commands/config.py | 7 +++---- deluge/ui/console/commands/manage.py | 2 +- deluge/ui/console/modes/alltorrents.py | 2 +- deluge/ui/console/modes/preferences.py | 4 ++-- deluge/ui/gtkui/dialogs.py | 2 +- deluge/ui/gtkui/listview.py | 2 +- deluge/ui/gtkui/menubar.py | 2 +- deluge/ui/gtkui/options_tab.py | 2 +- deluge/ui/gtkui/path_chooser.py | 4 ++-- deluge/ui/gtkui/peers_tab.py | 2 +- deluge/ui/gtkui/preferences.py | 12 ++++++------ deluge/ui/gtkui/statusbar.py | 2 +- deluge/ui/gtkui/systemtray.py | 2 +- deluge/ui/gtkui/torrentview.py | 6 +++--- deluge/ui/sessionproxy.py | 12 ++++++------ deluge/ui/ui_entry.py | 2 +- deluge/ui/web/auth.py | 2 +- deluge/ui/web/json_api.py | 4 ++-- gen_web_gettext.py | 2 +- msgfmt.py | 3 +-- 47 files changed, 95 insertions(+), 97 deletions(-) diff --git a/deluge/component.py b/deluge/component.py index ea9cb9676..04b2f2434 100644 --- a/deluge/component.py +++ b/deluge/component.py @@ -298,7 +298,7 @@ class ComponentRegistry(object): """ # Start all the components if names is empty if not names: - names = self.components.keys() + names = list(self.components) elif isinstance(names, str): names = [names] @@ -332,7 +332,7 @@ class ComponentRegistry(object): """ if not names: - names = self.components.keys() + names = list(self.components) elif isinstance(names, str): names = [names] @@ -370,7 +370,7 @@ class ComponentRegistry(object): """ if not names: - names = self.components.keys() + names = list(self.components) elif isinstance(names, str): names = [names] @@ -396,7 +396,7 @@ class ComponentRegistry(object): """ if not names: - names = self.components.keys() + names = list(self.components) elif isinstance(names, str): names = [names] @@ -421,7 +421,7 @@ class ComponentRegistry(object): def on_stopped(result): return DeferredList([comp._component_shutdown() for comp in self.components.values()]) - return self.stop(self.components.keys()).addCallback(on_stopped) + return self.stop(list(self.components)).addCallback(on_stopped) def update(self): """Update all Components that are in a Started state.""" diff --git a/deluge/configmanager.py b/deluge/configmanager.py index 173ee3e93..28e585dd3 100644 --- a/deluge/configmanager.py +++ b/deluge/configmanager.py @@ -88,7 +88,7 @@ class _ConfigManager(object): """Get a reference to the Config object for this filename""" log.debug("Getting config '%s'", config_file) # Create the config object if not already created - if config_file not in self.config_files.keys(): + if config_file not in self.config_files: self.config_files[config_file] = Config(config_file, defaults, self.config_directory) return self.config_files[config_file] diff --git a/deluge/core/core.py b/deluge/core/core.py index 7499698b0..9a4bc287b 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -505,7 +505,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) @@ -544,7 +544,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 if isinstance(config[key], basestring): diff --git a/deluge/core/filtermanager.py b/deluge/core/filtermanager.py index e58c1b53f..cce367826 100644 --- a/deluge/core/filtermanager.py +++ b/deluge/core/filtermanager.py @@ -169,7 +169,7 @@ class FilterManager(component.Component): if not filter_dict: return torrent_ids - torrent_keys, plugin_keys = self.torrents.separate_keys(filter_dict.keys(), torrent_ids) + torrent_keys, plugin_keys = self.torrents.separate_keys(list(filter_dict), torrent_ids) # Leftover filter arguments, default filter on status fields. for torrent_id in list(torrent_ids): status = self.core.create_torrent_status(torrent_id, torrent_keys, plugin_keys) @@ -186,7 +186,7 @@ class FilterManager(component.Component): for use in sidebar. """ torrent_ids = self.torrents.get_torrent_list() - tree_keys = list(self.tree_fields.keys()) + tree_keys = list(self.tree_fields) if hide_cat: for cat in hide_cat: tree_keys.remove(cat) diff --git a/deluge/core/pluginmanager.py b/deluge/core/pluginmanager.py index 8f82d362f..b7893e4a4 100644 --- a/deluge/core/pluginmanager.py +++ b/deluge/core/pluginmanager.py @@ -46,7 +46,7 @@ class PluginManager(deluge.pluginmanagerbase.PluginManagerBase, component.Compon self.stop() def update_plugins(self): - for plugin in self.plugins.keys(): + for plugin in self.plugins: if hasattr(self.plugins[plugin], "update"): try: self.plugins[plugin].update() @@ -82,7 +82,7 @@ class PluginManager(deluge.pluginmanagerbase.PluginManagerBase, component.Compon """Return the value of status fields for the selected torrent_id.""" status = {} if len(fields) == 0: - fields = self.status_fields.keys() + fields = list(self.status_fields) for field in fields: try: status[field] = self.status_fields[field](torrent_id) diff --git a/deluge/core/rpcserver.py b/deluge/core/rpcserver.py index 8fd6df54c..9f647a9f3 100644 --- a/deluge/core/rpcserver.py +++ b/deluge/core/rpcserver.py @@ -429,7 +429,7 @@ class RPCServer(component.Component): :returns: the exported methods :rtype: list """ - return self.factory.methods.keys() + return list(self.factory.methods) def get_session_id(self): """ diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py index 93e67c97b..277349337 100644 --- a/deluge/core/torrent.py +++ b/deluge/core/torrent.py @@ -940,7 +940,7 @@ class Torrent(object): self.update_status(self.handle.status()) if all_keys: - keys = self.status_funcs.keys() + keys = list(self.status_funcs) status_dict = {} @@ -1360,7 +1360,7 @@ class Torrent(object): If the key is no longer valid, the dict will be deleted. """ - for key in self.prev_status.keys(): + for key in self.prev_status: if not self.rpcserver.is_session_valid(key): del self.prev_status[key] diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 448886d4c..ffe388aa5 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -266,7 +266,7 @@ class TorrentManager(component.Component): list: A list of torrent_ids. """ - torrent_ids = self.torrents.keys() + torrent_ids = list(self.torrents) if component.get("RPCServer").get_session_auth_level() == AUTH_LEVEL_ADMIN: return torrent_ids @@ -924,25 +924,25 @@ class TorrentManager(component.Component): def on_set_max_connections_per_torrent(self, key, value): """Sets the per-torrent connection limit""" log.debug("max_connections_per_torrent set to %s...", value) - for key in self.torrents.keys(): + for key in self.torrents: self.torrents[key].set_max_connections(value) def on_set_max_upload_slots_per_torrent(self, key, value): """Sets the per-torrent upload slot limit""" log.debug("max_upload_slots_per_torrent set to %s...", value) - for key in self.torrents.keys(): + for key in self.torrents: self.torrents[key].set_max_upload_slots(value) def on_set_max_upload_speed_per_torrent(self, key, value): """Sets the per-torrent upload speed limit""" log.debug("max_upload_speed_per_torrent set to %s...", value) - for key in self.torrents.keys(): + for key in self.torrents: self.torrents[key].set_max_upload_speed(value) def on_set_max_download_speed_per_torrent(self, key, value): """Sets the per-torrent download speed limit""" log.debug("max_download_speed_per_torrent set to %s...", value) - for key in self.torrents.keys(): + for key in self.torrents: self.torrents[key].set_max_download_speed(value) # --- Alert handlers --- @@ -1301,7 +1301,7 @@ class TorrentManager(component.Component): if self.torrents: for torrent_id in torrent_ids: if torrent_id in self.torrents: - status_keys = self.torrents[torrent_id].status_funcs.keys() + status_keys = list(self.torrents[torrent_id].status_funcs) leftover_keys = list(set(keys) - set(status_keys)) torrent_keys = list(set(keys) - set(leftover_keys)) return torrent_keys, leftover_keys diff --git a/deluge/pluginmanagerbase.py b/deluge/pluginmanagerbase.py index d600e122f..54d41c3c3 100644 --- a/deluge/pluginmanagerbase.py +++ b/deluge/pluginmanagerbase.py @@ -74,7 +74,7 @@ class PluginManagerBase(object): def disable_plugins(self): # Disable all plugins that are enabled - for key in self.plugins.keys(): + for key in self.plugins: self.disable_plugin(key) def __getitem__(self, key): @@ -86,7 +86,7 @@ class PluginManagerBase(object): def get_enabled_plugins(self): """Returns a list of enabled plugins""" - return self.plugins.keys() + return list(self.plugins) def scan_for_plugins(self): """Scans for available plugins""" @@ -243,14 +243,14 @@ class PluginManagerBase(object): for line in self.pkg_env[name][0].get_metadata("PKG-INFO").splitlines(): if not line: continue - if line[0] in ' \t' and (len(line.split(":", 1)) == 1 or line.split(":", 1)[0] not in info.keys()): + if line[0] in ' \t' and (len(line.split(":", 1)) == 1 or line.split(":", 1)[0] not in info): # This is a continuation cont_lines.append(line.strip()) else: if cont_lines: info[last_header] = "\n".join(cont_lines).strip() cont_lines = [] - if line.split(":", 1)[0] in info.keys(): + if line.split(":", 1)[0] in info: last_header = line.split(":", 1)[0] info[last_header] = line.split(":", 1)[1].strip() return info diff --git a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py index f94588b7c..9487024f9 100644 --- a/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py +++ b/deluge/plugins/AutoAdd/deluge/plugins/autoadd/core.py @@ -131,9 +131,9 @@ class Core(CorePluginBase): for w_id, w in self.watchdirs.iteritems(): if options["abspath"] == w["abspath"] and watchdir_id != w_id: raise Exception("Path is already being watched.") - for key in options.keys(): + for key in options: if key not in OPTIONS_AVAILABLE: - if key not in [key2 + "_toggle" for key2 in OPTIONS_AVAILABLE.iterkeys()]: + if key not in [key2 + "_toggle" for key2 in OPTIONS_AVAILABLE]: raise Exception("autoadd: Invalid options key:%s" % key) # disable the watch loop if it was active if watchdir_id in self.update_timers: @@ -360,7 +360,7 @@ class Core(CorePluginBase): def set_config(self, config): """Sets the config dictionary.""" config = self._make_unicode(config) - for key in config.keys(): + for key in config: self.config[key] = config[key] self.config.save() component.get("EventManager").emit(AutoaddOptionsChangedEvent()) @@ -386,7 +386,7 @@ class Core(CorePluginBase): watchdirs[watchdir_id] = watchdir log.debug("Current logged in user %s is not an ADMIN, send only " - "his watchdirs: %s", session_user, watchdirs.keys()) + "his watchdirs: %s", session_user, list(watchdirs)) return watchdirs def _make_unicode(self, options): @@ -434,7 +434,7 @@ class Core(CorePluginBase): component.get("EventManager").emit(AutoaddOptionsChangedEvent()) def __migrate_config_1_to_2(self, config): - for watchdir_id in config["watchdirs"].iterkeys(): + for watchdir_id in config["watchdirs"]: config["watchdirs"][watchdir_id]["owner"] = "localclient" return config diff --git a/deluge/plugins/Blocklist/deluge/plugins/blocklist/core.py b/deluge/plugins/Blocklist/deluge/plugins/blocklist/core.py index 39829e7ec..5f7cc20c5 100644 --- a/deluge/plugins/Blocklist/deluge/plugins/blocklist/core.py +++ b/deluge/plugins/Blocklist/deluge/plugins/blocklist/core.py @@ -168,7 +168,7 @@ class Core(CorePluginBase): """ needs_blocklist_import = False - for key in config.keys(): + for key in config: if key == 'whitelisted': saved = set(self.config[key]) update = set(config[key]) diff --git a/deluge/plugins/Extractor/deluge/plugins/extractor/core.py b/deluge/plugins/Extractor/deluge/plugins/extractor/core.py index 4ddba3108..2be6f933c 100644 --- a/deluge/plugins/Extractor/deluge/plugins/extractor/core.py +++ b/deluge/plugins/Extractor/deluge/plugins/extractor/core.py @@ -157,7 +157,7 @@ class Core(CorePluginBase): @export def set_config(self, config): "sets the config dictionary" - for key in config.keys(): + for key in config: self.config[key] = config[key] self.config.save() diff --git a/deluge/plugins/Label/deluge/plugins/label/core.py b/deluge/plugins/Label/deluge/plugins/label/core.py index 3b8a36a36..5ca933375 100644 --- a/deluge/plugins/Label/deluge/plugins/label/core.py +++ b/deluge/plugins/Label/deluge/plugins/label/core.py @@ -107,8 +107,8 @@ class Core(CorePluginBase): pass def init_filter_dict(self): - filter_dict = dict([(label, 0) for label in self.labels.keys()]) - filter_dict['All'] = len(self.torrents.keys()) + filter_dict = dict([(label, 0) for label in self.labels]) + filter_dict['All'] = len(self.torrents) return filter_dict # Plugin hooks # @@ -142,8 +142,8 @@ class Core(CorePluginBase): *add any new keys in OPTIONS_DEFAULTS *set all None values to default <-fix development config """ - log.debug(self.labels.keys()) - for key in self.labels.keys(): + log.debug(list(self.labels)) + for key in self.labels: options = dict(OPTIONS_DEFAULTS) options.update(self.labels[key]) self.labels[key] = options @@ -159,7 +159,7 @@ class Core(CorePluginBase): @export def get_labels(self): - return sorted(self.labels.keys()) + return sorted(self.labels) # Labels: @export @@ -259,7 +259,7 @@ class Core(CorePluginBase): } """ check_input(label_id in self.labels, _("Unknown Label")) - for key in options_dict.keys(): + for key in options_dict: if key not in OPTIONS_DEFAULTS: raise Exception("label: Invalid options_dict key:%s" % key) diff --git a/deluge/plugins/Label/deluge/plugins/label/gtkui/sidebar_menu.py b/deluge/plugins/Label/deluge/plugins/label/gtkui/sidebar_menu.py index 39aeba2c3..81e494f19 100644 --- a/deluge/plugins/Label/deluge/plugins/label/gtkui/sidebar_menu.py +++ b/deluge/plugins/Label/deluge/plugins/label/gtkui/sidebar_menu.py @@ -172,7 +172,7 @@ class OptionsDialog(object): self.dialog.run() def load_options(self, options): - log.debug(options.keys()) + log.debug(list(options)) for spin_id in self.spin_ids + self.spin_int_ids: self.glade.get_widget(spin_id).set_value(options[spin_id]) diff --git a/deluge/plugins/Notifications/deluge/plugins/notifications/common.py b/deluge/plugins/Notifications/deluge/plugins/notifications/common.py index 191cb9e12..fbea48e4c 100644 --- a/deluge/plugins/Notifications/deluge/plugins/notifications/common.py +++ b/deluge/plugins/Notifications/deluge/plugins/notifications/common.py @@ -47,8 +47,8 @@ class CustomNotifications(object): pass def disable(self): - for kind in self.custom_notifications.iterkeys(): - for eventtype in self.custom_notifications[kind].copy().iterkeys(): + for kind in self.custom_notifications: + for eventtype in self.custom_notifications[kind].copy(): wrapper, handler = self.custom_notifications[kind][eventtype] self._deregister_custom_provider(kind, eventtype) diff --git a/deluge/plugins/Notifications/deluge/plugins/notifications/core.py b/deluge/plugins/Notifications/deluge/plugins/notifications/core.py index 45fe7f2e0..56a167c07 100644 --- a/deluge/plugins/Notifications/deluge/plugins/notifications/core.py +++ b/deluge/plugins/Notifications/deluge/plugins/notifications/core.py @@ -80,7 +80,7 @@ class CoreNotifications(CustomNotifications): def get_handled_events(self): handled_events = [] - for evt in sorted(known_events.keys()): + for evt in sorted(known_events): if known_events[evt].__module__.startswith('deluge.event'): if evt not in ('TorrentFinishedEvent',): # Skip all un-handled built-in events @@ -204,7 +204,7 @@ class Core(CorePluginBase, CoreNotifications): @export def set_config(self, config): "sets the config dictionary" - for key in config.keys(): + for key in config: self.config[key] = config[key] self.config.save() diff --git a/deluge/plugins/Scheduler/deluge/plugins/scheduler/core.py b/deluge/plugins/Scheduler/deluge/plugins/scheduler/core.py index ac4e64184..a5fed9ef0 100644 --- a/deluge/plugins/Scheduler/deluge/plugins/scheduler/core.py +++ b/deluge/plugins/Scheduler/deluge/plugins/scheduler/core.py @@ -146,7 +146,7 @@ class Core(CorePluginBase): @export() def set_config(self, config): "sets the config dictionary" - for key in config.keys(): + for key in config: self.config[key] = config[key] self.config.save() self.do_schedule(False) diff --git a/deluge/plugins/Stats/deluge/plugins/stats/core.py b/deluge/plugins/Stats/deluge/plugins/stats/core.py index 57bc00497..00ff0066a 100644 --- a/deluge/plugins/Stats/deluge/plugins/stats/core.py +++ b/deluge/plugins/Stats/deluge/plugins/stats/core.py @@ -213,7 +213,7 @@ class Core(CorePluginBase): @export def set_config(self, config): "sets the config dictionary" - for key in config.keys(): + for key in config: self.config[key] = config[key] self.config.save() diff --git a/deluge/plugins/Stats/deluge/plugins/stats/gtkui.py b/deluge/plugins/Stats/deluge/plugins/stats/gtkui.py index 8c8c91b3f..3e7d58696 100644 --- a/deluge/plugins/Stats/deluge/plugins/stats/gtkui.py +++ b/deluge/plugins/Stats/deluge/plugins/stats/gtkui.py @@ -128,7 +128,7 @@ class GraphsTab(Tab): return False def update(self): - d1 = client.stats.get_stats(self.graph.stat_info.keys(), self.selected_interval) + d1 = client.stats.get_stats(list(self.graph.stat_info), self.selected_interval) d1.addCallback(self.graph.set_stats) def _update_complete(result): diff --git a/deluge/plugins/WebUi/deluge/plugins/webui/core.py b/deluge/plugins/WebUi/deluge/plugins/webui/core.py index 0f13e6504..d617d105e 100644 --- a/deluge/plugins/WebUi/deluge/plugins/webui/core.py +++ b/deluge/plugins/WebUi/deluge/plugins/webui/core.py @@ -98,7 +98,7 @@ class Core(CorePluginBase): if not action: action = 'restart' - for key in config.keys(): + for key in config: self.config[key] = config[key] self.config.save() diff --git a/deluge/scripts/create_plugin.py b/deluge/scripts/create_plugin.py index 718ed59f9..da76c971b 100644 --- a/deluge/scripts/create_plugin.py +++ b/deluge/scripts/create_plugin.py @@ -132,7 +132,7 @@ class Core(CorePluginBase): @export def set_config(self, config): \"\"\"Sets the config dictionary\"\"\" - for key in config.keys(): + for key in config: self.config[key] = config[key] self.config.save() diff --git a/deluge/tests/test_json_api.py b/deluge/tests/test_json_api.py index 87c6f89e7..86b09f067 100644 --- a/deluge/tests/test_json_api.py +++ b/deluge/tests/test_json_api.py @@ -175,7 +175,7 @@ class RPCRaiseDelugeErrorJSONTestCase(JSONBase): self.assertTrue("testclass.test" in methods) request = MagicMock() - request.getCookie = MagicMock(return_value=auth.config["sessions"].keys()[0]) + request.getCookie = MagicMock(return_value=list(auth.config["sessions"])[0]) json_data = {"method": "testclass.test", "id": 0, "params": []} request.json = json_lib.dumps(json_data) request_id, result, error = json._handle_request(request) diff --git a/deluge/tests/test_sessionproxy.py b/deluge/tests/test_sessionproxy.py index 94f6ab786..48d09eb6d 100644 --- a/deluge/tests/test_sessionproxy.py +++ b/deluge/tests/test_sessionproxy.py @@ -33,7 +33,7 @@ class Core(object): def get_torrent_status(self, torrent_id, keys, diff=False): if not keys: - keys = self.torrents[torrent_id].keys() + keys = list(self.torrents[torrent_id]) if not diff: ret = {} @@ -55,9 +55,9 @@ class Core(object): def get_torrents_status(self, filter_dict, keys, diff=False): if not filter_dict: - filter_dict["id"] = self.torrents.keys() + filter_dict["id"] = list(self.torrents) if not keys: - keys = self.torrents["a"].keys() + keys = list(self.torrents["a"]) if not diff: if "id" in filter_dict: torrents = filter_dict["id"] diff --git a/deluge/ui/Win32IconImagePlugin.py b/deluge/ui/Win32IconImagePlugin.py index 7de9528fd..f0c8abe5a 100644 --- a/deluge/ui/Win32IconImagePlugin.py +++ b/deluge/ui/Win32IconImagePlugin.py @@ -139,7 +139,7 @@ class Win32IcoFile(object): # figure out where AND mask image starts mode = a[0] bpp = 8 - for k in PIL.BmpImagePlugin.BIT2MODE.keys(): + for k in PIL.BmpImagePlugin.BIT2MODE: if mode == PIL.BmpImagePlugin.BIT2MODE[k][1]: bpp = k break diff --git a/deluge/ui/baseargparser.py b/deluge/ui/baseargparser.py index 2693d3dd7..a7280f8a0 100644 --- a/deluge/ui/baseargparser.py +++ b/deluge/ui/baseargparser.py @@ -37,7 +37,7 @@ def find_subcommand(self, args=None, sys_argv=True): for x in self._subparsers._actions: if not isinstance(x, argparse._SubParsersAction): continue - for sp_name in x._name_parser_map.keys(): + for sp_name in x._name_parser_map: if sp_name in args: subcommand_found = args.index(sp_name) diff --git a/deluge/ui/client.py b/deluge/ui/client.py index 1bcd92bf2..994795b01 100644 --- a/deluge/ui/client.py +++ b/deluge/ui/client.py @@ -394,7 +394,7 @@ class DaemonSSLProxy(DaemonProxy): # We need to tell the daemon what events we're interested in receiving if self.__factory.event_handlers: self.call("daemon.set_event_interest", - self.__factory.event_handlers.keys()) + list(self.__factory.event_handlers)) self.call("core.get_auth_levels_mappings").addCallback( self.__on_auth_levels_mappings diff --git a/deluge/ui/common.py b/deluge/ui/common.py index 94e531d2d..ddcd7574a 100644 --- a/deluge/ui/common.py +++ b/deluge/ui/common.py @@ -134,7 +134,7 @@ class TorrentInfo(object): item.update(paths[path]) item["download"] = True - file_tree = FileTree2(paths.keys()) + file_tree = FileTree2(list(paths)) file_tree.walk(walk) else: def walk(path, item): @@ -313,7 +313,7 @@ class FileTree2(object): :type callback: function """ def walk(directory, parent_path): - for path in directory["contents"].keys(): + for path in directory["contents"]: full_path = os.path.join(parent_path, path).replace("\\", "/") if directory["contents"][path]["type"] == "dir": directory["contents"][path] = callback( @@ -393,7 +393,7 @@ class FileTree(object): :type callback: function """ def walk(directory, parent_path): - for path in directory.keys(): + for path in directory: full_path = os.path.join(parent_path, path) if isinstance(directory[path], dict): directory[path] = callback(full_path, directory[path]) or directory[path] diff --git a/deluge/ui/console/commands/config.py b/deluge/ui/console/commands/config.py index d62f9d0b3..41a686be3 100644 --- a/deluge/ui/console/commands/config.py +++ b/deluge/ui/console/commands/config.py @@ -83,9 +83,8 @@ class Command(BaseCommand): def _get_config(self, options): def _on_get_config(config): - keys = sorted(config.keys()) s = "" - for key in keys: + for key in sorted(config): if key not in options.values: continue color = "{!white,black,bold!}" @@ -120,7 +119,7 @@ class Command(BaseCommand): self.console.write("{!error!}%s" % ex) return - if key not in config.keys(): + if key not in config: self.console.write("{!error!}The key '%s' is invalid!" % key) return @@ -138,4 +137,4 @@ class Command(BaseCommand): return client.core.set_config({key: val}).addCallback(on_set_config) def complete(self, text): - return [k for k in component.get("CoreConfig").keys() if k.startswith(text)] + return [k for k in component.get("CoreConfig") if k.startswith(text)] diff --git a/deluge/ui/console/commands/manage.py b/deluge/ui/console/commands/manage.py index 59a2edc3f..8f48b372d 100644 --- a/deluge/ui/console/commands/manage.py +++ b/deluge/ui/console/commands/manage.py @@ -75,7 +75,7 @@ class Command(BaseCommand): return request_options.append(opt) if not request_options: - request_options = [opt for opt in torrent_options.keys()] + request_options = list(torrent_options) request_options.append('name') d = client.core.get_torrents_status({"id": torrent_ids}, request_options) diff --git a/deluge/ui/console/modes/alltorrents.py b/deluge/ui/console/modes/alltorrents.py index 14e0aaed6..a902481c8 100644 --- a/deluge/ui/console/modes/alltorrents.py +++ b/deluge/ui/console/modes/alltorrents.py @@ -555,7 +555,7 @@ class AllTorrents(BaseMode, component.Component): # Get first element so we can check if it has given field # and if it's a string - first_element = state[state.keys()[0]] + first_element = state[list(state)[0]] if field in first_element: is_string = isinstance(first_element[field], basestring) diff --git a/deluge/ui/console/modes/preferences.py b/deluge/ui/console/modes/preferences.py index f589916ad..8f1b02db8 100644 --- a/deluge/ui/console/modes/preferences.py +++ b/deluge/ui/console/modes/preferences.py @@ -194,7 +194,7 @@ class Preferences(BaseMode): if client.connected(): # Only do this if we're connected to a daemon config_to_set = {} - for key in new_core_config.keys(): + for key in new_core_config: # The values do not match so this needs to be updated if self.core_config[key] != new_core_config[key]: config_to_set[key] = new_core_config[key] @@ -214,7 +214,7 @@ class Preferences(BaseMode): # are ever reordered, so do it the slightly slower but safer way if isinstance(pane, InterfacePane) or isinstance(pane, ColumnsPane): pane.add_config_values(new_console_config) - for key in new_console_config.keys(): + for key in new_console_config: # The values do not match so this needs to be updated if self.console_config[key] != new_console_config[key]: self.console_config[key] = new_console_config[key] diff --git a/deluge/ui/gtkui/dialogs.py b/deluge/ui/gtkui/dialogs.py index 6a5a616e6..cb9388742 100644 --- a/deluge/ui/gtkui/dialogs.py +++ b/deluge/ui/gtkui/dialogs.py @@ -271,7 +271,7 @@ class AccountDialog(BaseDialog): self.authlevel_combo = gtk.combo_box_new_text() active_idx = None - for idx, level in enumerate(levels_mapping.keys()): + for idx, level in enumerate(levels_mapping): self.authlevel_combo.append_text(level) if authlevel and authlevel == level: active_idx = idx diff --git a/deluge/ui/gtkui/listview.py b/deluge/ui/gtkui/listview.py index 4f5ee726f..004560973 100644 --- a/deluge/ui/gtkui/listview.py +++ b/deluge/ui/gtkui/listview.py @@ -303,7 +303,7 @@ class ListView(object): def get_state_field_column(self, field): """Returns the column number for the state field""" - for column in self.columns.keys(): + for column in self.columns: if self.columns[column].status_field is None: continue diff --git a/deluge/ui/gtkui/menubar.py b/deluge/ui/gtkui/menubar.py index 082d6433e..c48eebc54 100644 --- a/deluge/ui/gtkui/menubar.py +++ b/deluge/ui/gtkui/menubar.py @@ -413,7 +413,7 @@ class MenuBar(component.Component): "menuitem_max_connections": client.core.set_torrent_max_connections, "menuitem_upload_slots": client.core.set_torrent_max_upload_slots } - if widget.name in funcs.keys(): + if widget.name in funcs: for torrent in component.get("TorrentView").get_selected_torrents(): funcs[widget.name](torrent, -1) diff --git a/deluge/ui/gtkui/options_tab.py b/deluge/ui/gtkui/options_tab.py index 39a4228ae..95c85cba2 100644 --- a/deluge/ui/gtkui/options_tab.py +++ b/deluge/ui/gtkui/options_tab.py @@ -112,7 +112,7 @@ class OptionsTab(Tab): # We only want to update values that have been applied in the core. This # is so we don't overwrite the user changes that haven't been applied yet. if self.prev_status is None: - self.prev_status = {}.fromkeys(status.keys(), None) + self.prev_status = {}.fromkeys(list(status), None) if status != self.prev_status: if status["max_download_speed"] != self.prev_status["max_download_speed"]: diff --git a/deluge/ui/gtkui/path_chooser.py b/deluge/ui/gtkui/path_chooser.py index 21f8b23c2..84330fd65 100644 --- a/deluge/ui/gtkui/path_chooser.py +++ b/deluge/ui/gtkui/path_chooser.py @@ -58,7 +58,7 @@ class PathChoosersHandler(component.Component): self.config_properties.update(config) for chooser in self.path_choosers: chooser.set_config(config) - keys = self.config_keys_to_funcs_mapping.keys() + keys = list(self.config_keys_to_funcs_mapping) keys += self.paths_list_keys client.core.get_config_values(keys).addCallback(_on_config_values) @@ -107,7 +107,7 @@ class PathChoosersHandler(component.Component): chooser.set_values(values) def get_config_keys(self): - keys = self.config_keys_to_funcs_mapping.keys() + keys = list(self.config_keys_to_funcs_mapping) keys += self.paths_list_keys return keys diff --git a/deluge/ui/gtkui/peers_tab.py b/deluge/ui/gtkui/peers_tab.py index 4954e3217..23bfb50f7 100644 --- a/deluge/ui/gtkui/peers_tab.py +++ b/deluge/ui/gtkui/peers_tab.py @@ -296,7 +296,7 @@ class PeersTab(Tab): self.peers[peer["ip"]] = row # Now we need to remove any ips that were not in status["peers"] list - for ip in set(self.peers.keys()).difference(new_ips): + for ip in set(self.peers).difference(new_ips): self.liststore.remove(self.peers[ip]) del self.peers[ip] diff --git a/deluge/ui/gtkui/preferences.py b/deluge/ui/gtkui/preferences.py index 747ccce49..a09dafbdb 100644 --- a/deluge/ui/gtkui/preferences.py +++ b/deluge/ui/gtkui/preferences.py @@ -400,7 +400,7 @@ class Preferences(component.Component): core_widgets[self.copy_torrent_files_path_chooser] = ("path_chooser", "torrentfiles_location") # Update the widgets accordingly - for key in core_widgets.keys(): + for key in core_widgets: modifier = core_widgets[key][0] if isinstance(key, str): widget = self.builder.get_object(key) @@ -433,7 +433,7 @@ class Preferences(component.Component): widget.set_text(value, cursor_end=False, default_text=True) if self.is_connected: - for key in core_widgets.keys(): + for key in core_widgets: if isinstance(key, str): widget = self.builder.get_object(key) else: @@ -674,7 +674,7 @@ class Preferences(component.Component): dialog.run() # GtkUI - for key in new_gtkui_config.keys(): + for key in new_gtkui_config: # The values do not match so this needs to be updated if self.gtkui_config[key] != new_gtkui_config[key]: self.gtkui_config[key] = new_gtkui_config[key] @@ -683,7 +683,7 @@ class Preferences(component.Component): if client.connected(): # Only do this if we're connected to a daemon config_to_set = {} - for key in new_core_config.keys(): + for key in new_core_config: # The values do not match so this needs to be updated if self.core_config[key] != new_core_config[key]: config_to_set[key] = new_core_config[key] @@ -799,7 +799,7 @@ class Preferences(component.Component): def update_dependent_widgets(name, value): dependency = dependents[name] - for dep in dependency.keys(): + for dep in dependency: if dep in path_choosers: depwidget = path_choosers[dep] else: @@ -809,7 +809,7 @@ class Preferences(component.Component): if dep in dependents: update_dependent_widgets(dep, depwidget.get_active() and sensitive) - for key in dependents.keys(): + for key in dependents: if widget != self.builder.get_object(key): continue update_dependent_widgets(key, value) diff --git a/deluge/ui/gtkui/statusbar.py b/deluge/ui/gtkui/statusbar.py index f5182f450..ab32c1e8e 100644 --- a/deluge/ui/gtkui/statusbar.py +++ b/deluge/ui/gtkui/statusbar.py @@ -286,7 +286,7 @@ class StatusBar(component.Component): This is called when we receive a ConfigValueChangedEvent from the core. """ - if key in self.config_value_changed_dict.keys(): + if key in self.config_value_changed_dict: self.config_value_changed_dict[key](value) def _on_max_connections_global(self, max_connections): diff --git a/deluge/ui/gtkui/systemtray.py b/deluge/ui/gtkui/systemtray.py index 215bad354..2151c534a 100644 --- a/deluge/ui/gtkui/systemtray.py +++ b/deluge/ui/gtkui/systemtray.py @@ -179,7 +179,7 @@ class SystemTray(component.Component): def config_value_changed(self, key, value): """This is called when we received a config_value_changed signal from the core.""" - if key in self.config_value_changed_dict.keys(): + if key in self.config_value_changed_dict: self.config_value_changed_dict[key](value) def _on_max_download_speed(self, max_download_speed): diff --git a/deluge/ui/gtkui/torrentview.py b/deluge/ui/gtkui/torrentview.py index e2b4def65..798e28ce7 100644 --- a/deluge/ui/gtkui/torrentview.py +++ b/deluge/ui/gtkui/torrentview.py @@ -408,7 +408,7 @@ class TorrentView(ListView, component.Component): if columns is None: # We need to iterate through all columns - columns = self.columns.keys() + columns = list(self.columns) # Iterate through supplied list of columns to update for column in columns: @@ -481,7 +481,7 @@ class TorrentView(ListView, component.Component): # Get the columns to update from one of the torrents if status: - torrent_id = status.keys()[0] + torrent_id = list(status)[0] fields_to_update = [] for column in self.columns_to_update: column_index = self.get_column_index(column) @@ -621,7 +621,7 @@ class TorrentView(ListView, component.Component): return {} def get_visible_torrents(self): - return self.status.keys() + return list(self.status) # Callbacks # def on_button_press_event(self, widget, event): diff --git a/deluge/ui/sessionproxy.py b/deluge/ui/sessionproxy.py index 882de9c19..d06fba72d 100644 --- a/deluge/ui/sessionproxy.py +++ b/deluge/ui/sessionproxy.py @@ -90,7 +90,7 @@ class SessionProxy(component.Component): keys_to_remove = keys_diff_cached else: # Not the same keys so create a new diff - keys_to_remove = set(sd[torrent_id].iterkeys()) - keys + keys_to_remove = set(sd[torrent_id]) - keys # Update the cached diff keys_diff_cached = keys_to_remove keys_len = len(sd[torrent_id]) @@ -123,7 +123,7 @@ class SessionProxy(component.Component): # Keep track of keys we need to request from the core keys_to_get = [] if not keys: - keys = self.torrents[torrent_id][1].keys() + keys = list(self.torrents[torrent_id][1]) for key in keys: if time() - self.cache_times[torrent_id].get(key, 0.0) > self.cache_time: @@ -190,7 +190,7 @@ class SessionProxy(component.Component): # Create the status dict if not torrent_ids: - torrent_ids = result.keys() + torrent_ids = list(result) return self.create_status_dict(torrent_ids, keys) @@ -214,13 +214,13 @@ class SessionProxy(component.Component): if not filter_dict: # This means we want all the torrents status # We get a list of any torrent_ids with expired status dicts - to_fetch = find_torrents_to_fetch(self.torrents.keys()) + to_fetch = find_torrents_to_fetch(list(self.torrents)) if to_fetch: d = client.core.get_torrents_status({"id": to_fetch}, keys, True) - return d.addCallback(on_status, self.torrents.keys(), keys) + return d.addCallback(on_status, list(self.torrents), keys) # Don't need to fetch anything - return maybeDeferred(self.create_status_dict, self.torrents.keys(), keys) + return maybeDeferred(self.create_status_dict, list(self.torrents), keys) if len(filter_dict) == 1 and "id" in filter_dict: # At this point we should have a filter with just "id" in it diff --git a/deluge/ui/ui_entry.py b/deluge/ui/ui_entry.py index f99989662..7db99c316 100644 --- a/deluge/ui/ui_entry.py +++ b/deluge/ui/ui_entry.py @@ -39,7 +39,7 @@ def start_ui(): # Get the registered UI entry points ui_entrypoints = dict([(entrypoint.name, entrypoint.load()) for entrypoint in pkg_resources.iter_entry_points("deluge.ui")]) - ui_titles = sorted(ui_entrypoints.keys()) + ui_titles = sorted(ui_entrypoints) def add_ui_options_group(_parser): """Function to enable reuse of UI Options group""" diff --git a/deluge/ui/web/auth.py b/deluge/ui/web/auth.py index ae6b11da5..57416bfec 100644 --- a/deluge/ui/web/auth.py +++ b/deluge/ui/web/auth.py @@ -90,7 +90,7 @@ class Auth(JSONComponent): self.worker.stop() def _clean_sessions(self): - session_ids = self.config["sessions"].keys() + session_ids = list(self.config["sessions"]) now = time.gmtime() for session_id in session_ids: diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py index 34cd14efe..86657514b 100644 --- a/deluge/ui/web/json_api.py +++ b/deluge/ui/web/json_api.py @@ -891,7 +891,7 @@ class WebApi(JSONComponent): :type config: dictionary """ web_config = component.get("DelugeWeb").config - for key in config.keys(): + for key in config: if key in ["sessions", "pwd_salt", "pwd_sha1"]: log.warn("Ignored attempt to overwrite web config key '%s'", key) continue @@ -912,7 +912,7 @@ class WebApi(JSONComponent): """ return { - "enabled_plugins": component.get("Web.PluginManager").plugins.keys(), + "enabled_plugins": list(component.get("Web.PluginManager").plugins), "available_plugins": component.get("Web.PluginManager").available_plugins } diff --git a/gen_web_gettext.py b/gen_web_gettext.py index 519edf7bd..69dd91d8e 100755 --- a/gen_web_gettext.py +++ b/gen_web_gettext.py @@ -89,7 +89,7 @@ def create_gettext_js(js_dir): gettext_file = os.path.join(os.path.dirname(js_dir), 'gettext.js') with open(gettext_file, 'w') as fp: fp.write(gettext_tpl) - for key in sorted(strings.keys()): + for key in sorted(strings): if DEBUG: fp.write('\n// %s\n' % ', '.join(['%s:%s' % x for x in strings[key]])) fp.write('''GetText.add('%(key)s','${escape(_("%(key)s"))}')\n''' % locals()) diff --git a/msgfmt.py b/msgfmt.py index ac1e9d415..e4debdd00 100755 --- a/msgfmt.py +++ b/msgfmt.py @@ -60,9 +60,8 @@ def generate(): """ Return the generated output. """ - keys = MESSAGES.keys() # the keys are sorted in the .mo file - keys.sort() + keys = sorted(MESSAGES) offsets = [] ids = strs = '' for _id in keys: