Improve custom config support (closes #10648)
authorSergey M․ <dstftw@gmail.com>
Sat, 31 Dec 2016 16:41:37 +0000 (23:41 +0700)
committerSergey M․ <dstftw@gmail.com>
Sat, 31 Dec 2016 16:41:37 +0000 (23:41 +0700)
youtube_dl/__init__.py
youtube_dl/options.py

index 1b459a706a4fd464b55c7b70f647106dba1f5597..dfa4ae839055621ab48b89527dbd50eb0c61c06f 100644 (file)
@@ -405,8 +405,7 @@ def _real_main(argv=None):
         'postprocessor_args': postprocessor_args,
         'cn_verification_proxy': opts.cn_verification_proxy,
         'geo_verification_proxy': opts.geo_verification_proxy,
-        'configfile': opts.configfile,
-
+        'config_location': opts.config_location,
     }
 
     with YoutubeDL(ydl_opts) as ydl:
index 4941abe8322d3a65452c7523b9a71cf19f6ebd5f..0eb4924b6fe04582b85053dc65f69e7fa3d42a51 100644 (file)
@@ -179,9 +179,9 @@ def parseOpts(overrideArguments=None):
         'Do not read the user configuration in ~/.config/youtube-dl/config '
         '(%APPDATA%/youtube-dl/config.txt on Windows)')
     general.add_option(
-        '--config-file',
-        dest='configfile', metavar='FILE',
-        help='File to read configuration from.')
+        '--config-location',
+        dest='config_location', metavar='PATH',
+        help='Location of the configuration file; either the path to the config or its containing directory.')
     general.add_option(
         '--flat-playlist',
         action='store_const', dest='extract_flat', const='in_playlist',
@@ -851,30 +851,30 @@ def parseOpts(overrideArguments=None):
         command_line_conf = compat_conf(sys.argv[1:])
         opts, args = parser.parse_args(command_line_conf)
 
-        if '--ignore-config' in command_line_conf:
-            system_conf = []
-            user_conf = []
-        elif '--config-file' in command_line_conf:
-            if not os.path.isfile(opts.configfile):
-                parser.error('Config file {0} not found.'.format(opts.configfile))
-            else:
-                user_conf = _readOptions(opts.configfile)
-                system_conf = []
-
+        system_conf = user_conf = custom_conf = []
+
+        if '--config-location' in command_line_conf:
+            location = compat_expanduser(opts.config_location)
+            if os.path.isdir(location):
+                location = os.path.join(location, 'youtube-dl.conf')
+            if not os.path.exists(location):
+                parser.error('config-location %s does not exist.' % location)
+            custom_conf = _readOptions(location)
+        elif '--ignore-config' in command_line_conf:
+            pass
         else:
             system_conf = _readOptions('/etc/youtube-dl.conf')
-            if '--ignore-config' in system_conf:
-                user_conf = []
-            else:
+            if '--ignore-config' not in system_conf:
                 user_conf = _readUserConf()
 
         argv = system_conf + user_conf + command_line_conf
-
         opts, args = parser.parse_args(argv)
-
         if opts.verbose:
-            write_string('[debug] System config: ' + repr(_hide_login_info(system_conf)) + '\n')
-            write_string('[debug] User config: ' + repr(_hide_login_info(user_conf)) + '\n')
-            write_string('[debug] Command-line args: ' + repr(_hide_login_info(command_line_conf)) + '\n')
+            for conf_label, conf in (
+                    ('System config', system_conf),
+                    ('User config', user_conf),
+                    ('Custom config', custom_conf),
+                    ('Command-line args', command_line_conf)):
+                write_string('[debug] %s: %s\n' % (conf_label, repr(_hide_login_info(conf))))
 
     return parser, opts, args