Skip to content

Commit 0c7a9ce

Browse files
authored
Modernize formatter: use ruff instead of flake8 (#103)
1 parent b1f3505 commit 0c7a9ce

File tree

9 files changed

+283
-192
lines changed

9 files changed

+283
-192
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
strategy:
6161
fail-fast: false
6262
matrix:
63-
env: [flake8, mypy]
63+
env: [lint, mypy]
6464

6565
steps:
6666
- name: Checkout code

sphinx_intl/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '2.3.0'
1+
__version__ = "2.3.0"

sphinx_intl/__main__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
if __name__ == '__main__':
1+
if __name__ == "__main__":
22
from sphinx_intl.commands import main
3+
34
main()

sphinx_intl/basic.py

+48-30
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@
1010
# ==================================
1111
# utility functions
1212

13+
1314
def get_lang_dirs(path):
14-
dirs = [relpath(d, path)
15-
for d in glob(path+'/[a-z]*')
16-
if os.path.isdir(d) and not d.endswith('pot')]
15+
dirs = [
16+
relpath(d, path)
17+
for d in glob(path + "/[a-z]*")
18+
if os.path.isdir(d) and not d.endswith("pot")
19+
]
1720
return (tuple(dirs),)
1821

1922

2023
# ==================================
2124
# commands
2225

26+
2327
def update(locale_dir, pot_dir, languages, line_width=76, ignore_obsolete=False):
2428
"""
2529
Update specified language's po files from pot.
@@ -33,9 +37,9 @@ def update(locale_dir, pot_dir, languages, line_width=76, ignore_obsolete=False)
3337
:rtype: dict
3438
"""
3539
status = {
36-
'create': 0,
37-
'update': 0,
38-
'notchanged': 0,
40+
"create": 0,
41+
"update": 0,
42+
"notchanged": 0,
3943
}
4044

4145
for dirpath, dirnames, filenames in os.walk(pot_dir):
@@ -46,7 +50,7 @@ def update(locale_dir, pot_dir, languages, line_width=76, ignore_obsolete=False)
4650
continue
4751
basename = relpath(base, pot_dir)
4852
for lang in languages:
49-
po_dir = os.path.join(locale_dir, lang, 'LC_MESSAGES')
53+
po_dir = os.path.join(locale_dir, lang, "LC_MESSAGES")
5054
po_file = os.path.join(po_dir, basename + ".po")
5155
cat_pot = c.load_po(pot_file)
5256
if os.path.exists(po_file):
@@ -57,20 +61,31 @@ def update(locale_dir, pot_dir, languages, line_width=76, ignore_obsolete=False)
5761
if msgids != new_msgids:
5862
added = new_msgids - msgids
5963
deleted = msgids - new_msgids
60-
status['update'] += 1
61-
click.echo('Update: {} +{}, -{}'.format(
62-
po_file, len(added), len(deleted)))
63-
c.dump_po(po_file, cat, width=line_width,
64-
ignore_obsolete=ignore_obsolete)
64+
status["update"] += 1
65+
click.echo(
66+
"Update: {} +{}, -{}".format(
67+
po_file, len(added), len(deleted)
68+
)
69+
)
70+
c.dump_po(
71+
po_file,
72+
cat,
73+
width=line_width,
74+
ignore_obsolete=ignore_obsolete,
75+
)
6576
else:
66-
status['notchanged'] += 1
67-
click.echo(f'Not Changed: {po_file}')
77+
status["notchanged"] += 1
78+
click.echo(f"Not Changed: {po_file}")
6879
else: # new po file
69-
status['create'] += 1
70-
click.echo(f'Create: {po_file}')
80+
status["create"] += 1
81+
click.echo(f"Create: {po_file}")
7182
cat_pot.locale = lang
72-
c.dump_po(po_file, cat_pot, width=line_width,
73-
ignore_obsolete=ignore_obsolete)
83+
c.dump_po(
84+
po_file,
85+
cat_pot,
86+
width=line_width,
87+
ignore_obsolete=ignore_obsolete,
88+
)
7489

7590
return status
7691

@@ -87,7 +102,9 @@ def build(locale_dir, output_dir, languages):
87102
for lang in languages:
88103
lang_dir = os.path.join(locale_dir, lang)
89104
for dirpath, dirnames, filenames in os.walk(lang_dir):
90-
dirpath_output = os.path.join(output_dir, os.path.relpath(dirpath, locale_dir))
105+
dirpath_output = os.path.join(
106+
output_dir, os.path.relpath(dirpath, locale_dir)
107+
)
91108

92109
for filename in filenames:
93110
base, ext = os.path.splitext(filename)
@@ -97,10 +114,11 @@ def build(locale_dir, output_dir, languages):
97114
mo_file = os.path.join(dirpath_output, base + ".mo")
98115
po_file = os.path.join(dirpath, filename)
99116

100-
if (os.path.exists(mo_file) and
101-
os.path.getmtime(mo_file) > os.path.getmtime(po_file)):
117+
if os.path.exists(mo_file) and os.path.getmtime(
118+
mo_file
119+
) > os.path.getmtime(po_file):
102120
continue
103-
click.echo(f'Build: {mo_file}')
121+
click.echo(f"Build: {mo_file}")
104122
cat = c.load_po(po_file)
105123
c.write_mo(mo_file, cat)
106124

@@ -126,17 +144,17 @@ def stat(locale_dir, languages):
126144
continue
127145

128146
cat = c.load_po(po_file)
129-
r = result[po_file.replace('\\', '/')] = {
130-
'translated': len(c.translated_entries(cat)),
131-
'fuzzy': len(c.fuzzy_entries(cat)),
132-
'untranslated': len(c.untranslated_entries(cat)),
147+
r = result[po_file.replace("\\", "/")] = {
148+
"translated": len(c.translated_entries(cat)),
149+
"fuzzy": len(c.fuzzy_entries(cat)),
150+
"untranslated": len(c.untranslated_entries(cat)),
133151
}
134152
click.echo(
135-
'{}: {} translated, {} fuzzy, {} untranslated.'.format(
153+
"{}: {} translated, {} fuzzy, {} untranslated.".format(
136154
po_file,
137-
r['translated'],
138-
r['fuzzy'],
139-
r['untranslated'],
155+
r["translated"],
156+
r["fuzzy"],
157+
r["untranslated"],
140158
)
141159
)
142160

sphinx_intl/catalog.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ def load_po(filename, **kwargs):
1111
:return: catalog object
1212
"""
1313
# pre-read to get charset
14-
with open(filename, 'rb') as f:
14+
with open(filename, "rb") as f:
1515
cat = pofile.read_po(f)
16-
charset = cat.charset or 'utf-8'
16+
charset = cat.charset or "utf-8"
1717

1818
# To decode lines by babel, read po file as binary mode and specify charset for
1919
# read_po function.
20-
with open(filename, 'rb') as f: # FIXME: encoding VS charset
20+
with open(filename, "rb") as f: # FIXME: encoding VS charset
2121
return pofile.read_po(f, charset=charset, **kwargs)
2222

2323

@@ -38,12 +38,12 @@ def dump_po(filename, catalog, **kwargs):
3838
# (compatibility) line_width was the original argument used to forward
3939
# line width hints into write_po's `width` argument; if provided,
4040
# set/override the width value
41-
if 'line_width' in kwargs:
42-
kwargs['width'] = kwargs['line_width']
43-
del kwargs['line_width']
41+
if "line_width" in kwargs:
42+
kwargs["width"] = kwargs["line_width"]
43+
del kwargs["line_width"]
4444

4545
# Because babel automatically encode strings, file should be open as binary mode.
46-
with open(filename, 'wb') as f:
46+
with open(filename, "wb") as f:
4747
pofile.write_po(f, catalog, **kwargs)
4848

4949

@@ -57,7 +57,7 @@ def write_mo(filename, catalog, **kwargs):
5757
dirname = os.path.dirname(filename)
5858
if not os.path.exists(dirname):
5959
os.makedirs(dirname)
60-
with open(filename, 'wb') as f:
60+
with open(filename, "wb") as f:
6161
mofile.write_mo(f, catalog, **kwargs)
6262

6363

0 commit comments

Comments
 (0)