Formatter
Formats and sorts WhatsMyName JSON per schema (see API Reference).
Basic Usage
import json
from pathlib import Path
from naminter import WMNFormatter
# Load data and schema
with open("wmn-data.json", encoding="utf-8") as f:
data = json.load(f)
with open("wmn-schema.json", encoding="utf-8") as f:
schema = json.load(f)
# Read original content for comparison
input_path = Path("wmn-data.json")
original_content = input_path.read_text(encoding="utf-8")
# Create formatter with schema
formatter = WMNFormatter(schema)
# Format data (data is not modified)
formatted_content = formatter.format_dataset(data)
# Compare and write if changed
if original_content != formatted_content:
output_path = Path("wmn-data-formatted.json")
output_path.write_text(formatted_content, encoding="utf-8")
print("File was formatted and saved")
else:
print("File was already properly formatted")
CLI Usage
The formatter is also available via the CLI:
naminter format \
--local-schema schema.json \
--local-data data.json \
--output formatted-data.json
API Reference
naminter.core.formatter.WMNFormatter
Formatter for WhatsMyName JSON data.
Initialize formatter with schema.
| PARAMETER | DESCRIPTION |
|---|---|
schema
|
JSON Schema for the dataset.
TYPE:
|
format_schema
Return formatted schema JSON string.
| RETURNS | DESCRIPTION |
|---|---|
str
|
Schema serialized as a formatted JSON string.
TYPE:
|
format_dataset
Return formatted data JSON string per schema.
| PARAMETER | DESCRIPTION |
|---|---|
data
|
WMN dataset to format. This will not be modified.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Formatted JSON string.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
WMNFormatError
|
If data is not JSON-serializable or invalid. |
WMNSchemaError
|
If site schema properties are not found or invalid. |