Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 5, 2021

Conversation

bastimeyer
Copy link
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
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