Skip to content

Commit 78acd8e

Browse files
StanFromIrelandAA-Turnertomasr8
authored
gh-138286: Run ruff on Tools/i18n (#138287)
Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Tomas R. <[email protected]>
1 parent 15e37ea commit 78acd8e

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ repos:
1414
name: Run Ruff (lint) on Tools/build/
1515
args: [--exit-non-zero-on-fix, --config=Tools/build/.ruff.toml]
1616
files: ^Tools/build/
17+
- id: ruff
18+
name: Run Ruff (lint) on Tools/i18n/
19+
args: [--exit-non-zero-on-fix, --config=Tools/i18n/.ruff.toml]
20+
files: ^Tools/i18n/
1721
- id: ruff
1822
name: Run Ruff (lint) on Argument Clinic
1923
args: [--exit-non-zero-on-fix, --config=Tools/clinic/.ruff.toml]

Tools/i18n/.ruff.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extend = "../../.ruff.toml" # Inherit the project-wide settings
2+
3+
target-version = "py313"
4+
5+
[lint]
6+
select = [
7+
"F", # pyflakes
8+
"I", # isort
9+
"UP", # pyupgrade
10+
]

Tools/i18n/makelocalealias.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99
import locale
1010
import sys
11+
1112
_locale = locale
1213

1314
# Location of the X11 alias file.
@@ -100,16 +101,15 @@ def parse_glibc_supported(filename):
100101
def pprint(data):
101102
items = sorted(data.items())
102103
for k, v in items:
103-
print(' %-40s%a,' % ('%a:' % k, v))
104+
print(f" {k!a:<40}{v!a},")
104105

105106
def print_differences(data, olddata):
106107
items = sorted(olddata.items())
107108
for k, v in items:
108109
if k not in data:
109-
print('# removed %a' % k)
110+
print(f'# removed {k!a}')
110111
elif olddata[k] != data[k]:
111-
print('# updated %a -> %a to %a' % \
112-
(k, olddata[k], data[k]))
112+
print(f'# updated {k!a} -> {olddata[k]!a} to {data[k]!a}')
113113
# Additions are not mentioned
114114

115115
def optimize(data):
@@ -132,7 +132,7 @@ def check(data):
132132
errors = 0
133133
for k, v in data.items():
134134
if locale.normalize(k) != v:
135-
print('ERROR: %a -> %a != %a' % (k, locale.normalize(k), v),
135+
print(f'ERROR: {k!a} -> {locale.normalize(k)!a} != {v!a}',
136136
file=sys.stderr)
137137
errors += 1
138138
return errors
@@ -142,10 +142,10 @@ def check(data):
142142
parser = argparse.ArgumentParser()
143143
parser.add_argument('--locale-alias', default=LOCALE_ALIAS,
144144
help='location of the X11 alias file '
145-
'(default: %a)' % LOCALE_ALIAS)
145+
f'(default: {LOCALE_ALIAS})')
146146
parser.add_argument('--glibc-supported', default=SUPPORTED,
147147
help='location of the glibc SUPPORTED locales file '
148-
'(default: %a)' % SUPPORTED)
148+
f'(default: {SUPPORTED})')
149149
args = parser.parse_args()
150150

151151
data = locale.locale_alias.copy()

Tools/i18n/msgfmt.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
Display version information and exit.
2525
"""
2626

27-
import os
28-
import sys
27+
import array
2928
import ast
29+
import codecs
3030
import getopt
31+
import os
3132
import struct
32-
import array
33+
import sys
3334
from email.parser import HeaderParser
34-
import codecs
3535

3636
__version__ = "1.2"
3737

@@ -113,7 +113,7 @@ def make(filename, outfile):
113113
try:
114114
with open(infile, 'rb') as f:
115115
lines = f.readlines()
116-
except IOError as msg:
116+
except OSError as msg:
117117
print(msg, file=sys.stderr)
118118
sys.exit(1)
119119

@@ -126,6 +126,7 @@ def make(filename, outfile):
126126
sys.exit(1)
127127

128128
section = msgctxt = None
129+
msgid = msgstr = b''
129130
fuzzy = 0
130131

131132
# Start off assuming Latin-1, so everything decodes without failure,
@@ -177,7 +178,7 @@ def make(filename, outfile):
177178
# This is a message with plural forms
178179
elif l.startswith('msgid_plural'):
179180
if section != ID:
180-
print('msgid_plural not preceded by msgid on %s:%d' % (infile, lno),
181+
print(f'msgid_plural not preceded by msgid on {infile}:{lno}',
181182
file=sys.stderr)
182183
sys.exit(1)
183184
l = l[12:]
@@ -188,15 +189,15 @@ def make(filename, outfile):
188189
section = STR
189190
if l.startswith('msgstr['):
190191
if not is_plural:
191-
print('plural without msgid_plural on %s:%d' % (infile, lno),
192+
print(f'plural without msgid_plural on {infile}:{lno}',
192193
file=sys.stderr)
193194
sys.exit(1)
194195
l = l.split(']', 1)[1]
195196
if msgstr:
196197
msgstr += b'\0' # Separator of the various plural forms
197198
else:
198199
if is_plural:
199-
print('indexed msgstr required for plural on %s:%d' % (infile, lno),
200+
print(f'indexed msgstr required for plural on {infile}:{lno}',
200201
file=sys.stderr)
201202
sys.exit(1)
202203
l = l[6:]
@@ -212,8 +213,7 @@ def make(filename, outfile):
212213
elif section == STR:
213214
msgstr += l.encode(encoding)
214215
else:
215-
print('Syntax error on %s:%d' % (infile, lno), \
216-
'before:', file=sys.stderr)
216+
print(f'Syntax error on {infile}:{lno} before:', file=sys.stderr)
217217
print(l, file=sys.stderr)
218218
sys.exit(1)
219219
# Add last entry
@@ -226,7 +226,7 @@ def make(filename, outfile):
226226
try:
227227
with open(outfile,"wb") as f:
228228
f.write(output)
229-
except IOError as msg:
229+
except OSError as msg:
230230
print(msg, file=sys.stderr)
231231

232232

Tools/i18n/pygettext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def make_escapes(pass_nonascii):
193193
escape = escape_ascii
194194
else:
195195
escape = escape_nonascii
196-
escapes = [r"\%03o" % i for i in range(256)]
196+
escapes = [fr"\{i:03o}" for i in range(256)]
197197
for i in range(32, 127):
198198
escapes[i] = chr(i)
199199
escapes[ord('\\')] = r'\\'
@@ -796,7 +796,7 @@ class Options:
796796
try:
797797
with open(options.excludefilename) as fp:
798798
options.toexclude = fp.readlines()
799-
except IOError:
799+
except OSError:
800800
print(f"Can't read --exclude-file: {options.excludefilename}",
801801
file=sys.stderr)
802802
sys.exit(1)

0 commit comments

Comments
 (0)