Browse Source

Handle unicode

doc-issue-template
MrS0m30n3 9 years ago
parent
commit
de555c31ac
4 changed files with 42 additions and 11 deletions
  1. 2
      youtube_dl_gui/downloaders.py
  2. 8
      youtube_dl_gui/logmanager.py
  3. 11
      youtube_dl_gui/parsers.py
  4. 32
      youtube_dl_gui/utils.py

2
youtube_dl_gui/downloaders.py

@ -266,7 +266,7 @@ class YoutubeDLDownloader(object):
# Encode command for subprocess # Encode command for subprocess
# Refer to http://stackoverflow.com/a/9951851/35070 # 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() encoding = self._get_encoding()
if encoding is not None: if encoding is not None:

8
youtube_dl_gui/logmanager.py

@ -8,7 +8,10 @@ from __future__ import unicode_literals
import os.path import os.path
from time import strftime from time import strftime
from .utils import check_path
from .utils import (
get_encoding,
check_path
)
class LogManager(object): class LogManager(object):
@ -38,6 +41,7 @@ class LogManager(object):
self.config_path = config_path self.config_path = config_path
self.add_time = add_time self.add_time = add_time
self.log_file = os.path.join(config_path, self.LOG_FILENAME) self.log_file = os.path.join(config_path, self.LOG_FILENAME)
self._encoding = get_encoding()
self._init_log() self._init_log()
self._auto_clear_log() self._auto_clear_log()
@ -74,7 +78,7 @@ class LogManager(object):
else: else:
msg = data msg = data
log.write(msg)
log.write(msg.encode(self._encoding, 'ignore'))
def _init_log(self): def _init_log(self):
"""Initialize the log file if not exist. """ """Initialize the log file if not exist. """

11
youtube_dl_gui/parsers.py

@ -7,7 +7,10 @@ from __future__ import unicode_literals
import os.path import os.path
from .utils import remove_shortcuts
from .utils import (
remove_shortcuts,
to_string
)
class OptionHolder(object): class OptionHolder(object):
@ -132,7 +135,7 @@ class OptionsParser(object):
options_list.append(option.flag) options_list.append(option.flag)
if not option.is_boolean(): if not option.is_boolean():
options_list.append(unicode(value))
options_list.append(to_string(value))
# Parse cmd_args # Parse cmd_args
for option in options_dict['cmd_args'].split(): for option in options_dict['cmd_args'].split():
@ -185,7 +188,7 @@ class OptionsParser(object):
""" """
if options_dict['min_filesize']: 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']: 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']

32
youtube_dl_gui/utils.py

@ -14,6 +14,7 @@ from __future__ import unicode_literals
import os import os
import sys import sys
import locale
import subprocess import subprocess
from .info import __appname__ from .info import __appname__
@ -36,7 +37,12 @@ def remove_shortcuts(path):
def absolute_path(filename): def absolute_path(filename):
"""Return absolute path to the given file. """ """Return absolute path to the given file. """
path = os.path.realpath(os.path.abspath(filename)) 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): def open_dir(path):
@ -88,8 +94,15 @@ def shutdown_sys(password=''):
if not password: if not password:
subprocess.call(['/sbin/shutdown', '-h', 'now']) subprocess.call(['/sbin/shutdown', '-h', 'now'])
else: else:
password = ('%s\n' % password).encode(get_encoding())
subprocess.Popen(['sudo', '-S', '/sbin/shutdown', '-h', 'now'], 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): def get_time(seconds):
@ -113,6 +126,17 @@ def get_time(seconds):
return dtime 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(): def get_locale_file():
"""Search for youtube-dlg locale file. """Search for youtube-dlg locale file.
@ -129,7 +153,7 @@ def get_locale_file():
SEARCH_DIRS = [ SEARCH_DIRS = [
os.path.join(absolute_path(sys.argv[0]), DIR_NAME), 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) os.path.join('/usr', 'share', __appname__.lower(), DIR_NAME)
] ]
@ -159,7 +183,7 @@ def get_icon_file():
search_dirs = [ search_dirs = [
os.path.join(absolute_path(sys.argv[0]), 'icons'), 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 # Append $XDG_DATA_DIRS on search_dirs

Loading…
Cancel
Save