2020-09-27 16:54:49 +06:00
|
|
|
#!/usr/bin/env python3
|
2015-03-09 13:38:31 +06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-07 00:19:18 +06:00
|
|
|
import sys
|
2015-03-09 13:38:31 +06:00
|
|
|
|
|
|
|
# get sys.argv arguments and encode them into utf-8
|
2022-08-07 00:19:18 +06:00
|
|
|
def unicode_argv(default_name):
|
|
|
|
|
|
|
|
try:
|
|
|
|
from calibre.constants import iswindows
|
|
|
|
except:
|
|
|
|
iswindows = sys.platform.startswith('win')
|
|
|
|
|
|
|
|
if iswindows:
|
2015-03-09 13:38:31 +06:00
|
|
|
# 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
|
2020-10-14 21:23:49 +06:00
|
|
|
range(start, argc.value)]
|
2015-03-09 13:38:31 +06:00
|
|
|
# if we don't have any arguments at all, just pass back script name
|
|
|
|
# this should never happen
|
2022-08-07 00:19:18 +06:00
|
|
|
return [ default_name ]
|
2015-03-09 13:38:31 +06:00
|
|
|
else:
|
2020-10-14 21:23:49 +06:00
|
|
|
argvencoding = sys.stdin.encoding or "utf-8"
|
2021-11-16 16:09:03 +06:00
|
|
|
return [arg if (isinstance(arg, str) or isinstance(arg,unicode)) else str(arg, argvencoding) for arg in sys.argv]
|
2015-03-09 13:38:31 +06:00
|
|
|
|