mirror of
https://github.com/l1ving/youtube-dl
synced 2025-02-14 02:13:26 +08:00
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
|
||||
|
||||
|
||||
def _real_main(argv=None):
|
||||
def _real_main(argv=None, ignoreConfig=True):
|
||||
# Compatibility fixes for Windows
|
||||
if sys.platform == 'win32':
|
||||
# https://github.com/rg3/youtube-dl/issues/820
|
||||
@ -53,7 +53,7 @@ def _real_main(argv=None):
|
||||
|
||||
setproctitle('youtube-dl')
|
||||
|
||||
parser, opts, args = parseOpts(argv)
|
||||
parser, opts, args = parseOpts(argv, ignoreConfig)
|
||||
|
||||
# Set user agent
|
||||
if opts.user_agent is not None:
|
||||
@ -382,8 +382,6 @@ def _real_main(argv=None):
|
||||
'external_downloader_args': external_downloader_args,
|
||||
'postprocessor_args': postprocessor_args,
|
||||
'cn_verification_proxy': opts.cn_verification_proxy,
|
||||
'geo_verification_proxy': opts.geo_verification_proxy,
|
||||
|
||||
}
|
||||
|
||||
with YoutubeDL(ydl_opts) as ydl:
|
||||
@ -417,9 +415,9 @@ def _real_main(argv=None):
|
||||
sys.exit(retcode)
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
def main(argv=None, ignoreConfig=True):
|
||||
try:
|
||||
_real_main(argv)
|
||||
_real_main(argv, ignoreConfig)
|
||||
except DownloadError:
|
||||
sys.exit(1)
|
||||
except SameFileError:
|
||||
|
@ -19,18 +19,16 @@ from .utils import (
|
||||
from .version import __version__
|
||||
|
||||
|
||||
def parseOpts(overrideArguments=None):
|
||||
def parseOpts(overrideArguments=None, overrideIgnoreConfig=True):
|
||||
def _readOptions(filename_bytes, default=[]):
|
||||
try:
|
||||
optionf = open(filename_bytes)
|
||||
except IOError:
|
||||
return default # silently skip if file is not present
|
||||
try:
|
||||
# FIXME: https://github.com/rg3/youtube-dl/commit/dfe5fa49aed02cf36ba9f743b11b0903554b5e56
|
||||
contents = optionf.read()
|
||||
if sys.version_info < (3,):
|
||||
contents = contents.decode(preferredencoding())
|
||||
res = compat_shlex_split(contents, comments=True)
|
||||
res = []
|
||||
for l in optionf:
|
||||
res += compat_shlex_split(l, comments=True)
|
||||
finally:
|
||||
optionf.close()
|
||||
return res
|
||||
@ -102,6 +100,7 @@ def parseOpts(overrideArguments=None):
|
||||
pass
|
||||
return opts
|
||||
|
||||
def create_parser():
|
||||
# No need to wrap help messages if we're on a wide console
|
||||
columns = compat_get_terminal_size().columns
|
||||
max_width = columns if columns else 80
|
||||
@ -213,16 +212,11 @@ def parseOpts(overrideArguments=None):
|
||||
action='store_const', const='::', dest='source_address',
|
||||
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(
|
||||
'--cn-verification-proxy',
|
||||
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')
|
||||
@ -800,33 +794,39 @@ def parseOpts(overrideArguments=None):
|
||||
parser.add_option_group(authentication)
|
||||
parser.add_option_group(postproc)
|
||||
|
||||
if overrideArguments is not None:
|
||||
opts, args = parser.parse_args(overrideArguments)
|
||||
if opts.verbose:
|
||||
write_string('[debug] Override config: ' + repr(overrideArguments) + '\n')
|
||||
else:
|
||||
return parser
|
||||
|
||||
parser = create_parser()
|
||||
|
||||
def compat_conf(conf):
|
||||
if sys.version_info < (3,):
|
||||
return [a.decode(preferredencoding(), 'replace') for a in conf]
|
||||
return conf
|
||||
|
||||
command_line_conf = compat_conf(sys.argv[1:])
|
||||
|
||||
if '--ignore-config' in command_line_conf:
|
||||
command_line_conf = overrideArguments \
|
||||
if overrideArguments is not None \
|
||||
else compat_conf(sys.argv[1:])
|
||||
if '--ignore-config' in command_line_conf \
|
||||
or (overrideArguments is not None and overrideIgnoreConfig):
|
||||
system_conf = []
|
||||
user_conf = []
|
||||
else:
|
||||
system_conf = _readOptions('/etc/youtube-dl.conf')
|
||||
system_conf = compat_conf(_readOptions('/etc/youtube-dl.conf'))
|
||||
if '--ignore-config' in system_conf:
|
||||
user_conf = []
|
||||
else:
|
||||
user_conf = _readUserConf()
|
||||
user_conf = compat_conf(_readUserConf())
|
||||
argv = system_conf + user_conf + command_line_conf
|
||||
|
||||
opts, args = parser.parse_args(argv)
|
||||
if opts.verbose:
|
||||
if len(system_conf) > 0:
|
||||
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')
|
||||
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')
|
||||
|
||||
return parser, opts, args
|
||||
|
Loading…
Reference in New Issue
Block a user