]> jfr.im git - yt-dlp.git/commitdiff
Standardized function for creating dict from repeated options
authorpukkandan <redacted>
Sat, 23 Jan 2021 05:57:27 +0000 (11:27 +0530)
committerpukkandan <redacted>
Sat, 23 Jan 2021 11:30:10 +0000 (17:00 +0530)
README.md
youtube_dlc/__init__.py
youtube_dlc/options.py

index 643b53349df5aac9f19566d77c4a5efe4233cbf8..ebfad3781d6c31d985ae45ef297c1775f7e6ddb3 100644 (file)
--- a/README.md
+++ b/README.md
@@ -435,7 +435,7 @@ ## Workarounds:
     --referer URL                    Specify a custom referer, use if the video
                                      access is restricted to one domain
     --add-header FIELD:VALUE         Specify a custom HTTP header and its value,
-                                     separated by a colon ':'. You can use this
+                                     separated by a colon ":". You can use this
                                      option multiple times
     --bidi-workaround                Work around terminals that lack
                                      bidirectional text support. Requires bidiv
@@ -554,8 +554,8 @@ ## Post-Processing Options:
                                      supported: mp4|flv|ogg|webm|mkv|avi)
     --postprocessor-args NAME:ARGS   Give these arguments to the postprocessors.
                                      Specify the postprocessor/executable name
-                                     and the arguments separated by a colon ':'
-                                     to give the argument to only the specified
+                                     and the arguments separated by a colon ":"
+                                     to give the argument to the specified
                                      postprocessor/executable. Supported
                                      postprocessors are: SponSkrub,
                                      ExtractAudio, VideoRemuxer, VideoConvertor,
@@ -569,7 +569,8 @@ ## Post-Processing Options:
                                      to different postprocessors. You can also
                                      specify "PP+EXE:ARGS" to give the arguments
                                      to the specified executable only when being
-                                     used by the specified postprocessor (Alias:
+                                     used by the specified postprocessor. You
+                                     can use this option multiple times (Alias:
                                      --ppa)
     -k, --keep-video                 Keep the intermediate video file on disk
                                      after post-processing
index 90479c6ff147ebd338943893789425cfba5c5196..01b1a347b27f7b096df9b8213d22431ce5edab52 100644 (file)
@@ -70,14 +70,7 @@ def _real_main(argv=None):
         std_headers['Referer'] = opts.referer
 
     # Custom HTTP headers
-    if opts.headers is not None:
-        for h in opts.headers:
-            if ':' not in h:
-                parser.error('wrong header formatting, it should be key:value, not "%s"' % h)
-            key, value = h.split(':', 1)
-            if opts.verbose:
-                write_string('[debug] Adding header from command line option %s:%s\n' % (key, value))
-            std_headers[key] = value
+    std_headers.update(opts.headers)
 
     # Dump user agent
     if opts.dump_user_agent:
@@ -337,21 +330,9 @@ def parse_retries(retries):
     if opts.external_downloader_args:
         external_downloader_args = compat_shlex_split(opts.external_downloader_args)
 
-    postprocessor_args = {}
-    if opts.postprocessor_args is not None:
-        for string in opts.postprocessor_args:
-            mobj = re.match(r'(?P<pp>\w+(?:\+\w+)?):(?P<args>.*)$', string)
-            if mobj is None:
-                if 'sponskrub' not in postprocessor_args:  # for backward compatibility
-                    postprocessor_args['sponskrub'] = []
-                    if opts.verbose:
-                        write_string('[debug] Adding postprocessor args from command line option sponskrub: \n')
-                pp_key, pp_args = 'default', string
-            else:
-                pp_key, pp_args = mobj.group('pp').lower(), mobj.group('args')
-            if opts.verbose:
-                write_string('[debug] Adding postprocessor args from command line option %s: %s\n' % (pp_key, pp_args))
-            postprocessor_args[pp_key] = compat_shlex_split(pp_args)
+    if 'default-compat' in opts.postprocessor_args and 'default' not in opts.postprocessor_args:
+        opts.postprocessor_args.setdefault('sponskrub', [])
+        opts.postprocessor_args['default'] = opts.postprocessor_args['default-compat']
 
     match_filter = (
         None if opts.match_filter is None
@@ -486,7 +467,7 @@ def parse_retries(retries):
         'hls_prefer_native': opts.hls_prefer_native,
         'hls_use_mpegts': opts.hls_use_mpegts,
         'external_downloader_args': external_downloader_args,
-        'postprocessor_args': postprocessor_args,
+        'postprocessor_args': opts.postprocessor_args,
         'cn_verification_proxy': opts.cn_verification_proxy,
         'geo_verification_proxy': opts.geo_verification_proxy,
         'config_location': opts.config_location,
index f1fc9adb2436adba0e3172c6ac6b4b263fe03698..3e7be145106c10037847c38a8411759b7674b96c 100644 (file)
@@ -104,6 +104,20 @@ def _format_option_string(option):
     def _comma_separated_values_options_callback(option, opt_str, value, parser):
         setattr(parser.values, option.dest, value.split(','))
 
+    def _dict_from_multiple_values_options_callback(
+            option, opt_str, value, parser, allowed_keys=r'[\w-]+', delimiter=':', default_key=None, process=None):
+
+        out_dict = getattr(parser.values, option.dest)
+        mobj = re.match(r'(?i)(?P<key>%s)%s(?P<val>.*)$' % (allowed_keys, delimiter), value)
+        if mobj is not None:
+            key, val = mobj.group('key').lower(), mobj.group('val')
+        elif default_key is not None:
+            key, val = default_key, value
+        else:
+            raise optparse.OptionValueError(
+                'wrong %s formatting; it should be %s, not "%s"' % (opt_str, option.metavar, value))
+        out_dict[key] = process(val) if callable(process) else val
+
     # 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
@@ -651,8 +665,9 @@ def _comma_separated_values_options_callback(option, opt_str, value, parser):
     )
     workarounds.add_option(
         '--add-header',
-        metavar='FIELD:VALUE', dest='headers', action='append',
-        help='Specify a custom HTTP header and its value, separated by a colon \':\'. You can use this option multiple times',
+        metavar='FIELD:VALUE', dest='headers', default={}, type='str',
+        action='callback', callback=_dict_from_multiple_values_options_callback,
+        help='Specify a custom HTTP header and its value, separated by a colon ":". You can use this option multiple times',
     )
     workarounds.add_option(
         '--bidi-workaround',
@@ -975,18 +990,21 @@ def _comma_separated_values_options_callback(option, opt_str, value, parser):
         metavar='FORMAT', dest='recodevideo', default=None,
         help='Re-encode the video into another format if re-encoding is necessary (currently supported: mp4|flv|ogg|webm|mkv|avi)')
     postproc.add_option(
-        '--postprocessor-args', '--ppa', metavar='NAME:ARGS',
-        dest='postprocessor_args', action='append',
+        '--postprocessor-args', '--ppa',
+        metavar='NAME:ARGS', dest='postprocessor_args', default={}, type='str',
+        action='callback', callback=_dict_from_multiple_values_options_callback,
+        callback_kwargs={'default_key': 'default-compat', 'allowed_keys': r'\w+(?:\+\w+)?', 'process': compat_shlex_split},
         help=(
             'Give these arguments to the postprocessors. '
             'Specify the postprocessor/executable name and the arguments separated by a colon ":" '
-            'to give the argument to only the specified postprocessor/executable. Supported postprocessors are: '
+            'to give the argument to the specified postprocessor/executable. Supported postprocessors are: '
             'SponSkrub, ExtractAudio, VideoRemuxer, VideoConvertor, EmbedSubtitle, Metadata, Merger, '
             'FixupStretched, FixupM4a, FixupM3u8, SubtitlesConvertor and EmbedThumbnail. '
             'The supported executables are: SponSkrub, FFmpeg, FFprobe, avconf, avprobe and AtomicParsley. '
             'You can use this option multiple times to give different arguments to different postprocessors. '
             'You can also specify "PP+EXE:ARGS" to give the arguments to the specified executable '
-            'only when being used by the specified postprocessor (Alias: --ppa)'))
+            'only when being used by the specified postprocessor. '
+            'You can use this option multiple times (Alias: --ppa)'))
     postproc.add_option(
         '-k', '--keep-video',
         action='store_true', dest='keepvideo', default=False,