2
0

reference-comparison.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import json
  2. import os
  3. import requests
  4. API_KEY = os.getenv("POEDITOR_API_KEY")
  5. PROJECT_ID = os.getenv("POEDITOR_PROJECT_ID")
  6. GITHUB_OUTPUT = os.getenv('GITHUB_OUTPUT')
  7. API_URL = "https://api.poeditor.com/v2/projects/export"
  8. LANGUAGE = "en"
  9. LOCAL_FILE_PATH = "src/PixiEditor/Data/Localization/Languages/en.json"
  10. def fetch_poeditor_json():
  11. """Fetches the latest en.json from POEditor API (remote data)"""
  12. if not API_KEY or not PROJECT_ID:
  13. print("::error::Missing API_KEY or PROJECT_ID in environment variables.")
  14. return None
  15. response = requests.post(API_URL, data={
  16. "api_token": API_KEY,
  17. "id": PROJECT_ID,
  18. "type": "key_value_json",
  19. "language": LANGUAGE
  20. })
  21. if response.status_code == 200:
  22. data = response.json()
  23. if "result" in data and "url" in data["result"]:
  24. download_url = data["result"]["url"]
  25. latest_response = requests.get(download_url)
  26. return latest_response.json() if latest_response.status_code == 200 else None
  27. return None
  28. def load_local_json():
  29. """Loads the local en.json file (authoritative source)"""
  30. try:
  31. with open(LOCAL_FILE_PATH, "r", encoding="utf-8") as file:
  32. return json.load(file)
  33. except FileNotFoundError:
  34. print("::error::Local en.json file not found!")
  35. return {}
  36. def compare_json(local_data, remote_data):
  37. """Compares the local and remote JSON data, detecting added, removed, and modified keys"""
  38. modifications = []
  39. additions = []
  40. deletions = []
  41. # Check for modified keys (key exists in both, but value changed)
  42. for key, local_value in local_data.items():
  43. remote_value = remote_data.get(key)
  44. if remote_value is not None and local_value != remote_value:
  45. modifications.append(f"🔄 {key}: '{remote_value}' → '{local_value}'")
  46. # Check for added keys (exist in local but missing in POEditor)
  47. for key in local_data.keys() - remote_data.keys():
  48. additions.append(f"➕ {key}: '{local_data[key]}'")
  49. # Check for removed keys (exist in POEditor but missing locally)
  50. for key in remote_data.keys() - local_data.keys():
  51. deletions.append(f"❌ {key}: '{remote_data[key]}'")
  52. return modifications, additions, deletions
  53. def print_group(title, items):
  54. """Prints grouped items using GitHub Actions logging format"""
  55. if items:
  56. print(f"::group::{len(items)} {title}")
  57. for item in items:
  58. print(item)
  59. print("::endgroup::")
  60. def main():
  61. remote_json = fetch_poeditor_json()
  62. if remote_json is None:
  63. print("::error::Failed to fetch POEditor en.json")
  64. exit(1)
  65. return
  66. local_json = load_local_json()
  67. modifications, additions, deletions = compare_json(local_json, remote_json)
  68. has_changes = (modifications or additions or deletions)
  69. with open(GITHUB_OUTPUT, "a") as f:
  70. if not has_changes:
  71. f.write(f"HAS_CHANGES=false")
  72. print("✅ No changes detected. Local and remote are in sync.")
  73. else:
  74. f.write(f"HAS_CHANGES=true")
  75. print_group("Key(s) Modified", modifications)
  76. print_group("Key(s) to be Added", additions)
  77. print_group("Key(s) to be Removed", deletions)
  78. if __name__ == "__main__":
  79. main()