Skip to content

Commit

Permalink
[outtmpl] Support str.format syntax inside replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
pukkandan committed Apr 24, 2023
1 parent 78fde6e commit ec9311c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ The field names themselves (the part inside the parenthesis) can also have some

1. **Alternatives**: Alternate fields can be specified separated with a `,`. E.g. `%(release_date>%Y,upload_date>%Y|Unknown)s`

1. **Replacement**: A replacement value can be specified using a `&` separator. If the field is *not* empty, this replacement value will be used instead of the actual field content. This is done after alternate fields are considered; thus the replacement is used if *any* of the alternative fields is *not* empty.
1. **Replacement**: A replacement value can be specified using a `&` separator according to the [`str.format` mini-language](https://docs.python.org/3/library/string.html#format-specification-mini-language). If the field is *not* empty, this replacement value will be used instead of the actual field content. This is done after alternate fields are considered; thus the replacement is used if *any* of the alternative fields is *not* empty. E.g. `%(chapters&has chapters|no chapters)s`, `%(title&TITLE={:>20}|NO TITLE)s`

1. **Default**: A literal default value can be specified for when the field is empty using a `|` separator. This overrides `--output-na-placeholder`. E.g. `%(uploader|Unknown)s`

Expand Down
5 changes: 4 additions & 1 deletion test/test_YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,10 @@ def expect_same_infodict(out):
test('%(title&foo|baz)s.bar', 'baz.bar')
test('%(x,id&foo|baz)s.bar', 'foo.bar')
test('%(x,title&foo|baz)s.bar', 'baz.bar')
test('%(title&\n|)s', '\n')
test('%(id&a\nb|)s', ('a\nb', 'a b'))
test('%(id&hi {:>10} {}|)s', 'hi 1234 1234')
test(R'%(id&{0} {}|)s', 'NA')
test(R'%(id&{0.1}|)s', 'NA')

# Laziness
def gen():
Expand Down
18 changes: 16 additions & 2 deletions yt_dlp/YoutubeDL.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import traceback
import unicodedata
import urllib.request
from string import ascii_letters
from string import Formatter, ascii_letters

from .cache import Cache
from .compat import compat_os_name, compat_shlex_quote
Expand Down Expand Up @@ -1237,6 +1237,14 @@ def _dumpjson_default(obj):
return list(obj)
return repr(obj)

class _ReplacementFormatter(Formatter):
def get_field(self, field_name, args, kwargs):
if field_name.isdigit():
return args[0], -1
raise ValueError('Unsupported field')

replacement_formatter = _ReplacementFormatter()

def create_key(outer_mobj):
if not outer_mobj.group('has_key'):
return outer_mobj.group(0)
Expand All @@ -1258,7 +1266,13 @@ def create_key(outer_mobj):
if fmt == 's' and value is not None and key in field_size_compat_map.keys():
fmt = f'0{field_size_compat_map[key]:d}d'

value = default if value is None else value if replacement is None else replacement
if value is None:
value = default
elif replacement is not None:
try:
value = replacement_formatter.format(replacement, value)
except ValueError:
value = na

flags = outer_mobj.group('conversion') or ''
str_fmt = f'{fmt[:-1]}s'
Expand Down

0 comments on commit ec9311c

Please sign in to comment.