-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_metadata.py
More file actions
38 lines (30 loc) · 1.04 KB
/
script_metadata.py
File metadata and controls
38 lines (30 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
**script_metadata.py**: This Python module contains functions for parsing inline script metadata following the PEP 723 Specification.
:Author: W. Aaron (william.aaron@cfa.harvard.edu)
:Last Updated: Jan 16 2026
"""
import os
import re
import tomllib
PEP_723_REGEX = (
r"(?m)^# /// (?P<type>[a-zA-Z0-9-]+)$\s(?P<content>(^#(| .*)$\s)+)^# ///$"
)
def parse_metadata(script: str) -> dict | None:
block_find_iter = re.finditer(PEP_723_REGEX, script)
blocks = {}
for match in block_find_iter:
_type = match.group("type")
_content = "".join(
line[2:] if line.startswith("# ") else line[1:]
for line in match.group("content").splitlines(keepends=True)
)
blocks[_type] = tomllib.loads(_content)
return blocks
def parse_file_metadata(filepath: str):
if os.path.isfile(filepath):
with open(filepath) as f:
content = f.read()
metadata = parse_metadata(content)
return metadata
else:
raise FileNotFoundError(f"Couldn't path to {filepath}")