Skip to content

Commit 5f35b69

Browse files
committed
Add UDC class for parsing UDC files with section handling
1 parent dcc2f9c commit 5f35b69

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

not1mm/lib/parse_udc.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import logging
2+
3+
if __name__ == "__main__":
4+
print("I'm not the program you are looking for.")
5+
6+
logger = logging.getLogger("parse_udc")
7+
8+
9+
class UDC:
10+
"""Parse UDC files"""
11+
12+
# Sections
13+
# [Author]
14+
# [File]
15+
# [Contest]
16+
17+
def __init__(self) -> None: ...
18+
19+
def parse_udc(self, path: str) -> dict[str, dict[str, str]]:
20+
"""Parse a .udc file at `path` and return a dict of sections.
21+
22+
Each section is a dict of key -> value (both stripped). Lines without
23+
an '=' are ignored. Empty values are preserved as empty strings.
24+
"""
25+
result: dict[str, dict[str, str]] = {}
26+
current_section: str | None = None
27+
28+
with open(path, "r", encoding="utf-8") as fh:
29+
for raw in fh:
30+
line = raw.strip()
31+
if not line:
32+
continue
33+
if line.startswith("[") and line.endswith("]"):
34+
current_section = line[1:-1].strip()
35+
if current_section not in result:
36+
result[current_section] = {}
37+
continue
38+
if current_section is None:
39+
# skip any lines before first section
40+
continue
41+
42+
# split on first '='; keys or values may contain '=' rarely
43+
if "=" not in line:
44+
continue
45+
key, _, value = line.partition("=")
46+
key = key.strip()
47+
value = value.strip()
48+
result[current_section][key] = value
49+
50+
return result

0 commit comments

Comments
 (0)