Optionally process sys and user config in embedded app
This commit is contained in:
parent
4e51ec5f57
commit
68becf5ea2
@ -43,7 +43,7 @@ from .extractor import gen_extractors, list_extractors
|
|||||||
from .YoutubeDL import YoutubeDL
|
from .YoutubeDL import YoutubeDL
|
||||||
|
|
||||||
|
|
||||||
def _real_main(argv=None):
|
def _real_main(argv=None, ignoreConfig=True):
|
||||||
# Compatibility fixes for Windows
|
# Compatibility fixes for Windows
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
# https://github.com/rg3/youtube-dl/issues/820
|
# https://github.com/rg3/youtube-dl/issues/820
|
||||||
@ -53,7 +53,7 @@ def _real_main(argv=None):
|
|||||||
|
|
||||||
setproctitle('youtube-dl')
|
setproctitle('youtube-dl')
|
||||||
|
|
||||||
parser, opts, args = parseOpts(argv)
|
parser, opts, args = parseOpts(argv, ignoreConfig)
|
||||||
|
|
||||||
# Set user agent
|
# Set user agent
|
||||||
if opts.user_agent is not None:
|
if opts.user_agent is not None:
|
||||||
@ -382,8 +382,6 @@ def _real_main(argv=None):
|
|||||||
'external_downloader_args': external_downloader_args,
|
'external_downloader_args': external_downloader_args,
|
||||||
'postprocessor_args': postprocessor_args,
|
'postprocessor_args': postprocessor_args,
|
||||||
'cn_verification_proxy': opts.cn_verification_proxy,
|
'cn_verification_proxy': opts.cn_verification_proxy,
|
||||||
'geo_verification_proxy': opts.geo_verification_proxy,
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
with YoutubeDL(ydl_opts) as ydl:
|
with YoutubeDL(ydl_opts) as ydl:
|
||||||
@ -417,9 +415,9 @@ def _real_main(argv=None):
|
|||||||
sys.exit(retcode)
|
sys.exit(retcode)
|
||||||
|
|
||||||
|
|
||||||
def main(argv=None):
|
def main(argv=None, ignoreConfig=True):
|
||||||
try:
|
try:
|
||||||
_real_main(argv)
|
_real_main(argv, ignoreConfig)
|
||||||
except DownloadError:
|
except DownloadError:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
except SameFileError:
|
except SameFileError:
|
||||||
|
@ -19,18 +19,16 @@ from .utils import (
|
|||||||
from .version import __version__
|
from .version import __version__
|
||||||
|
|
||||||
|
|
||||||
def parseOpts(overrideArguments=None):
|
def parseOpts(overrideArguments=None, overrideIgnoreConfig=True):
|
||||||
def _readOptions(filename_bytes, default=[]):
|
def _readOptions(filename_bytes, default=[]):
|
||||||
try:
|
try:
|
||||||
optionf = open(filename_bytes)
|
optionf = open(filename_bytes)
|
||||||
except IOError:
|
except IOError:
|
||||||
return default # silently skip if file is not present
|
return default # silently skip if file is not present
|
||||||
try:
|
try:
|
||||||
# FIXME: https://github.com/rg3/youtube-dl/commit/dfe5fa49aed02cf36ba9f743b11b0903554b5e56
|
res = []
|
||||||
contents = optionf.read()
|
for l in optionf:
|
||||||
if sys.version_info < (3,):
|
res += compat_shlex_split(l, comments=True)
|
||||||
contents = contents.decode(preferredencoding())
|
|
||||||
res = compat_shlex_split(contents, comments=True)
|
|
||||||
finally:
|
finally:
|
||||||
optionf.close()
|
optionf.close()
|
||||||
return res
|
return res
|
||||||
@ -102,6 +100,7 @@ def parseOpts(overrideArguments=None):
|
|||||||
pass
|
pass
|
||||||
return opts
|
return opts
|
||||||
|
|
||||||
|
def create_parser():
|
||||||
# No need to wrap help messages if we're on a wide console
|
# No need to wrap help messages if we're on a wide console
|
||||||
columns = compat_get_terminal_size().columns
|
columns = compat_get_terminal_size().columns
|
||||||
max_width = columns if columns else 80
|
max_width = columns if columns else 80
|
||||||
@ -213,16 +212,11 @@ def parseOpts(overrideArguments=None):
|
|||||||
action='store_const', const='::', dest='source_address',
|
action='store_const', const='::', dest='source_address',
|
||||||
help='Make all connections via IPv6 (experimental)',
|
help='Make all connections via IPv6 (experimental)',
|
||||||
)
|
)
|
||||||
network.add_option(
|
|
||||||
'--geo-verification-proxy',
|
|
||||||
dest='geo_verification_proxy', default=None, metavar='URL',
|
|
||||||
help='Use this proxy to verify the IP address for some geo-restricted sites. '
|
|
||||||
'The default proxy specified by --proxy (or none, if the options is not present) is used for the actual downloading. (experimental)'
|
|
||||||
)
|
|
||||||
network.add_option(
|
network.add_option(
|
||||||
'--cn-verification-proxy',
|
'--cn-verification-proxy',
|
||||||
dest='cn_verification_proxy', default=None, metavar='URL',
|
dest='cn_verification_proxy', default=None, metavar='URL',
|
||||||
help=optparse.SUPPRESS_HELP,
|
help='Use this proxy to verify the IP address for some Chinese sites. '
|
||||||
|
'The default proxy specified by --proxy (or none, if the options is not present) is used for the actual downloading. (experimental)'
|
||||||
)
|
)
|
||||||
|
|
||||||
selection = optparse.OptionGroup(parser, 'Video Selection')
|
selection = optparse.OptionGroup(parser, 'Video Selection')
|
||||||
@ -800,33 +794,39 @@ def parseOpts(overrideArguments=None):
|
|||||||
parser.add_option_group(authentication)
|
parser.add_option_group(authentication)
|
||||||
parser.add_option_group(postproc)
|
parser.add_option_group(postproc)
|
||||||
|
|
||||||
if overrideArguments is not None:
|
return parser
|
||||||
opts, args = parser.parse_args(overrideArguments)
|
|
||||||
if opts.verbose:
|
parser = create_parser()
|
||||||
write_string('[debug] Override config: ' + repr(overrideArguments) + '\n')
|
|
||||||
else:
|
|
||||||
def compat_conf(conf):
|
def compat_conf(conf):
|
||||||
if sys.version_info < (3,):
|
if sys.version_info < (3,):
|
||||||
return [a.decode(preferredencoding(), 'replace') for a in conf]
|
return [a.decode(preferredencoding(), 'replace') for a in conf]
|
||||||
return conf
|
return conf
|
||||||
|
|
||||||
command_line_conf = compat_conf(sys.argv[1:])
|
command_line_conf = overrideArguments \
|
||||||
|
if overrideArguments is not None \
|
||||||
if '--ignore-config' in command_line_conf:
|
else compat_conf(sys.argv[1:])
|
||||||
|
if '--ignore-config' in command_line_conf \
|
||||||
|
or (overrideArguments is not None and overrideIgnoreConfig):
|
||||||
system_conf = []
|
system_conf = []
|
||||||
user_conf = []
|
user_conf = []
|
||||||
else:
|
else:
|
||||||
system_conf = _readOptions('/etc/youtube-dl.conf')
|
system_conf = compat_conf(_readOptions('/etc/youtube-dl.conf'))
|
||||||
if '--ignore-config' in system_conf:
|
if '--ignore-config' in system_conf:
|
||||||
user_conf = []
|
user_conf = []
|
||||||
else:
|
else:
|
||||||
user_conf = _readUserConf()
|
user_conf = compat_conf(_readUserConf())
|
||||||
argv = system_conf + user_conf + command_line_conf
|
argv = system_conf + user_conf + command_line_conf
|
||||||
|
|
||||||
opts, args = parser.parse_args(argv)
|
opts, args = parser.parse_args(argv)
|
||||||
if opts.verbose:
|
if opts.verbose:
|
||||||
|
if len(system_conf) > 0:
|
||||||
write_string('[debug] System config: ' + repr(_hide_login_info(system_conf)) + '\n')
|
write_string('[debug] System config: ' + repr(_hide_login_info(system_conf)) + '\n')
|
||||||
|
if len(user_conf) > 0:
|
||||||
write_string('[debug] User config: ' + repr(_hide_login_info(user_conf)) + '\n')
|
write_string('[debug] User config: ' + repr(_hide_login_info(user_conf)) + '\n')
|
||||||
|
if overrideArguments is not None:
|
||||||
|
write_string('[debug] Override config: ' + repr(command_line_conf)) + '\n'
|
||||||
|
else:
|
||||||
write_string('[debug] Command-line args: ' + repr(_hide_login_info(command_line_conf)) + '\n')
|
write_string('[debug] Command-line args: ' + repr(_hide_login_info(command_line_conf)) + '\n')
|
||||||
|
|
||||||
return parser, opts, args
|
return parser, opts, args
|
||||||
|
Loading…
x
Reference in New Issue
Block a user