|
53 | 53 | import os |
54 | 54 | import re |
55 | 55 | import sys |
| 56 | +import tomllib |
56 | 57 | from pathlib import Path |
57 | 58 | from typing import Any, Optional |
58 | 59 |
|
@@ -87,60 +88,12 @@ def fail(message: str) -> "NoReturn": # noqa: F821 - py39 compat, comment only |
87 | 88 | # --------------------------------------------------------------------------- |
88 | 89 |
|
89 | 90 |
|
90 | | -def strip_toml_comment(line: str) -> str: |
91 | | - """Drop a trailing # comment, ignoring # characters inside quoted strings.""" |
92 | | - in_quote = "" |
93 | | - for index, char in enumerate(line): |
94 | | - if in_quote: |
95 | | - if char == in_quote: |
96 | | - in_quote = "" |
97 | | - elif char in "\"'": |
98 | | - in_quote = char |
99 | | - elif char == "#": |
100 | | - return line[:index] |
101 | | - return line |
102 | | - |
103 | | - |
104 | 91 | def parse_toml_youtube_section(text: str) -> dict[str, Any]: |
105 | | - """Read the [youtube] table from podguy.toml. |
106 | | -
|
107 | | - Uses tomllib when available (Python 3.11+) and falls back to a minimal |
108 | | - flat-key parser for older interpreters. Only simple keys are supported; |
109 | | - the fallback does not handle [youtube.*] subtables, multiline strings, |
110 | | - or array items containing quote characters. |
111 | | - """ |
| 92 | + """Read the [youtube] table from podguy.toml.""" |
112 | 93 | try: |
113 | | - import tomllib |
114 | | - |
115 | 94 | return dict(tomllib.loads(text).get("youtube", {})) |
116 | | - except ModuleNotFoundError: |
117 | | - pass |
118 | | - |
119 | | - section: dict[str, Any] = {} |
120 | | - in_youtube = False |
121 | | - for raw_line in text.splitlines(): |
122 | | - line = strip_toml_comment(raw_line).strip() |
123 | | - if not line: |
124 | | - continue |
125 | | - if line.startswith("["): |
126 | | - in_youtube = line == "[youtube]" |
127 | | - continue |
128 | | - if not in_youtube or "=" not in line: |
129 | | - continue |
130 | | - key, value = (part.strip() for part in line.split("=", 1)) |
131 | | - if value in ("true", "false"): |
132 | | - section[key] = value == "true" |
133 | | - elif value.startswith("[") and value.endswith("]"): |
134 | | - items = re.findall(r"[\"']([^\"']*)[\"']", value) |
135 | | - section[key] = items |
136 | | - elif len(value) >= 2 and value[0] in "\"'" and value[-1] == value[0]: |
137 | | - section[key] = value[1:-1] |
138 | | - else: |
139 | | - try: |
140 | | - section[key] = int(value) |
141 | | - except ValueError: |
142 | | - section[key] = value |
143 | | - return section |
| 95 | + except tomllib.TOMLDecodeError as error: |
| 96 | + fail(f"error: could not parse profile TOML: {error}") |
144 | 97 |
|
145 | 98 |
|
146 | 99 | def load_profile_defaults(profile_path: Optional[str]) -> dict[str, Any]: |
|
0 commit comments