Sorry if I missed this, but I didn’t see any discussion about using a block delimiter, like how Jekyll treats ---
as a delimiter between the YAML header/metadata and the Markdown article content.
PEP 722 and 723 are obviously not the same, but they are both proposing the relatively novel feature of a metadata block embedded in comments. Maybe there’s room here to standardize a format for delimiting metadata from other comments?
Hypothetically:
#!/usr/bin/env python3
# My app!
#
# -*-
# Script Dependencies:
# requests
# click
# -*-
#
# Usage: ...
if __name__ == "__main__":
print("Hello!")
You can still parse that out of the code with a single (absurd) multi-line regex: https://regex101.com/r/ECOTLu/1
import re
block_pattern = re.compile(r"""(?imx)
# Optional shebang
(?:^\#![^\r\n]+$(?:\r|\n|\r\n))?
# Optional blank and comment lines
(?:^[ \t]*.*$(?:\r|\n|\r\n))*?
# The opening delimiter
^\#[ \t]*-\*-[ \t]*$(?:\r|\n|\r\n)
# Header
^\#(?P<indent>[ \t]*)Script[ \t]*Dependencies:[ \t]*$(?:\r|\n|\r\n)
# Dependencies
(?P<deplines>(?:^\#(?P=indent)[ \t]*(?:[A-Z0-9][A-Z0-9._-]*[A-Z0-9]|[A-Z0-9])[ \t]*$(?:\r|\n|\r\n))+)
# Closing delimiter
^\#[ \t]*-\*-[ \t]*$
""")
line_prefix_pattern = re.compile(r"^[ \t]*#[ \t]*")
text = r"""
#!/usr/bin/env python3
# My app!
#
# -*-
# Script Dependencies:
# requests
# click
# -*-
#
# Usage: ...
if __name__ == "__main__":
print("Hello!")
"""
m = pattern.match(text)
if m is not None:
deplines = m.group("deplines")
deps = [prefix_pattern.sub("", line) for line in deplines.splitlines()]
print(deps)
['requests', 'click']
Hopefully you wouldn’t actually use regex to parse this, but it’s meant to show that this block-delimited format is still amenable to usage with simple tools available to all languages.
Edit: I extended this idea in a different post in the PEP 723 thread. Maybe it’s worth drafting a separate PEP?