From de555c31ac72c8a01b8cb890c5ca1db06973f69e Mon Sep 17 00:00:00 2001 From: MrS0m30n3 Date: Sun, 8 Mar 2015 16:53:55 +0200 Subject: [PATCH] Handle unicode --- youtube_dl_gui/downloaders.py | 2 +- youtube_dl_gui/logmanager.py | 8 ++++++-- youtube_dl_gui/parsers.py | 11 +++++++---- youtube_dl_gui/utils.py | 32 ++++++++++++++++++++++++++++---- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/youtube_dl_gui/downloaders.py b/youtube_dl_gui/downloaders.py index 921c48d..378d244 100644 --- a/youtube_dl_gui/downloaders.py +++ b/youtube_dl_gui/downloaders.py @@ -266,7 +266,7 @@ class YoutubeDLDownloader(object): # Encode command for subprocess # Refer to http://stackoverflow.com/a/9951851/35070 - if sys.version_info < (3, 0) and sys.platform == 'win32': + if sys.version_info < (3, 0): encoding = self._get_encoding() if encoding is not None: diff --git a/youtube_dl_gui/logmanager.py b/youtube_dl_gui/logmanager.py index 229377b..0751e56 100644 --- a/youtube_dl_gui/logmanager.py +++ b/youtube_dl_gui/logmanager.py @@ -8,7 +8,10 @@ from __future__ import unicode_literals import os.path from time import strftime -from .utils import check_path +from .utils import ( + get_encoding, + check_path +) class LogManager(object): @@ -38,6 +41,7 @@ class LogManager(object): self.config_path = config_path self.add_time = add_time self.log_file = os.path.join(config_path, self.LOG_FILENAME) + self._encoding = get_encoding() self._init_log() self._auto_clear_log() @@ -74,7 +78,7 @@ class LogManager(object): else: msg = data - log.write(msg) + log.write(msg.encode(self._encoding, 'ignore')) def _init_log(self): """Initialize the log file if not exist. """ diff --git a/youtube_dl_gui/parsers.py b/youtube_dl_gui/parsers.py index 8612993..b77d155 100644 --- a/youtube_dl_gui/parsers.py +++ b/youtube_dl_gui/parsers.py @@ -7,7 +7,10 @@ from __future__ import unicode_literals import os.path -from .utils import remove_shortcuts +from .utils import ( + remove_shortcuts, + to_string +) class OptionHolder(object): @@ -132,7 +135,7 @@ class OptionsParser(object): options_list.append(option.flag) if not option.is_boolean(): - options_list.append(unicode(value)) + options_list.append(to_string(value)) # Parse cmd_args for option in options_dict['cmd_args'].split(): @@ -185,7 +188,7 @@ class OptionsParser(object): """ if options_dict['min_filesize']: - options_dict['min_filesize'] = unicode(options_dict['min_filesize']) + options_dict['min_filesize_unit'] + options_dict['min_filesize'] = to_string(options_dict['min_filesize']) + options_dict['min_filesize_unit'] if options_dict['max_filesize']: - options_dict['max_filesize'] = unicode(options_dict['max_filesize']) + options_dict['max_filesize_unit'] + options_dict['max_filesize'] = to_string(options_dict['max_filesize']) + options_dict['max_filesize_unit'] diff --git a/youtube_dl_gui/utils.py b/youtube_dl_gui/utils.py index a927256..d3fb908 100644 --- a/youtube_dl_gui/utils.py +++ b/youtube_dl_gui/utils.py @@ -14,6 +14,7 @@ from __future__ import unicode_literals import os import sys +import locale import subprocess from .info import __appname__ @@ -36,7 +37,12 @@ def remove_shortcuts(path): def absolute_path(filename): """Return absolute path to the given file. """ path = os.path.realpath(os.path.abspath(filename)) - return os.path.dirname(path) + return os.path.dirname(path).decode(get_encoding(), 'ignore') + + +def get_lib_path(): + """Return path to the current file. """ + return os.path.dirname(__file__).decode(get_encoding(), 'ignore') def open_dir(path): @@ -88,8 +94,15 @@ def shutdown_sys(password=''): if not password: subprocess.call(['/sbin/shutdown', '-h', 'now']) else: + password = ('%s\n' % password).encode(get_encoding()) subprocess.Popen(['sudo', '-S', '/sbin/shutdown', '-h', 'now'], - stdin=subprocess.PIPE).communicate(password + '\n') + stdin=subprocess.PIPE).communicate(password) + + +def to_string(data): + """Convert data to string. + Works for both Python2 & Python3. """ + return '%s' % data def get_time(seconds): @@ -113,6 +126,17 @@ def get_time(seconds): return dtime +def get_encoding(): + """Return system encoding. """ + try: + encoding = locale.getpreferredencoding() + 'TEST'.encode(encoding) + except: + encoding = 'UTF-8' + + return encoding + + def get_locale_file(): """Search for youtube-dlg locale file. @@ -129,7 +153,7 @@ def get_locale_file(): SEARCH_DIRS = [ os.path.join(absolute_path(sys.argv[0]), DIR_NAME), - os.path.join(os.path.dirname(__file__), DIR_NAME), + os.path.join(get_lib_path(), DIR_NAME), os.path.join('/usr', 'share', __appname__.lower(), DIR_NAME) ] @@ -159,7 +183,7 @@ def get_icon_file(): search_dirs = [ os.path.join(absolute_path(sys.argv[0]), 'icons'), - os.path.join(os.path.dirname(__file__), 'icons'), + os.path.join(get_lib_path(), 'icons'), ] # Append $XDG_DATA_DIRS on search_dirs