[outtmpl] Alternate form for D and fix suffix's case

Fixes: https://github.com/yt-dlp/yt-dlp/issues/2085#issuecomment-1002247689, https://github.com/yt-dlp/yt-dlp/pull/2132/files#r775729811
This commit is contained in:
pukkandan 2021-12-30 08:43:40 +05:30
parent 2c539d493a
commit abbeeebc4c
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698
4 changed files with 12 additions and 6 deletions

View File

@ -1090,7 +1090,7 @@ # OUTPUT TEMPLATE
1. **Default**: A literal default value can be specified for when the field is empty using a `|` separator. This overrides `--output-na-template`. Eg: `%(uploader|Unknown)s` 1. **Default**: A literal default value can be specified for when the field is empty using a `|` separator. This overrides `--output-na-template`. Eg: `%(uploader|Unknown)s`
1. **More Conversions**: In addition to the normal format types `diouxXeEfFgGcrs`, `B`, `j`, `l`, `q`, `D`, `S` can be used for converting to **B**ytes, **j**son (flag `#` for pretty-printing), a comma separated **l**ist (flag `#` for `\n` newline-separated), a string **q**uoted for the terminal (flag `#` to split a list into different arguments), to add **D**ecimal suffixes (Eg: 10M), and to **S**anitize as filename (flag `#` for restricted), respectively 1. **More Conversions**: In addition to the normal format types `diouxXeEfFgGcrs`, `B`, `j`, `l`, `q`, `D`, `S` can be used for converting to **B**ytes, **j**son (flag `#` for pretty-printing), a comma separated **l**ist (flag `#` for `\n` newline-separated), a string **q**uoted for the terminal (flag `#` to split a list into different arguments), to add **D**ecimal suffixes (Eg: 10M) (flag `#` to use 1024 as factor), and to **S**anitize as filename (flag `#` for restricted), respectively
1. **Unicode normalization**: The format type `U` can be used for NFC [unicode normalization](https://docs.python.org/3/library/unicodedata.html#unicodedata.normalize). The alternate form flag (`#`) changes the normalization to NFD and the conversion flag `+` can be used for NFKC/NFKD compatibility equivalence normalization. Eg: `%(title)+.100U` is NFKC 1. **Unicode normalization**: The format type `U` can be used for NFC [unicode normalization](https://docs.python.org/3/library/unicodedata.html#unicodedata.normalize). The alternate form flag (`#`) changes the normalization to NFD and the conversion flag `+` can be used for NFKC/NFKD compatibility equivalence normalization. Eg: `%(title)+.100U` is NFKC

View File

@ -645,6 +645,7 @@ def test_add_extra_info(self):
'ext': 'mp4', 'ext': 'mp4',
'width': None, 'width': None,
'height': 1080, 'height': 1080,
'filesize': 1024,
'title1': '$PATH', 'title1': '$PATH',
'title2': '%PATH%', 'title2': '%PATH%',
'title3': 'foo/bar\\test', 'title3': 'foo/bar\\test',
@ -778,8 +779,9 @@ def expect_same_infodict(out):
test('%(title5)#U', 'a\u0301e\u0301i\u0301 𝐀') test('%(title5)#U', 'a\u0301e\u0301i\u0301 𝐀')
test('%(title5)+U', 'áéí A') test('%(title5)+U', 'áéí A')
test('%(title5)+#U', 'a\u0301e\u0301i\u0301 A') test('%(title5)+#U', 'a\u0301e\u0301i\u0301 A')
test('%(height)D', '1K') test('%(height)D', '1k')
test('%(height)5.2D', ' 1.08K') test('%(filesize)#D', '1Ki')
test('%(height)5.2D', ' 1.08k')
test('%(title4)#S', 'foo_bar_test') test('%(title4)#S', 'foo_bar_test')
test('%(title4).10S', ('foo \'bar\' ', 'foo \'bar\'' + ('#' if compat_os_name == 'nt' else ' '))) test('%(title4).10S', ('foo \'bar\' ', 'foo \'bar\'' + ('#' if compat_os_name == 'nt' else ' ')))
if compat_os_name == 'nt': if compat_os_name == 'nt':

View File

@ -1166,7 +1166,9 @@ def create_key(outer_mobj):
'NF%s%s' % ('K' if '+' in flags else '', 'D' if '#' in flags else 'C'), 'NF%s%s' % ('K' if '+' in flags else '', 'D' if '#' in flags else 'C'),
value), str_fmt value), str_fmt
elif fmt[-1] == 'D': # decimal suffix elif fmt[-1] == 'D': # decimal suffix
value, fmt = format_decimal_suffix(value, f'%{fmt[:-1]}f%s' if fmt[:-1] else '%d%s'), 's' num_fmt, fmt = fmt[:-1].replace('#', ''), 's'
value = format_decimal_suffix(value, f'%{num_fmt}f%s' if num_fmt else '%d%s',
factor=1024 if '#' in flags else 1000)
elif fmt[-1] == 'S': # filename sanitization elif fmt[-1] == 'S': # filename sanitization
value, fmt = filename_sanitizer(initial_field, value, restricted='#' in flags), str_fmt value, fmt = filename_sanitizer(initial_field, value, restricted='#' in flags), str_fmt
elif fmt[-1] == 'c': elif fmt[-1] == 'c':

View File

@ -2116,9 +2116,11 @@ def format_decimal_suffix(num, fmt='%d%s', *, factor=1000):
if num is None: if num is None:
return None return None
exponent = 0 if num == 0 else int(math.log(num, factor)) exponent = 0 if num == 0 else int(math.log(num, factor))
suffix = ['', *'KMGTPEZY'][exponent] suffix = ['', *'kMGTPEZY'][exponent]
if factor == 1024:
suffix = {'k': 'Ki', '': ''}.get(suffix, f'{suffix}i')
converted = num / (factor ** exponent) converted = num / (factor ** exponent)
return fmt % (converted, f'{suffix}i' if suffix and factor == 1024 else suffix) return fmt % (converted, suffix)
def format_bytes(bytes): def format_bytes(bytes):