diff options
author | Calum Lind <calumlind+deluge@gmail.com> | 2017-02-22 13:40:17 +0000 |
---|---|---|
committer | Calum Lind <calumlind+deluge@gmail.com> | 2017-02-23 00:35:43 +0000 |
commit | b2db96e4df86ef38d79b9c304d527e793597260c (patch) | |
tree | a123a1fdcb73eb66f6aca87bf3ccf6048e80a69c /deluge/ui | |
parent | 52a85cb91cc3f536941133bfff1ef26f6855e151 (diff) | |
download | deluge-b2db96e4df86ef38d79b9c304d527e793597260c.tar.gz deluge-b2db96e4df86ef38d79b9c304d527e793597260c.tar.bz2 deluge-b2db96e4df86ef38d79b9c304d527e793597260c.zip |
[Py2to3] Refactor out usage of unicode and basestring
- Python 3 renames `unicode` type to `str` and introduces `bytes` type.
- Python 2.7 has `bytes` but is only an alias for `str` so restricted
to comparisons but helps keep compatibility.
- To test for unicode string on Py2 and Py3 uses the "''.__class__" type.
- Remove usage of utf8encode and just encode, problems with bytes being passed
in code will be picked up faster.
- Where possible refactor out isinstance for try..except duck-typing.
Diffstat (limited to 'deluge/ui')
-rw-r--r-- | deluge/ui/console/cmdline/commands/info.py | 3 | ||||
-rw-r--r-- | deluge/ui/console/main.py | 10 | ||||
-rw-r--r-- | deluge/ui/console/modes/cmdline.py | 5 | ||||
-rw-r--r-- | deluge/ui/console/modes/torrentlist/torrentactions.py | 13 | ||||
-rw-r--r-- | deluge/ui/console/modes/torrentlist/torrentview.py | 15 | ||||
-rw-r--r-- | deluge/ui/console/utils/colors.py | 13 | ||||
-rw-r--r-- | deluge/ui/console/utils/format_utils.py | 18 | ||||
-rw-r--r-- | deluge/ui/console/widgets/inputpane.py | 2 | ||||
-rw-r--r-- | deluge/ui/gtkui/listview.py | 3 | ||||
-rw-r--r-- | deluge/ui/gtkui/preferences.py | 16 | ||||
-rw-r--r-- | deluge/ui/translations_util.py | 1 | ||||
-rw-r--r-- | deluge/ui/web/auth.py | 10 | ||||
-rw-r--r-- | deluge/ui/web/json_api.py | 2 |
13 files changed, 40 insertions, 71 deletions
diff --git a/deluge/ui/console/cmdline/commands/info.py b/deluge/ui/console/cmdline/commands/info.py index 84838713f..fc80d1aba 100644 --- a/deluge/ui/console/cmdline/commands/info.py +++ b/deluge/ui/console/cmdline/commands/info.py @@ -212,9 +212,6 @@ class Command(BaseCommand): def tlen(string): return strwidth(format_utils.remove_formatting(string)) - if not isinstance(col_filename, unicode): - col_filename = unicode(col_filename, 'utf-8') - col_all_info = col_size + col_progress + col_priority # Check how much space we've got left after writing all the info space_left = cols - tlen(col_all_info) diff --git a/deluge/ui/console/main.py b/deluge/ui/console/main.py index cce63d662..7b7904c0a 100644 --- a/deluge/ui/console/main.py +++ b/deluge/ui/console/main.py @@ -371,8 +371,7 @@ Please use commands from the command line, e.g.:\n no matches are found. """ - if not isinstance(string, unicode): - string = unicode(string, self.encoding) + deluge.common.decode_bytes(string, self.encoding) if string == '*' or string == '': return [tid for tid, name in self.torrents] @@ -387,8 +386,7 @@ Please use commands from the command line, e.g.:\n matches = [] for tid, name in self.torrents: - if not isinstance(name, unicode): - name = unicode(name, self.encoding) + deluge.common.decode_bytes(name, self.encoding) if getattr(tid, match_func, None)(string) or getattr(name, match_func, None)(string): matches.append(tid) return matches @@ -423,7 +421,7 @@ Please use commands from the command line, e.g.:\n component.get('CmdLine').add_line(s, False) self.events.append(s) else: - print(colors.strip_colors(deluge.common.utf8_encoded(s))) + print(colors.strip_colors(s.encode('utf8'))) def write_event(self, s): if self.interactive: @@ -434,7 +432,7 @@ Please use commands from the command line, e.g.:\n component.get('CmdLine').add_line(s, False) self.events.append(s) else: - print(colors.strip_colors(deluge.common.utf8_encoded(s))) + print(colors.strip_colors(s.encode('utf8'))) def _migrate_config_1_to_2(self, config): """Create better structure by moving most settings out of dict root diff --git a/deluge/ui/console/modes/cmdline.py b/deluge/ui/console/modes/cmdline.py index 0eb134e6c..287791a52 100644 --- a/deluge/ui/console/modes/cmdline.py +++ b/deluge/ui/console/modes/cmdline.py @@ -417,10 +417,7 @@ class CmdLine(BaseMode, Commander): # Write the line with open(self.history_file[active_file], 'a', encoding='utf8') as _file: - if isinstance(text, unicode): - text = text.encode(self.encoding) - _file.write(text) - _file.write(os.linesep) + _file.write(text + '\n') # And increment line counter self._hf_lines[active_file] += 1 diff --git a/deluge/ui/console/modes/torrentlist/torrentactions.py b/deluge/ui/console/modes/torrentlist/torrentactions.py index d855aaa95..404d2c27d 100644 --- a/deluge/ui/console/modes/torrentlist/torrentactions.py +++ b/deluge/ui/console/modes/torrentlist/torrentactions.py @@ -131,17 +131,14 @@ def action_torrent_info(mode=None, torrent_ids=None, **kwargs): for field in torrent_options: caption = '{!info!}' + TORRENT_DATA_FIELD[field]['name'] value = options[field] - field_type = type(value) - if field_type in [str, unicode]: - if not isinstance(value, basestring): - value = str(value) + if isinstance(value, ''.__class__): option_popup.add_text_input(field, caption, value) - elif field_type == bool: - choices = (['Yes', 'No'], [True, False], [True, False].index(options[field])) + elif isinstance(value, bool): + choices = (['Yes', 'No'], [True, False], [True, False].index(value)) option_popup.add_select_input(field, caption, choices[0], choices[1], choices[2]) - elif field_type == float: + elif isinstance(value, float): option_popup.add_float_spin_input(field, caption, value=value, min_val=-1) - elif field_type == int: + elif isinstance(value, int): option_popup.add_int_spin_input(field, caption, value=value, min_val=-1) mode.push_popup(option_popup) diff --git a/deluge/ui/console/modes/torrentlist/torrentview.py b/deluge/ui/console/modes/torrentlist/torrentview.py index 98b18f1ec..6ef1d7e83 100644 --- a/deluge/ui/console/modes/torrentlist/torrentview.py +++ b/deluge/ui/console/modes/torrentlist/torrentview.py @@ -237,18 +237,15 @@ class TorrentView(InputKeyHandler): # and if it's a string first_element = state[state.keys()[0]] if field in first_element: - is_string = isinstance(first_element[field], basestring) - def sort_key(s): - return state.get(s)[field] - - def sort_key2(s): - return state.get(s)[field].lower() + try: + # Sort case-insensitively but preserve A>a order. + return state.get(s)[field].lower() + except AttributeError: + # Not a string. + return state.get(s)[field] - # If it's a string, sort case-insensitively but preserve A>a order to_sort = sorted(to_sort, _queue_sort, sort_key, reverse) - if is_string: - to_sort = sorted(to_sort, _queue_sort, sort_key2, reverse) if field == 'eta': to_sort = sorted(to_sort, key=lambda s: state.get(s)['eta'] == 0) diff --git a/deluge/ui/console/utils/colors.py b/deluge/ui/console/utils/colors.py index 50ec6d40c..17119e71a 100644 --- a/deluge/ui/console/utils/colors.py +++ b/deluge/ui/console/utils/colors.py @@ -131,7 +131,7 @@ def strip_colors(line): return line -def get_line_length(line, encoding='UTF-8'): +def get_line_length(line): """ Returns the string length without the color formatting. @@ -139,9 +139,6 @@ def get_line_length(line, encoding='UTF-8'): if line.count('{!') != line.count('!}'): raise BadColorString('Number of {! is not equal to number of !}') - if isinstance(line, unicode): - line = line.encode(encoding, 'replace') - # Remove all the color tags line = strip_colors(line) @@ -150,7 +147,7 @@ def get_line_length(line, encoding='UTF-8'): return len(line) -def get_line_width(line, encoding='UTF-8'): +def get_line_width(line): """ Get width of string considering double width characters @@ -158,9 +155,6 @@ def get_line_width(line, encoding='UTF-8'): if line.count('{!') != line.count('!}'): raise BadColorString('Number of {! is not equal to number of !}') - if isinstance(line, unicode): - line = line.encode(encoding, 'replace') - # Remove all the color tags line = strip_colors(line) @@ -180,9 +174,6 @@ def parse_color_string(s, encoding='UTF-8'): if s.count('{!') != s.count('!}'): raise BadColorString('Number of {! is not equal to number of !}') - if isinstance(s, unicode): - s = s.encode(encoding, 'replace') - ret = [] last_color_attr = None # Keep track of where the strings diff --git a/deluge/ui/console/utils/format_utils.py b/deluge/ui/console/utils/format_utils.py index f259061d6..ac7fc2539 100644 --- a/deluge/ui/console/utils/format_utils.py +++ b/deluge/ui/console/utils/format_utils.py @@ -11,8 +11,7 @@ from __future__ import unicode_literals import re from collections import deque -from unicodedata import normalize as ud_normalize -from unicodedata import east_asian_width +from unicodedata import east_asian_width, normalize import deluge.common from deluge.ui.common import FILE_PRIORITY @@ -88,7 +87,7 @@ def trim_string(string, w, have_dbls): idx = 0 while width < w: chrs.append(string[idx]) - if east_asian_width(string[idx]) in ['W', 'F']: + if east_asian_width(string[idx]) in 'WF': width += 2 else: width += 1 @@ -102,14 +101,13 @@ def trim_string(string, w, have_dbls): def format_column(col, lim): - dbls = 0 - # Chosen over isinstance(col, unicode) and col.__class__ == unicode - # for speed - it's ~3 times faster for non-unicode strings and ~1.5 - # for unicode strings. - if col.__class__ is unicode: + try: # might have some double width chars - col = ud_normalize('NFC', col) + col = normalize('NFC', col) dbls = sum(east_asian_width(c) in 'WF' for c in col) + except TypeError: + dbls = 0 + size = len(col) + dbls if size >= lim - 1: return trim_string(col, lim, dbls > 0) @@ -239,8 +237,6 @@ def strwidth(string): """ Measure width of a string considering asian double width characters """ - if not isinstance(string, unicode): - string = unicode(string, 'utf-8') return sum([1 + (east_asian_width(char) in ['W', 'F']) for char in string]) diff --git a/deluge/ui/console/widgets/inputpane.py b/deluge/ui/console/widgets/inputpane.py index e76708f21..cf53b48ea 100644 --- a/deluge/ui/console/widgets/inputpane.py +++ b/deluge/ui/console/widgets/inputpane.py @@ -299,7 +299,7 @@ class BaseInputPane(InputKeyHandler): if ipt.default_col != -1: default_col = int(ipt.default_col) - if isinstance(ipt.default_col, basestring) and ipt.default_col[0] in ['+', '-']: + if isinstance(ipt.default_col, ''.__class__) and ipt.default_col[0] in ['+', '-']: col += default_col cursor_offset += default_col field_width -= default_col # Increase to col must be reflected here diff --git a/deluge/ui/gtkui/listview.py b/deluge/ui/gtkui/listview.py index e61162a6f..2b3f7b1e5 100644 --- a/deluge/ui/gtkui/listview.py +++ b/deluge/ui/gtkui/listview.py @@ -15,6 +15,7 @@ import gtk from gobject import SIGNAL_RUN_LAST, TYPE_NONE, signal_new from gtk.gdk import Event # pylint: disable=ungrouped-imports +from deluge.common import decode_bytes from deluge.ui.gtkui.common import load_pickled_state_file, save_pickled_state_file signal_new('button-press-event', gtk.TreeViewColumn, SIGNAL_RUN_LAST, TYPE_NONE, (Event,)) @@ -320,7 +321,7 @@ class ListView(object): try: self.columns[name].column.set_visible(widget.get_active()) except KeyError: - self.columns[unicode(name)].column.set_visible(widget.get_active()) + self.columns[decode_bytes(name)].column.set_visible(widget.get_active()) return def on_treeview_header_right_clicked(self, column, event): diff --git a/deluge/ui/gtkui/preferences.py b/deluge/ui/gtkui/preferences.py index ab5954cc7..2e424549c 100644 --- a/deluge/ui/gtkui/preferences.py +++ b/deluge/ui/gtkui/preferences.py @@ -401,20 +401,20 @@ class Preferences(component.Component): # Update the widgets accordingly for key in core_widgets: modifier = core_widgets[key][0] - if isinstance(key, basestring): + try: widget = self.builder.get_object(key) - else: + except TypeError: widget = key widget.set_sensitive(self.is_connected) if self.is_connected: value = core_widgets[key][1] - from types import FunctionType - if isinstance(value, FunctionType): - value = value() - elif isinstance(value, basestring): + try: value = self.core_config[value] + except KeyError: + if callable(value): + value = value() elif modifier: value = {'active': False, 'not_active': False, 'value': 0, 'text': '', 'path_chooser': ''}[modifier] @@ -433,9 +433,9 @@ class Preferences(component.Component): if self.is_connected: for key in core_widgets: - if isinstance(key, basestring): + try: widget = self.builder.get_object(key) - else: + except TypeError: widget = key # Update the toggle status if necessary self.on_toggle(widget) diff --git a/deluge/ui/translations_util.py b/deluge/ui/translations_util.py index 6803d55a2..656cabd1c 100644 --- a/deluge/ui/translations_util.py +++ b/deluge/ui/translations_util.py @@ -70,7 +70,6 @@ def set_language(lang): :param lang: the language, e.g. "en", "de" or "en_GB" :type lang: str """ - lang = str(lang) # Necessary to set these environment variables for GtkBuilder deluge.common.set_env_variable('LANGUAGE', lang) # Windows/Linux deluge.common.set_env_variable('LANG', lang) # For OSX diff --git a/deluge/ui/web/auth.py b/deluge/ui/web/auth.py index be862a11d..d5a439d42 100644 --- a/deluge/ui/web/auth.py +++ b/deluge/ui/web/auth.py @@ -18,8 +18,6 @@ from email.utils import formatdate from twisted.internet.task import LoopingCall -from deluge.common import utf8_encoded - log = logging.getLogger(__name__) @@ -143,7 +141,7 @@ class Auth(JSONComponent): log.debug('Received a password via the 1.2-dev auth method') m = hashlib.md5() m.update(config['pwd_salt']) - m.update(utf8_encoded(password)) + m.update(password.encode('utf8')) if m.hexdigest() == config['pwd_md5']: # We want to move the password over to sha1 and remove # the old passwords from the config file. @@ -163,7 +161,7 @@ class Auth(JSONComponent): from base64 import decodestring m = hashlib.md5() m.update(decodestring(config['old_pwd_salt'])) - m.update(utf8_encoded(password)) + m.update(password.encode('utf8')) if m.digest() == decodestring(config['old_pwd_md5']): # We want to move the password over to sha1 and remove @@ -179,7 +177,7 @@ class Auth(JSONComponent): log.debug('Received a password via the 1.2 auth method') s = hashlib.sha1() s.update(config['pwd_salt']) - s.update(utf8_encoded(password)) + s.update(password.encode('utf8')) if s.hexdigest() == config['pwd_sha1']: return True @@ -249,7 +247,7 @@ class Auth(JSONComponent): log.debug('Changing password') salt = hashlib.sha1(os.urandom(32)).hexdigest() s = hashlib.sha1(salt) - s.update(utf8_encoded(new_password)) + s.update(new_password.encode('utf8')) self.config['pwd_salt'] = salt self.config['pwd_sha1'] = s.hexdigest() return True diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py index 96cf80aaf..0e66408c8 100644 --- a/deluge/ui/web/json_api.py +++ b/deluge/ui/web/json_api.py @@ -897,8 +897,6 @@ class WebApi(JSONComponent): if key in ['sessions', 'pwd_salt', 'pwd_sha1']: log.warn('Ignored attempt to overwrite web config key: %s', key) continue - if isinstance(config[key], basestring): - config[key] = config[key].encode('utf8') web_config[key] = config[key] @export |