MCPcopy
hub / github.com/qazbnm456/awesome-web-security / yaml_decode_scalar

Function yaml_decode_scalar

scripts/generate.py:38–80  ·  view source on GitHub ↗

Decode a YAML scalar (limited subset: null/bool/int/float/str).

(s: str)

Source from the content-addressed store, hash-verified

36
37
38def yaml_decode_scalar(s: str):
39 """Decode a YAML scalar (limited subset: null/bool/int/float/str)."""
40 t = s.strip()
41 if t == "" or t == "~" or t == "null":
42 return None
43 if t == "true":
44 return True
45 if t == "false":
46 return False
47 if (t.startswith('"') and t.endswith('"')) or (t.startswith("'") and t.endswith("'")):
48 # JSON-compatible double-quote unescape; single-quoted just strips quotes
49 if t.startswith('"'):
50 return json.loads(t)
51 return t[1:-1].replace("''", "'")
52 # try int
53 if re.match(r"^-?\d+$", t):
54 return int(t)
55 # try float
56 if re.match(r"^-?\d+\.\d+$", t):
57 return float(t)
58 # flow list: [a, b, c]
59 if t.startswith("[") and t.endswith("]"):
60 inner = t[1:-1].strip()
61 if not inner:
62 return []
63 # naive split on commas not inside brackets/quotes
64 out = []
65 depth = 0
66 cur = ""
67 for c in inner:
68 if c in "[\"'":
69 depth += 1
70 elif c in "]\"'":
71 depth -= 1
72 if c == "," and depth == 0:
73 out.append(yaml_decode_scalar(cur))
74 cur = ""
75 else:
76 cur += c
77 if cur.strip():
78 out.append(yaml_decode_scalar(cur))
79 return out
80 return t
81
82
83def parse_yaml(text: str):

Callers 1

parse_yamlFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected