Thanks to visit codestin.com
Credit goes to github.com

Skip to content

cli: add {time:format} var to --output / --title#3993

Merged
gravyboat merged 3 commits into
streamlink:masterfrom
bastimeyer:cli/time-variable
Sep 5, 2021
Merged

cli: add {time:format} var to --output / --title#3993
gravyboat merged 3 commits into
streamlink:masterfrom
bastimeyer:cli/time-variable

Conversation

@bastimeyer

Copy link
Copy Markdown
Member

This adds support for the {time} / {time:format} variable to --title and --output, which has been a much requested feature in the past. The answer has always been "use your command line shell", which is not that great and Streamlink should support something very basic like that.

The :format part is optional and based on the standard string format specifiers described in PEP3101:
https://www.python.org/dev/peps/pep-3101/#format-specifiers
The logic for this is however much simplified and doesn't support nesting, etc, because we don't need it here.

Format strings on variables where there's no formatting callback registered get ignored. Conversion flags like !r, !s, etc, get ignored as well.
https://www.python.org/dev/peps/pep-3101/#explicit-conversion-flag


  • In order to re-format the variables' values, the Formatter class had to be rewritten, based on string.Formatter's parse() method. It doesn't inherit from string.Formatter because of the formatting simplification. This is the first commit, which also fixes/improves the tests.
  • The second commit then adds support for the parsed format_spec. Instead of adding a second parameter to the Formatter's constructor with formatting functions, the mapping functions could've also received a parameter with the optional formatting string, but that would've prevented caching, so this is the better choice (I think).
  • The third commit adds the {time} var to the CLI module, fixes the default string value of datetime.now() (for file name reasons), and adds documentation. Linking to the python docs (datetime.strftime()) via reST is unfortunately not possible in argparse arguments because of the --help output and man page content, which can't have reST.

Format strings via string.Formatter.parse() and build the formatted
string with a custom method similar to string.Formatter._vformat()
instead of calling str.format_map().
@gravyboat

Copy link
Copy Markdown
Member

Great addition @bastimeyer, thanks!

@gravyboat gravyboat merged commit 356367a into streamlink:master Sep 5, 2021
@bastimeyer bastimeyer deleted the cli/time-variable branch September 5, 2021 05:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants