161 lines
5.0 KiB
Python
161 lines
5.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Generate bicorder.txt from bicorder.json
|
|
"""
|
|
|
|
import json
|
|
import argparse
|
|
import sys
|
|
|
|
|
|
def center_text(text, width=80):
|
|
"""Center text within a given width"""
|
|
return text.center(width)
|
|
|
|
|
|
def format_gradient_bar(value):
|
|
"""
|
|
Format the gradient bar based on value.
|
|
If value is None, show all bars: [|||||||||]
|
|
If value is 0-8, replace that position with #: [|#||||||||]
|
|
"""
|
|
if value is None:
|
|
return "[|||||||||]"
|
|
|
|
# Ensure value is in valid range
|
|
if not isinstance(value, int) or value < 0 or value > 8:
|
|
return "[|||||||||]"
|
|
|
|
bars = list("|||||||||")
|
|
bars[value] = "#"
|
|
return "[" + "".join(bars) + "]"
|
|
|
|
|
|
def format_gradient_line(term_left, term_right, value, left_width=18, right_width=18):
|
|
"""
|
|
Format a gradient line with proper spacing.
|
|
Example: " explicit < [|||||||||] > implicit "
|
|
"""
|
|
bar = format_gradient_bar(value)
|
|
# Right-align the left term, add the bar, then left-align the right term
|
|
line = f"{term_left.rjust(left_width)} < {bar} > {term_right.ljust(right_width)}"
|
|
return center_text(line)
|
|
|
|
|
|
def format_metadata_field(field_value, field_name):
|
|
"""
|
|
Format metadata field - show value if provided, otherwise show field name in brackets
|
|
"""
|
|
if field_value is None or field_value == "":
|
|
return f"[{field_name}]"
|
|
return str(field_value)
|
|
|
|
|
|
def generate_bicorder_text(json_data):
|
|
"""Generate the formatted bicorder text from JSON data"""
|
|
lines = []
|
|
|
|
# First pass: calculate maximum widths for left and right terms
|
|
max_left_width = 0
|
|
max_right_width = 0
|
|
|
|
# Check diagnostic gradients
|
|
for diagnostic_set in json_data.get("diagnostic", []):
|
|
for gradient in diagnostic_set.get("gradients", []):
|
|
term_left = gradient.get("term_left", "")
|
|
term_right = gradient.get("term_right", "")
|
|
max_left_width = max(max_left_width, len(term_left))
|
|
max_right_width = max(max_right_width, len(term_right))
|
|
|
|
# Check analysis items
|
|
for analysis_item in json_data.get("analysis", []):
|
|
term_left = analysis_item.get("term_left", "")
|
|
term_right = analysis_item.get("term_right", "")
|
|
max_left_width = max(max_left_width, len(term_left))
|
|
max_right_width = max(max_right_width, len(term_right))
|
|
|
|
# Header
|
|
lines.append(center_text("Protocol"))
|
|
lines.append(center_text("BICORDER"))
|
|
lines.append("")
|
|
|
|
# Metadata section
|
|
metadata = json_data.get("metadata", {})
|
|
lines.append(center_text(format_metadata_field(metadata.get("protocol"), "Protocol")))
|
|
lines.append(center_text(format_metadata_field(metadata.get("analyst"), "Analyst")))
|
|
lines.append(center_text(format_metadata_field(metadata.get("standpoint"), "Standpoint")))
|
|
lines.append(center_text(format_metadata_field(metadata.get("timestamp"), "Timestamp")))
|
|
lines.append("")
|
|
|
|
# Diagnostic sections
|
|
for diagnostic_set in json_data.get("diagnostic", []):
|
|
set_name = diagnostic_set.get("set_name", "").upper()
|
|
lines.append(center_text(set_name))
|
|
|
|
for gradient in diagnostic_set.get("gradients", []):
|
|
term_left = gradient.get("term_left", "")
|
|
term_right = gradient.get("term_right", "")
|
|
value = gradient.get("value")
|
|
|
|
lines.append(format_gradient_line(term_left, term_right, value, max_left_width, max_right_width))
|
|
|
|
lines.append("")
|
|
|
|
# Analysis section
|
|
lines.append(center_text("ANALYSIS"))
|
|
for analysis_item in json_data.get("analysis", []):
|
|
term_left = analysis_item.get("term_left", "")
|
|
term_right = analysis_item.get("term_right", "")
|
|
value = analysis_item.get("value")
|
|
|
|
lines.append(format_gradient_line(term_left, term_right, value, max_left_width, max_right_width))
|
|
|
|
lines.append("")
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
def main():
|
|
"""Main function to read JSON and generate text output"""
|
|
# Set up argument parser
|
|
parser = argparse.ArgumentParser(
|
|
description="Generate formatted bicorder text from JSON input"
|
|
)
|
|
parser.add_argument(
|
|
"input_json",
|
|
help="Path to input JSON file"
|
|
)
|
|
parser.add_argument(
|
|
"output_txt",
|
|
help="Path to output TXT file"
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
# Read the JSON file
|
|
try:
|
|
with open(args.input_json, "r") as f:
|
|
data = json.load(f)
|
|
except FileNotFoundError:
|
|
print(f"Error: Input file '{args.input_json}' not found.", file=sys.stderr)
|
|
sys.exit(1)
|
|
except json.JSONDecodeError as e:
|
|
print(f"Error: Invalid JSON in '{args.input_json}': {e}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
# Generate the formatted text
|
|
output = generate_bicorder_text(data)
|
|
|
|
# Write to output file
|
|
try:
|
|
with open(args.output_txt, "w") as f:
|
|
f.write(output)
|
|
print(f"Successfully generated '{args.output_txt}'")
|
|
except IOError as e:
|
|
print(f"Error: Could not write to '{args.output_txt}': {e}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|