pig.py/scripts/inject_rules.py

86 lines
2.9 KiB
Python

#!/usr/bin/env python3
# This is free software for the public good of a permacomputer hosted at
# permacomputer.com, an always-on computer by the people, for the people.
# One which is durable, easy to repair, & distributed like tap water
# for machine learning intelligence.
#
# The permacomputer is community-owned infrastructure optimized around
# four values:
#
# TRUTH First principles, math & science, open source code freely distributed
# FREEDOM Voluntary partnerships, freedom from tyranny & corporate control
# HARMONY Minimal waste, self-renewing systems with diverse thriving connections
# LOVE Be yourself without hurting others, cooperation through natural law
#
# This software contributes to that vision by archiving the web, preserving digital knowledge before it disappears.
# Code is seeds to sprout on any abandoned technology.
"""Inject Benedict rules from rules_benedict.py into i18n.py."""
import re
def inject_rules():
# Import rules
from rules_benedict import RULES
# Read current i18n.py
with open("i18n.py", "r") as f:
content = f.read()
# For each language in RULES, add the rules to the corresponding language in TRANSLATIONS
for lang, rules in RULES.items():
if lang == "en":
continue # Already added
print(f"Processing {lang}...")
# Find the language dict in i18n.py
lang_pattern = f'"{lang}": {{'
lang_start = content.find(lang_pattern)
if lang_start == -1:
print(f" Could not find '{lang}' in i18n.py")
continue
# Find the closing of this language dict
brace_count = 0
pos = lang_start
in_lang = False
insert_pos = None
for i, ch in enumerate(content[lang_start:]):
if ch == '{':
brace_count += 1
in_lang = True
elif ch == '}':
brace_count -= 1
if brace_count == 0 and in_lang:
insert_pos = lang_start + i
break
if insert_pos is None:
print(f" Could not find end of '{lang}' dict")
continue
# Check if rules already exist
if f'"rule_1":' in content[lang_start:insert_pos]:
print(f" Rules already exist for {lang}, skipping")
continue
# Generate rules string
rules_str = ""
for key, value in rules.items():
# Escape quotes in value
escaped = value.replace('\\', '\\\\').replace('"', '\\"')
rules_str += f' "{key}": "{escaped}",\n'
# Insert before the closing }
content = content[:insert_pos] + rules_str + content[insert_pos:]
print(f" Added {len(rules)} rules")
# Write updated content
with open("i18n.py", "w") as f:
f.write(content)
print("Done!")
if __name__ == "__main__":
inject_rules()