Skip to content

Commit

Permalink
Modernize formatter: use ruff instead of flake8 (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
shimizukawa committed May 19, 2024
1 parent b1f3505 commit 0c7a9ce
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 192 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
strategy:
fail-fast: false
matrix:
env: [flake8, mypy]
env: [lint, mypy]

steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion sphinx_intl/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.3.0'
__version__ = "2.3.0"
3 changes: 2 additions & 1 deletion sphinx_intl/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
if __name__ == '__main__':
if __name__ == "__main__":
from sphinx_intl.commands import main

main()
78 changes: 48 additions & 30 deletions sphinx_intl/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
# ==================================
# utility functions


def get_lang_dirs(path):
dirs = [relpath(d, path)
for d in glob(path+'/[a-z]*')
if os.path.isdir(d) and not d.endswith('pot')]
dirs = [
relpath(d, path)
for d in glob(path + "/[a-z]*")
if os.path.isdir(d) and not d.endswith("pot")
]
return (tuple(dirs),)


# ==================================
# commands


def update(locale_dir, pot_dir, languages, line_width=76, ignore_obsolete=False):
"""
Update specified language's po files from pot.
Expand All @@ -33,9 +37,9 @@ def update(locale_dir, pot_dir, languages, line_width=76, ignore_obsolete=False)
:rtype: dict
"""
status = {
'create': 0,
'update': 0,
'notchanged': 0,
"create": 0,
"update": 0,
"notchanged": 0,
}

for dirpath, dirnames, filenames in os.walk(pot_dir):
Expand All @@ -46,7 +50,7 @@ def update(locale_dir, pot_dir, languages, line_width=76, ignore_obsolete=False)
continue
basename = relpath(base, pot_dir)
for lang in languages:
po_dir = os.path.join(locale_dir, lang, 'LC_MESSAGES')
po_dir = os.path.join(locale_dir, lang, "LC_MESSAGES")
po_file = os.path.join(po_dir, basename + ".po")
cat_pot = c.load_po(pot_file)
if os.path.exists(po_file):
Expand All @@ -57,20 +61,31 @@ def update(locale_dir, pot_dir, languages, line_width=76, ignore_obsolete=False)
if msgids != new_msgids:
added = new_msgids - msgids
deleted = msgids - new_msgids
status['update'] += 1
click.echo('Update: {} +{}, -{}'.format(
po_file, len(added), len(deleted)))
c.dump_po(po_file, cat, width=line_width,
ignore_obsolete=ignore_obsolete)
status["update"] += 1
click.echo(
"Update: {} +{}, -{}".format(
po_file, len(added), len(deleted)
)
)
c.dump_po(
po_file,
cat,
width=line_width,
ignore_obsolete=ignore_obsolete,
)
else:
status['notchanged'] += 1
click.echo(f'Not Changed: {po_file}')
status["notchanged"] += 1
click.echo(f"Not Changed: {po_file}")
else: # new po file
status['create'] += 1
click.echo(f'Create: {po_file}')
status["create"] += 1
click.echo(f"Create: {po_file}")
cat_pot.locale = lang
c.dump_po(po_file, cat_pot, width=line_width,
ignore_obsolete=ignore_obsolete)
c.dump_po(
po_file,
cat_pot,
width=line_width,
ignore_obsolete=ignore_obsolete,
)

return status

Expand All @@ -87,7 +102,9 @@ def build(locale_dir, output_dir, languages):
for lang in languages:
lang_dir = os.path.join(locale_dir, lang)
for dirpath, dirnames, filenames in os.walk(lang_dir):
dirpath_output = os.path.join(output_dir, os.path.relpath(dirpath, locale_dir))
dirpath_output = os.path.join(
output_dir, os.path.relpath(dirpath, locale_dir)
)

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

if (os.path.exists(mo_file) and
os.path.getmtime(mo_file) > os.path.getmtime(po_file)):
if os.path.exists(mo_file) and os.path.getmtime(
mo_file
) > os.path.getmtime(po_file):
continue
click.echo(f'Build: {mo_file}')
click.echo(f"Build: {mo_file}")
cat = c.load_po(po_file)
c.write_mo(mo_file, cat)

Expand All @@ -126,17 +144,17 @@ def stat(locale_dir, languages):
continue

cat = c.load_po(po_file)
r = result[po_file.replace('\\', '/')] = {
'translated': len(c.translated_entries(cat)),
'fuzzy': len(c.fuzzy_entries(cat)),
'untranslated': len(c.untranslated_entries(cat)),
r = result[po_file.replace("\\", "/")] = {
"translated": len(c.translated_entries(cat)),
"fuzzy": len(c.fuzzy_entries(cat)),
"untranslated": len(c.untranslated_entries(cat)),
}
click.echo(
'{}: {} translated, {} fuzzy, {} untranslated.'.format(
"{}: {} translated, {} fuzzy, {} untranslated.".format(
po_file,
r['translated'],
r['fuzzy'],
r['untranslated'],
r["translated"],
r["fuzzy"],
r["untranslated"],
)
)

Expand Down
16 changes: 8 additions & 8 deletions sphinx_intl/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ def load_po(filename, **kwargs):
:return: catalog object
"""
# pre-read to get charset
with open(filename, 'rb') as f:
with open(filename, "rb") as f:
cat = pofile.read_po(f)
charset = cat.charset or 'utf-8'
charset = cat.charset or "utf-8"

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


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

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


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


Expand Down

0 comments on commit 0c7a9ce

Please sign in to comment.