-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathfix_commas.py
More file actions
171 lines (142 loc) · 5.1 KB
/
Copy pathfix_commas.py
File metadata and controls
171 lines (142 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import polib
import re
import sys
import os
import glob
code_patterns = [
# Basic patterns
r"\([^)]*،[^)]*\)", # foo(3.0، -4.5)
r"\[[^]]*،[^]]*\]", # ['a'، 'b'، 'c']
r"\{[^}]*،[^}]*\}", # {'Sjoerd': 4127، 'Jack': 4098}
# Function and class definitions
r"def\s+\w+\s*\([^)]*،[^)]*\)", # def __init__(self، name)
r"class\s+\w+\s*\([^)]*،[^)]*\)", # class AsyncZip(threading.Thread، file)
r"__init__\([^)]*،[^)]*\)", # __init__(self، realpart، imagpart)
# Method and function calls
r"\w+\([^)]*،[^)]*\)", # round(Decimal('0.70')، 2)
r"__import__\([^،]*(?:،\s*[^،)]*){0,4}\)", # __import__('spam.ham'، globals()، locals())
r"(?:globals|locals)\(\)،\s*(?:globals|locals)\(\)", # globals()، locals()
# Variable assignments and operations
r"for\s+\w+،\s*\w+\s+in\s+", # for a، b in table.items()
r"print\([^)]*،[^)]*\)", # print(a، end=' ')
# Variable and attribute access
r"\w+\.\w+،\s*\w+\.\w+", # x.r، x.i
r"(?:\w+\([^)]*\)|'[^']*'|\"[^\"]*\"|[)\]])،\s*\w+", # os.open('mydata.db'، 'rb')
# Dictionary operations
r"{\s*'[^']*':\s*\w+،", # {'primary': یک، 'secondary': دو}
r"for\s+\w+،\s*\w+\s+in\s+\w+\.items\(\)", # for a، b in table.items()
r">>>.*،.*", # >>> a، b
]
def is_code_pattern(text: str) -> bool:
"""
Check if the text matches common code patterns
where virgules should be converted to commas.
"""
return any(re.search(pattern, text) for pattern in code_patterns)
def fix_virgules_in_code(entry):
"""
Fix virgules in code sections while preserving them in regular text.
Returns True if any changes were made.
"""
modified = False
# Check if original message is complete Python code
if is_complete_python_code(entry.msgid):
if entry.msgid != entry.msgstr and entry.msgstr != "":
entry.msgstr = entry.msgid
modified = True
return modified
# If the text appears to be code, replace virgules
if is_code_pattern(entry.msgstr):
new_text = entry.msgstr
# Handle matches
def replace_virgules(match):
return match.group().replace("،", ",")
# Use the same patterns from is_code_pattern
for pattern in code_patterns:
new_text = re.sub(pattern, replace_virgules, new_text)
if new_text != entry.msgstr:
entry.msgstr = new_text
modified = True
return modified
def is_complete_python_code(text: str) -> bool:
"""
Check if the text is complete, runnable Python code.
"""
if (
len(text.split()) == 1
and "_" not in text
and "(" not in text
and ")" not in text
and "," not in text
):
return False
try:
compile(text, "<string>", "exec")
return True
except SyntaxError:
pass
try:
# remove >>> and ... prompts
text_without_prompts = re.sub(r"^>>> |^\.\.\. ", "", text, flags=re.MULTILINE)
compile(text_without_prompts, "<string>", "exec")
return True
except SyntaxError:
pass
try:
# remove >>> and ... prompts except the last one (might be the output)
text_without_prompts = re.sub(r"^>>> |^\.\.\. ", "", text, flags=re.MULTILINE)
text_without_prompts = re.sub(
r"^>>> |^\.\.\. ", "", text_without_prompts, count=1, flags=re.MULTILINE
)
compile(text_without_prompts, "<string>", "exec")
return True
except SyntaxError:
pass
def process_po_file(po_file):
"""Process a single PO file."""
try:
po = polib.pofile(po_file)
fixed_entries = 0
for entry in po:
if fix_virgules_in_code(entry):
fixed_entries += 1
if fixed_entries > 0:
po.save()
print(f"Fixed {fixed_entries} entries in {po_file}")
except Exception as e:
print(f"Error processing {po_file}: {e}")
def collect_po_files(path):
"""
Given a path (wildcard, file, or directory), return a list of .po files.
"""
files = []
if os.path.isfile(path) and path.endswith(".po"):
files.append(path)
elif os.path.isdir(path):
for root, _, filenames in os.walk(path):
for filename in filenames:
if filename.endswith(".po"):
files.append(os.path.join(root, filename))
else:
# Handle wildcards
for p in glob.glob(path, recursive=True):
if os.path.isfile(p) and p.endswith(".po"):
files.append(p)
return files
def main(paths):
processed_files = set()
for path in paths:
for po_file in collect_po_files(path):
if po_file not in processed_files:
process_po_file(po_file)
processed_files.add(po_file)
if not processed_files:
print("No PO files found.")
if __name__ == "__main__":
if len(sys.argv) < 2:
print(
"Usage: python fix_code_virgules.py "
"<po-file|directory|glob-pattern> [additional paths...]"
)
sys.exit(1)
main(sys.argv[1:])