cq: Format with black and isort

This commit is contained in:
Philipp Hörist
2025-01-25 19:15:37 +01:00
parent e6e71d82bf
commit 841b1fb25e
44 changed files with 1641 additions and 1660 deletions

View File

@@ -1,40 +1,38 @@
# Keep this file python 3.7 compatible because it is executed on the server
from typing import Any
from typing import Iterator
import sys
import json
import logging
import sys
from collections import defaultdict
from pathlib import Path
from zipfile import ZipFile
FORMAT = '%(asctime)s %(message)s'
FORMAT = "%(asctime)s %(message)s"
logging.basicConfig(format=FORMAT, level=logging.DEBUG)
log = logging.getLogger()
REQUIRED_KEYS: set[str] = {
'authors',
'description',
'homepage',
'config_dialog',
'name',
'platforms',
'requirements',
'short_name',
'version'
"authors",
"description",
"homepage",
"config_dialog",
"name",
"platforms",
"requirements",
"short_name",
"version",
}
PACKAGE_INDEX: dict[str, Any] = {
'metadata': {
'repository_name': 'master',
'image_path': 'images.zip',
"metadata": {
"repository_name": "master",
"image_path": "images.zip",
},
'plugins': defaultdict(dict)
"plugins": defaultdict(dict),
}
@@ -44,53 +42,53 @@ def is_manifest_valid(manifest: dict[str, Any]) -> bool:
def iter_releases(release_folder: Path) -> Iterator[dict[str, Any]]:
for path in release_folder.rglob('*.zip'):
for path in release_folder.rglob("*.zip"):
with ZipFile(path) as release_zip:
if path.name == 'images.zip':
if path.name == "images.zip":
continue
log.info('Check path: %s', path)
log.info("Check path: %s", path)
try:
with release_zip.open('plugin-manifest.json') as file:
with release_zip.open("plugin-manifest.json") as file:
manifest = json.load(file)
yield manifest
except Exception:
log.error('Error loading manifest')
log.exception('')
log.error("Error loading manifest")
log.exception("")
def build_package_index(release_folder: Path) -> None:
log.info('Build package index')
log.info("Build package index")
for manifest in iter_releases(release_folder):
if not is_manifest_valid(manifest):
log.warning('Invalid manifest')
log.warning("Invalid manifest")
log.warning(manifest)
continue
short_name = manifest.pop('short_name')
version = manifest.pop('version')
PACKAGE_INDEX['plugins'][short_name][version] = manifest
log.info('Found manifest: %s - %s', short_name, version)
short_name = manifest.pop("short_name")
version = manifest.pop("version")
PACKAGE_INDEX["plugins"][short_name][version] = manifest
log.info("Found manifest: %s - %s", short_name, version)
path = release_folder / 'package_index.json'
with path.open('w') as f:
path = release_folder / "package_index.json"
with path.open("w") as f:
json.dump(PACKAGE_INDEX, f)
def build_image_zip(release_folder: Path) -> None:
log.info('Build images.zip')
with ZipFile(release_folder / 'images.zip', mode='w') as image_zip:
log.info("Build images.zip")
with ZipFile(release_folder / "images.zip", mode="w") as image_zip:
for path in release_folder.iterdir():
if not path.is_dir():
continue
image = path / f'{path.name}.png'
image = path / f"{path.name}.png"
if not image.exists():
continue
image_zip.write(image, arcname=image.name)
if __name__ == '__main__':
if __name__ == "__main__":
path = Path(sys.argv[1])
build_package_index(path)
build_image_zip(path)
log.info('Finished')
log.info("Finished")

View File

@@ -5,25 +5,24 @@ import re
import subprocess
from pathlib import Path
REPO_DIR = Path(__file__).parent.parent
TRANS_DIR = REPO_DIR / 'po'
TRANS_TEMPLATE = TRANS_DIR / 'gajim_plugins.pot'
BUILD_DIR = REPO_DIR / 'plugins_translations'
TRANS_DIR = REPO_DIR / "po"
TRANS_TEMPLATE = TRANS_DIR / "gajim_plugins.pot"
BUILD_DIR = REPO_DIR / "plugins_translations"
TRANSLATABLE_FILES = [
'*.py',
'*.ui',
"*.py",
"*.ui",
]
def template_is_equal(old_template_path: Path, new_template: str) -> bool:
with open(old_template_path, 'r') as f:
with open(old_template_path, "r") as f:
old_template = f.read()
pattern = r'"POT-Creation-Date: .*\n"'
old_template = re.sub(pattern, '', old_template, count=1)
new_template = re.sub(pattern, '', new_template, count=1)
old_template = re.sub(pattern, "", old_template, count=1)
new_template = re.sub(pattern, "", new_template, count=1)
return old_template == new_template
@@ -34,86 +33,77 @@ def update_translation_template() -> bool:
paths += list(REPO_DIR.rglob(file_path))
cmd = [
'xgettext',
'-o', '-',
'-c#',
'--from-code=utf-8',
'--keyword=Q_',
'--no-location',
'--sort-output',
'--package-name=Gajim Plugins'
"xgettext",
"-o",
"-",
"-c#",
"--from-code=utf-8",
"--keyword=Q_",
"--no-location",
"--sort-output",
"--package-name=Gajim Plugins",
]
for path in paths:
cmd.append(str(path))
result = subprocess.run(cmd,
cwd=REPO_DIR,
text=True,
check=True,
capture_output=True)
result = subprocess.run(
cmd, cwd=REPO_DIR, text=True, check=True, capture_output=True
)
template = result.stdout
if (TRANS_TEMPLATE.exists() and
template_is_equal(TRANS_TEMPLATE, template)):
if TRANS_TEMPLATE.exists() and template_is_equal(TRANS_TEMPLATE, template):
# No new strings were discovered
return False
with open(TRANS_TEMPLATE, 'w') as f:
with open(TRANS_TEMPLATE, "w") as f:
f.write(template)
return True
def update_translation_files() -> None:
for file in TRANS_DIR.glob('*.po'):
subprocess.run(['msgmerge',
'-U',
'--sort-output',
str(file),
TRANS_TEMPLATE],
cwd=REPO_DIR,
check=True)
for file in TRANS_DIR.glob("*.po"):
subprocess.run(
["msgmerge", "-U", "--sort-output", str(file), TRANS_TEMPLATE],
cwd=REPO_DIR,
check=True,
)
def build_translations() -> None:
for po_file in TRANS_DIR.glob('*.po'):
for po_file in TRANS_DIR.glob("*.po"):
lang = po_file.stem
po_file = TRANS_DIR / f'{lang}.po'
mo_file = BUILD_DIR / f'{po_file.stem}.mo'
po_file = TRANS_DIR / f"{lang}.po"
mo_file = BUILD_DIR / f"{po_file.stem}.mo"
subprocess.run(['msgfmt',
str(po_file),
'-o',
str(mo_file)],
cwd=REPO_DIR,
check=True)
subprocess.run(
["msgfmt", str(po_file), "-o", str(mo_file)], cwd=REPO_DIR, check=True
)
def cleanup_translations() -> None:
for po_file in TRANS_DIR.glob('*.po'):
subprocess.run(['msgattrib',
'--output-file',
str(po_file),
'--no-obsolete',
str(po_file)],
cwd=REPO_DIR,
check=True)
for po_file in TRANS_DIR.glob("*.po"):
subprocess.run(
["msgattrib", "--output-file", str(po_file), "--no-obsolete", str(po_file)],
cwd=REPO_DIR,
check=True,
)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Update Translations')
parser.add_argument('command', choices=['update', 'build', 'cleanup'])
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Update Translations")
parser.add_argument("command", choices=["update", "build", "cleanup"])
args = parser.parse_args()
if args.command == 'cleanup':
if args.command == "cleanup":
cleanup_translations()
elif args.command == 'update':
elif args.command == "update":
update_translation_template()
update_translation_files()
elif args.command == 'build':
elif args.command == "build":
update_translation_template()
update_translation_files()
build_translations()