[Console] Use io.open to en/decode utf8 files

This commit is contained in:
Calum Lind 2017-02-12 11:26:31 +00:00
parent 4619b31aa3
commit 04b8b14828
1 changed files with 5 additions and 7 deletions

View File

@ -13,6 +13,7 @@ from __future__ import unicode_literals
import logging import logging
import os import os
import re import re
from io import open
import deluge.component as component import deluge.component as component
import deluge.configmanager import deluge.configmanager
@ -131,7 +132,7 @@ class CmdLine(BaseMode, Commander):
self._hf_lines = [0, 0] self._hf_lines = [0, 0]
if self.console_config['cmdline']['save_command_history']: if self.console_config['cmdline']['save_command_history']:
try: try:
with open(self.history_file[0], 'r') as _file: with open(self.history_file[0], 'r', encoding='utf8') as _file:
lines1 = _file.read().splitlines() lines1 = _file.read().splitlines()
self._hf_lines[0] = len(lines1) self._hf_lines[0] = len(lines1)
except IOError: except IOError:
@ -139,7 +140,7 @@ class CmdLine(BaseMode, Commander):
self._hf_lines[0] = 0 self._hf_lines[0] = 0
try: try:
with open(self.history_file[1], 'r') as _file: with open(self.history_file[1], 'r', encoding='utf8') as _file:
lines2 = _file.read().splitlines() lines2 = _file.read().splitlines()
self._hf_lines[1] = len(lines2) self._hf_lines[1] = len(lines2)
except IOError: except IOError:
@ -158,9 +159,6 @@ class CmdLine(BaseMode, Commander):
# Instead of having additional input history file, we can # Instead of having additional input history file, we can
# simply scan for lines beginning with ">>> " # simply scan for lines beginning with ">>> "
for i, line in enumerate(self.lines): for i, line in enumerate(self.lines):
# if not isinstance(line, unicode):
# line = line.encode(self.encoding)
# self.lines[i] = line
line = remove_formatting(line) line = remove_formatting(line)
if line.startswith('>>> '): if line.startswith('>>> '):
console_input = line[4:] console_input = line[4:]
@ -418,7 +416,7 @@ class CmdLine(BaseMode, Commander):
active_file = 0 active_file = 0
# Write the line # Write the line
with open(self.history_file[active_file], 'a') as _file: with open(self.history_file[active_file], 'a', encoding='utf8') as _file:
if isinstance(text, unicode): if isinstance(text, unicode):
text = text.encode(self.encoding) text = text.encode(self.encoding)
_file.write(text) _file.write(text)
@ -431,7 +429,7 @@ class CmdLine(BaseMode, Commander):
# therefore swapping the currently active file # therefore swapping the currently active file
if self._hf_lines[active_file] == MAX_HISTFILE_SIZE: if self._hf_lines[active_file] == MAX_HISTFILE_SIZE:
self._hf_lines[1 - active_file] = 0 self._hf_lines[1 - active_file] = 0
with open(self.history_file[1 - active_file], 'w') as _file: with open(self.history_file[1 - active_file], 'w', encoding='utf8') as _file:
_file.truncate(0) _file.truncate(0)
def get_line_chunks(line): def get_line_chunks(line):