Spaces:
Sleeping
Sleeping
import os | |
import json | |
def extract_label_prefix(file_name): | |
label_prefix = os.path.splitext(os.path.basename(file_name))[0] | |
return label_prefix.replace(" ", "_").replace("-", "_") | |
def strip_keys(d): | |
if isinstance(d, dict): | |
return {k.strip(): strip_keys(v) for k, v in d.items()} | |
elif isinstance(d, list): | |
return [strip_keys(i) for i in d] | |
else: | |
return d | |
def format_json(json_data): | |
formatted_json = "{\n \"nodes\": [\n" | |
for node in json_data['nodes']: | |
formatted_json += f" {json.dumps(node)},\n" | |
formatted_json = formatted_json.rstrip(',\n') + "\n ],\n \"edges\": [\n" | |
for edge in json_data['edges']: | |
formatted_json += f" {json.dumps(edge)},\n" | |
formatted_json = formatted_json.rstrip(',\n') + "\n ]\n}" | |
return formatted_json | |
def validate_json(json_data): | |
if not isinstance(json_data, dict) or 'nodes' not in json_data or 'edges' not in json_data: | |
raise ValueError("JSON must contain 'nodes' and 'edges' keys") | |
if not isinstance(json_data['nodes'], list) or not isinstance(json_data['edges'], list): | |
raise ValueError("'nodes' and 'edges' must be lists") | |
for node in json_data['nodes']: | |
if 'id' not in node or 'label' not in node: | |
raise ValueError("Each node must have 'id' and 'label' properties") | |
for edge in json_data['edges']: | |
if 'source' not in edge or 'target' not in edge or 'type' not in edge: | |
raise ValueError("Each edge must have 'source', 'target', and 'type' properties") | |
if edge['type'] != "->": | |
raise ValueError("Edge type must be '->' strictly") |