Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python - Use Python fstrings everywhere #166

Merged
merged 1 commit into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qgispluginci/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
]

__author__ = "Denis Rouzaud, Étienne Trimaille, Julien Moura"
__copyright__ = "2019 - {0}, {1}".format(date.today().year, __author__)
__copyright__ = f"2019 - {date.today().year}, {__author__}"
__email__ = "[email protected]"
__license__ = "GNU General Public License v3.0"
__summary__ = (
Expand Down
13 changes: 4 additions & 9 deletions qgispluginci/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ def __init__(self, definition: dict):
self.plugin_slug = slugify(self.plugin_name)
self.project_slug = definition.get(
"project_slug",
os.environ.get("TRAVIS_REPO_SLUG", ".../{}".format(self.plugin_slug)).split(
"/"
)[1],
os.environ.get("TRAVIS_REPO_SLUG", f".../{self.plugin_slug}").split("/")[1],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Travis still around....?

)
self.github_organization_slug = definition.get(
"github_organization_slug",
Expand Down Expand Up @@ -177,11 +175,8 @@ def archive_name(
if "-" in plugin_name:
logger.warning(DASH_WARNING)

return "{zipname}{experimental}.{release_version}.zip".format(
zipname=plugin_name,
experimental="-experimental" if experimental else "",
release_version=release_version,
)
experimental = "-experimental" if experimental else ""
return f"{plugin_name}{experimental}.{release_version}.zip"

def __get_from_metadata(self, key: str, default_value: any = None) -> str:
if not self.plugin_path:
Expand All @@ -190,7 +185,7 @@ def __get_from_metadata(self, key: str, default_value: any = None) -> str:
metadata_file = f"{self.plugin_path}/metadata.txt"
with open(metadata_file) as f:
for line in f:
m = re.match(r"{}\s*=\s*(.*)$".format(key), line)
m = re.match(rf"{key}\s*=\s*(.*)$", line)
if m:
return m.group(1)
if default_value is None:
Expand Down
36 changes: 17 additions & 19 deletions qgispluginci/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def create_archive(
sub_repo = submodule.module()
logger.info("Git archive submodule: {sub_repo}")
sub_repo.git.archive(
"HEAD", "--prefix", "{}/".format(submodule.path), "-o", sub_tar_file
"HEAD", "--prefix", f"{submodule.path}/", "-o", sub_tar_file
)
with tarfile.open(top_tar_file, mode="a") as tt:
with tarfile.open(sub_tar_file, mode="r:") as st:
Expand All @@ -198,8 +198,8 @@ def create_archive(
if add_translations:
with tarfile.open(top_tar_file, mode="a") as tt:
logger.debug("Adding translations")
for file in glob("{}/i18n/*.qm".format(parameters.plugin_path)):
logger.debug(" adding translation: {}".format(os.path.basename(file)))
for file in glob(f"{parameters.plugin_path}/i18n/*.qm"):
logger.debug(f" adding translation: {os.path.basename(file)}")
# https://stackoverflow.com/a/48462950/1548052
tt.add(file)

Expand All @@ -208,12 +208,12 @@ def create_archive(
pyqt5ac.main(
ioPaths=[
[
"{}/*.qrc".format(parameters.plugin_path),
"{}/%%FILENAME%%_rc.py".format(parameters.plugin_path),
f"{parameters.plugin_path}/*.qrc",
f"{parameters.plugin_path}/%%FILENAME%%_rc.py",
]
]
)
for file in glob("{}/*_rc.py".format(parameters.plugin_path)):
for file in glob(f"{parameters.plugin_path}/*_rc.py"):
with tarfile.open(top_tar_file, mode="r:") as tt:
for n in tt.getnames():
if n == file:
Expand All @@ -226,7 +226,7 @@ def create_archive(
logger.error(err_msg, exc_info=BuiltResourceInSources())
sys.exit(1)
with tarfile.open(top_tar_file, mode="a") as tt:
logger.debug("\tAdding resource: {}".format(file))
logger.debug(f"\tAdding resource: {file}")
# https://stackoverflow.com/a/48462950/1548052
tt.add(file)

Expand Down Expand Up @@ -255,7 +255,7 @@ def create_archive(
zf.writestr(info, fl)

logger.debug("-" * 40)
logger.debug("Files in ZIP archive ({}):".format(archive_name))
logger.debug(f"Files in ZIP archive ({archive_name}):")
with zipfile.ZipFile(file=archive_name, mode="r") as zf:
for f in zf.namelist():
logger.debug(f)
Expand Down Expand Up @@ -283,7 +283,7 @@ def upload_asset_to_github_release(
asset_name: str = None,
):

slug = "{}/{}".format(parameters.github_organization_slug, parameters.project_slug)
slug = f"{parameters.github_organization_slug}/{parameters.project_slug}"
repo = Github(github_token).get_repo(slug)
try:
logger.debug(
Expand Down Expand Up @@ -337,7 +337,7 @@ def release_is_prerelease(
if not github_token:
return False

slug = "{}/{}".format(parameters.github_organization_slug, parameters.project_slug)
slug = f"{parameters.github_organization_slug}/{parameters.project_slug}"
repo = Github(github_token).get_repo(slug)
try:
logger.debug(
Expand Down Expand Up @@ -393,18 +393,16 @@ def create_plugin_repo(
"__REPO_URL__": parameters.repository_url,
}
if not plugin_repo_url:
download_url = "https://github.com/{org}/{repo}/releases/download/{tag}/{pluginzip}".format(
org=replace_dict["__ORG__"],
repo=replace_dict["__REPO__"],
tag=replace_dict["__RELEASE_TAG__"],
pluginzip=replace_dict["__PLUGINZIP__"],
orgs = replace_dict["__ORG__"]
repo = replace_dict["__REPO__"]
tag = replace_dict["__RELEASE_TAG__"]
plugin_zip = replace_dict["__PLUGINZIP__"]
download_url = (
f"https://github.com/{orgs}/{repo}/releases/download/{tag}/{plugin_zip}"
)
_, xml_repo = mkstemp(suffix=".xml")
else:
download_url = "{url}{pluginzip}".format(
url=plugin_repo_url,
pluginzip=replace_dict["__PLUGINZIP__"],
)
download_url = f"{plugin_repo_url}{replace_dict['__PLUGINZIP__']}"
xml_repo = "./plugins.xml"
replace_dict["__DOWNLOAD_URL__"] = download_url
with importlib_resources.path(
Expand Down
35 changes: 13 additions & 22 deletions qgispluginci/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ def __init__(
transifex_token, parameters.transifex_organization, i18n_type="QT"
)
assert self._t.ping()
self.ts_file = "{dir}/i18n/{res}_{lan}.ts".format(
dir=self.parameters.plugin_path,
res=self.parameters.transifex_resource,
lan=self.parameters.translation_source_language,
)
plugin_path = self.parameters.plugin_path
tx = self.parameters.transifex_resource
lang = self.parameters.translation_source_language
self.ts_file = f"{plugin_path}/i18n/{tx}_{lang}.ts"

if self._t.project_exists(parameters.transifex_project):
logger.debug(
Expand Down Expand Up @@ -91,12 +90,10 @@ def update_strings(self):
"""
source_py_files = []
source_ui_files = []
relative_path = "./{plugin_path}".format(
plugin_path=self.parameters.plugin_path
)
relative_path = f"./{self.parameters.plugin_path}"
for ext in ("py", "ui"):
for file in glob.glob(
"{dir}/**/*.{ext}".format(dir=self.parameters.plugin_path, ext=ext),
f"{self.parameters.plugin_path}/**/*.{ext}",
recursive=True,
):
file_path = str(Path(file).relative_to(relative_path))
Expand All @@ -112,13 +109,13 @@ def update_strings(self):
)

with open(project_file, "w") as f:
source_py_files = " ".join(source_py_files)
source_ui_files = " ".join(source_ui_files)
assert f.write("CODECFORTR = UTF-8\n")
assert f.write("SOURCES = {}\n".format(" ".join(source_py_files)))
assert f.write("FORMS = {}\n".format(" ".join(source_ui_files)))
assert f.write(f"SOURCES = {source_py_files}\n")
assert f.write(f"FORMS = {source_ui_files}\n")
assert f.write(
"TRANSLATIONS = {}\n".format(
Path(self.ts_file).relative_to(relative_path)
)
f"TRANSLATIONS = {Path(self.ts_file).relative_to(relative_path)}\n"
)
f.flush()
f.close()
Expand All @@ -142,9 +139,7 @@ def compile_strings(self):
Compile TS file into QM files
"""
cmd = [self.parameters.lrelease_path]
for file in glob.glob(
"{dir}/i18n/*.ts".format(dir=self.parameters.plugin_path)
):
for file in glob.glob(f"{self.parameters.plugin_path}/i18n/*.ts"):
cmd.append(file)
output = subprocess.run(cmd, capture_output=True, text=True)
if output.returncode != 0:
Expand Down Expand Up @@ -179,11 +174,7 @@ def pull(self):
)
existing_langs.append(lang)
for lang in existing_langs:
ts_file = "{dir}/i18n/{res}_{lan}.ts".format(
dir=self.parameters.plugin_path,
res=self.parameters.transifex_resource,
lan=lang,
)
ts_file = f"{self.parameters.plugin_path}/i18n/{self.parameters.transifex_resource}_{lang}.ts"
logger.debug(f"Downloading translation file: {ts_file}")
self._t.get_translation(
self.parameters.transifex_project, resource["slug"], lang, ts_file
Expand Down
14 changes: 5 additions & 9 deletions test/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,19 @@ def clean_assets(self):
try:
rel = self.repo.get_release(id=RELEASE_VERSION_TEST)
except GithubException:
raise GithubReleaseNotFound(
"Release {} not found".format(RELEASE_VERSION_TEST)
)
raise GithubReleaseNotFound(f"Release {RELEASE_VERSION_TEST} not found")
if rel:
print("deleting release assets")
for asset in rel.get_assets():
print(" delete {}".format(asset.name))
print(f" delete {asset.name}")
asset.delete_asset()
if self.t:
try:
self.t._t.delete_project(self.parameters.project_slug)
except PyTransifexException:
pass
try:
self.t._t.delete_team("{}-team".format(self.parameters.project_slug))
self.t._t.delete_team(f"{self.parameters.project_slug}-team")
except PyTransifexException:
pass

Expand Down Expand Up @@ -116,10 +114,8 @@ def test_release_upload_github(self):

# check the custom plugin repo
_, xml_repo = mkstemp(suffix=".xml")
url = "https://github.com/opengisch/qgis-plugin-ci/releases/download/{}/plugins.xml".format(
RELEASE_VERSION_TEST
)
print("retrieve repo from {}".format(url))
url = f"https://github.com/opengisch/qgis-plugin-ci/releases/download/{RELEASE_VERSION_TEST}/plugins.xml"
print(f"retrieve repo from {url}")
urllib.request.urlretrieve(url, xml_repo)
replace_in_file(
xml_repo,
Expand Down
2 changes: 1 addition & 1 deletion test/test_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def tearDown(self):
except PyTransifexException:
pass
try:
self.t._t.delete_team("{}-team".format(self.parameters.project_slug))
self.t._t.delete_team(f"{self.parameters.project_slug}-team")
except PyTransifexException:
pass

Expand Down