-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_sources.py
More file actions
67 lines (51 loc) · 2.2 KB
/
Copy pathparse_sources.py
File metadata and controls
67 lines (51 loc) · 2.2 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import re
import json
def parse_source_schema_v1(text_data):
"""
Parses 'Source Information' blocks according to Schema Specification v1.0.
Handles Audit Fields (Date-Entry-Created, etc.) and lists (Tags).
"""
# Capture blocks starting with "# Source Information"
entry_pattern = re.compile(r'# Source Information\n(.*?)(?=\n# Source Information|\Z)', re.DOTALL)
# Capture "**Key:** Value"
field_pattern = re.compile(r'\*\*(.*?):\*\*\s*(.*)')
entries = entry_pattern.findall(text_data)
parsed_data = []
for entry in entries:
record = {}
lines = entry.strip().split('\n')
for line in lines:
match = field_pattern.match(line.strip())
if match:
key = match.group(1).strip()
value = match.group(2).strip()
# --- Type Conversion & Cleaning ---
# Handle Nulls
if value.lower() in ["not visible", "not displayed", "n/a", ""]:
value = None
# Handle Integers
elif key == "Stars":
try:
value = int(value)
except ValueError:
value = None # Keep null if strictly not a number
# Handle Lists (Tags)
elif key == "Tags":
if value:
# Split by comma, strip whitespace, remove empty strings
value = [t.strip() for t in value.split(',') if t.strip()]
else:
value = []
# Store the field
record[key] = value
if record:
parsed_data.append(record)
return parsed_data
# --- Test with the new Schema ---
if __name__ == "__main__":
# Reading the file content (Simulated here with a string)
with open('skills.md', 'r') as f:
content = f.read()
data = parse_source_schema_v1(content)
# print output
print(json.dumps(data, indent=2))