mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-05 07:58:38 +00:00
Get cli arguments in unicode in deluge core as well.
Make a better guess at what arguments are encoded in on linux.
This commit is contained in:
parent
c2d301bf52
commit
1ac62fce01
3 changed files with 39 additions and 34 deletions
|
@ -68,6 +68,7 @@ if not hasattr(json, "dumps"):
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
import gettext
|
import gettext
|
||||||
import locale
|
import locale
|
||||||
|
import sys
|
||||||
|
|
||||||
from deluge.error import *
|
from deluge.error import *
|
||||||
|
|
||||||
|
@ -751,3 +752,39 @@ def setup_translations(setup_pygtk=False):
|
||||||
log.exception(e)
|
log.exception(e)
|
||||||
import __builtin__
|
import __builtin__
|
||||||
__builtin__.__dict__["_"] = lambda x: x
|
__builtin__.__dict__["_"] = lambda x: x
|
||||||
|
|
||||||
|
def unicode_argv():
|
||||||
|
""" Gets sys.argv as list of unicode objects on any platform."""
|
||||||
|
if windows_check():
|
||||||
|
# Versions 2.x of Python don't support Unicode in sys.argv on
|
||||||
|
# Windows, with the underlying Windows API instead replacing multi-byte
|
||||||
|
# characters with '?'.
|
||||||
|
from ctypes import POINTER, byref, cdll, c_int, windll
|
||||||
|
from ctypes.wintypes import LPCWSTR, LPWSTR
|
||||||
|
|
||||||
|
GetCommandLineW = cdll.kernel32.GetCommandLineW
|
||||||
|
GetCommandLineW.argtypes = []
|
||||||
|
GetCommandLineW.restype = LPCWSTR
|
||||||
|
|
||||||
|
CommandLineToArgvW = windll.shell32.CommandLineToArgvW
|
||||||
|
CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
|
||||||
|
CommandLineToArgvW.restype = POINTER(LPWSTR)
|
||||||
|
|
||||||
|
cmd = GetCommandLineW()
|
||||||
|
argc = c_int(0)
|
||||||
|
argv = CommandLineToArgvW(cmd, byref(argc))
|
||||||
|
if argc.value > 0:
|
||||||
|
# Remove Python executable and commands if present
|
||||||
|
start = argc.value - len(sys.argv)
|
||||||
|
return [argv[i] for i in
|
||||||
|
xrange(start, argc.value)]
|
||||||
|
else:
|
||||||
|
# On other platforms, we have to find the likely encoding of the args and decode
|
||||||
|
# First check if sys.stdout or stdin have encoding set
|
||||||
|
encoding = getattr(sys.stdout, "encoding") or getattr(sys.stdin, "encoding")
|
||||||
|
# If that fails, check what the locale is set to
|
||||||
|
encoding = encoding or locale.getpreferredencoding()
|
||||||
|
# As a last resort, just default to utf-8
|
||||||
|
encoding = encoding or "utf-8"
|
||||||
|
|
||||||
|
return [arg.decode(encoding) for arg in sys.argv]
|
||||||
|
|
|
@ -86,7 +86,7 @@ def start_ui():
|
||||||
help="Rotate logfiles.", action="store_true", default=False)
|
help="Rotate logfiles.", action="store_true", default=False)
|
||||||
|
|
||||||
# Get the options and args from the OptionParser
|
# Get the options and args from the OptionParser
|
||||||
(options, args) = parser.parse_args()
|
(options, args) = parser.parse_args(deluge.common.unicode_argv()[1:])
|
||||||
|
|
||||||
if options.quiet:
|
if options.quiet:
|
||||||
options.loglevel = "none"
|
options.loglevel = "none"
|
||||||
|
|
|
@ -64,35 +64,6 @@ if 'dev' not in deluge.common.get_version():
|
||||||
import warnings
|
import warnings
|
||||||
warnings.filterwarnings('ignore', category=DeprecationWarning, module='twisted')
|
warnings.filterwarnings('ignore', category=DeprecationWarning, module='twisted')
|
||||||
|
|
||||||
def win32_unicode_argv():
|
|
||||||
"""Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
|
|
||||||
strings.
|
|
||||||
|
|
||||||
Versions 2.x of Python don't support Unicode in sys.argv on
|
|
||||||
Windows, with the underlying Windows API instead replacing multi-byte
|
|
||||||
characters with '?'.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from ctypes import POINTER, byref, cdll, c_int, windll
|
|
||||||
from ctypes.wintypes import LPCWSTR, LPWSTR
|
|
||||||
|
|
||||||
GetCommandLineW = cdll.kernel32.GetCommandLineW
|
|
||||||
GetCommandLineW.argtypes = []
|
|
||||||
GetCommandLineW.restype = LPCWSTR
|
|
||||||
|
|
||||||
CommandLineToArgvW = windll.shell32.CommandLineToArgvW
|
|
||||||
CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
|
|
||||||
CommandLineToArgvW.restype = POINTER(LPWSTR)
|
|
||||||
|
|
||||||
cmd = GetCommandLineW()
|
|
||||||
argc = c_int(0)
|
|
||||||
argv = CommandLineToArgvW(cmd, byref(argc))
|
|
||||||
if argc.value > 0:
|
|
||||||
# Remove Python executable and commands if present
|
|
||||||
start = argc.value - len(sys.argv)
|
|
||||||
return [argv[i] for i in
|
|
||||||
xrange(start, argc.value)]
|
|
||||||
|
|
||||||
class _UI(object):
|
class _UI(object):
|
||||||
|
|
||||||
def __init__(self, name="gtk"):
|
def __init__(self, name="gtk"):
|
||||||
|
@ -137,10 +108,7 @@ class _UI(object):
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
# Make sure all arguments are unicode
|
# Make sure all arguments are unicode
|
||||||
if deluge.common.windows_check():
|
argv = deluge.common.unicode_argv()[1:]
|
||||||
argv = win32_unicode_argv()[1:]
|
|
||||||
else:
|
|
||||||
argv = [arg.decode(sys.stdin.encoding) for arg in sys.argv[1:]]
|
|
||||||
(self.__options, self.__args) = self.__parser.parse_args(argv)
|
(self.__options, self.__args) = self.__parser.parse_args(argv)
|
||||||
|
|
||||||
if self.__options.quiet:
|
if self.__options.quiet:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue