MCPcopy Create free account
hub / github.com/ParzivalHack/PySpector / _assemble_module_json

Function _assemble_module_json

src/pyspector/ast_cache.py:89–123  ·  view source on GitHub ↗

Build the Module JSON wrapper around pre-serialized body/type_ignore fragments. Pre-conditions (caller must ensure): Every string in body_parts / type_ignore_parts is valid JSON produced by encode_node(). Values are embedded verbatim — not re-serialized or escaped. Mirrors

(
    body_parts: List[str],
    type_ignore_parts: List[str],
)

Source from the content-addressed store, hash-verified

87
88
89def _assemble_module_json(
90 body_parts: List[str],
91 type_ignore_parts: List[str],
92) -> str:
93 """
94 Build the Module JSON wrapper around pre-serialized body/type_ignore fragments.
95
96 Pre-conditions (caller must ensure):
97 Every string in body_parts / type_ignore_parts is valid JSON produced by
98 encode_node(). Values are embedded verbatim — not re-serialized or escaped.
99
100 Mirrors AstEncoder's field/children split:
101 - non-empty AST-node list → placed under "children"
102 - empty list → placed under "fields" as []
103 """
104 ch_items: List[str] = []
105 fi_items: List[str] = []
106
107 if body_parts:
108 ch_items.append('"body": [' + ",".join(body_parts) + "]")
109 else:
110 fi_items.append('"body": []')
111
112 if type_ignore_parts:
113 ch_items.append('"type_ignores": [' + ",".join(type_ignore_parts) + "]")
114 else:
115 fi_items.append('"type_ignores": []')
116
117 ch_json = "{" + ", ".join(ch_items) + "}"
118 fi_json = "{" + ", ".join(fi_items) + "}"
119 return (
120 '{"node_type": "Module", "lineno": -1, "col_offset": -1, '
121 '"children": ' + ch_json + ', '
122 '"fields": ' + fi_json + "}"
123 )
124
125
126# ── Incremental JSON construction ────────────────────────────────────────────

Calls

no outgoing calls