Skip to content

Commit

Permalink
♻️ (hooks): Refactor check xcstrings for unusual terminators
Browse files Browse the repository at this point in the history
  • Loading branch information
ladislas committed Feb 16, 2024
1 parent 3ff2088 commit 61cc732
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 22 deletions.
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ repos:
- id: check_xcstrings_unusual_terminators
name: Check .xcstrings files for unusual terminators
description: This hook checks .xcstrings files for unusual terminators
entry: Tools/Hooks/check_xcstrings_unusual_terminators.sh
language: script
entry: python3 Tools/Hooks/check_xcstrings_unusual_terminators.py
language: python
additional_dependencies: ["pygments"]
files: '.*\.xcstrings'

- id: check_yaml_definitions_professions
Expand Down
74 changes: 74 additions & 0 deletions Tools/Hooks/check_xcstrings_unusual_terminators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/python3
"""Check xstrings for unusal terminators"""

# Leka - LekaOS
# Copyright 2020 APF France handicap
# SPDX-License-Identifier: Apache-2.0

import sys
import json
from pygments import highlight
from pygments.lexers.data import JsonLexer
from pygments.formatters.terminal import TerminalFormatter


CHARACTER_TO_SEARCH = "\u2028"

#
# Mark: - Functions
#


def check_for_unusual_terminators(file_path):
"""Check the given file for unusual terminators."""
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)

wrong_entries = []

# Navigate through the JSON structure
for key, value in data["strings"].items():
for _, localizations in value["localizations"].items():
localized_string = localizations["stringUnit"]["value"]
if CHARACTER_TO_SEARCH in localized_string:
wrong_entries.append((key, value))

return wrong_entries


#
# Mark: - Main
#


def main():
"""Main function"""
# Check if a file was specified
if len(sys.argv) > 1:
profession_definitions_files = sys.argv[1:]
else:
print("❌ No file specified")
sys.exit(1)

must_fail = False

for file in profession_definitions_files:
print(f"🔍 Checking file {file} for unusual terminators...")
wrong_entries = check_for_unusual_terminators(file)
if wrong_entries:
must_fail = True
print(
f"❌ Unusual terminators 'U+{ord(CHARACTER_TO_SEARCH):04X}' found in {file}"
)
for key, data in wrong_entries:
data = json.dumps(data, indent=4)
print(highlight(f'"{key}": {data}', JsonLexer(), TerminalFormatter()))

if must_fail:
return 1

return 0


if __name__ == "__main__":
sys.exit(main())
20 changes: 0 additions & 20 deletions Tools/Hooks/check_xcstrings_unusual_terminators.sh

This file was deleted.

0 comments on commit 61cc732

Please sign in to comment.