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