Skip to content

Commit

Permalink
Replace old style strings with f-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
danyeaw authored and pbor committed Jan 13, 2025
1 parent 1dd3f7e commit f4e502b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 53 deletions.
4 changes: 2 additions & 2 deletions gvsbuild/utils/base_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def exec_msbuild(self, cmd, configuration=None, add_path=None):
add_path=add_path,
)

def _msbuild_make_search_replace(self, org_platform):
def _msbuild_make_search_replace(self, org_platform: str) -> tuple[bytes, bytes]:
"""Return the search & replace strings (converted to bytes to update
the platform Toolset version (v140, v141, ...) to use a new compiler,
e.g. to use vs2017 solution's files for vs2019.
Expand All @@ -226,7 +226,7 @@ def _msbuild_make_search_replace(self, org_platform):
dst_platform = "143"
else:
dst_platform = f"{ver}0"
search = (">v%u</PlatformToolset>" % (org_platform,)).encode("utf-8")
search = f">v{org_platform}</PlatformToolset>".encode()
replace = f">v{dst_platform}</PlatformToolset>".encode()

return search, replace
Expand Down
37 changes: 8 additions & 29 deletions gvsbuild/utils/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def build(self, projects):
if self.opts.check_hash:
return

# List of all the project we can mark for build because of a dependend
# List of all the project we can mark for build because of a dependent
self.prj_to_mark = [x for x in Project._projects if x.is_project()]

self.prj_done = []
Expand All @@ -551,11 +551,7 @@ def build(self, projects):
if self.__build_one(p):
self.prj_skipped.append(p.name)
else:
msg = "%-*s (%.3f s)" % (
Project.name_len,
p.name,
time.time() - st,
)
msg = f"{p.name:{Project.name_len}} ({time.time() - st:.3f})"
self.prj_done.append(msg)
except KeyboardInterrupt:
traceback.print_exc()
Expand Down Expand Up @@ -597,8 +593,7 @@ def build(self, projects):
log.message(f" {p}")
miss += len(self.prj_dropped)

# Don't fool appveyor
log.error_exit("%u project(s) missing ;(" % (miss,))
log.error_exit("%d project(s) missing ;(")

log.close()

Expand Down Expand Up @@ -798,21 +793,14 @@ def __download_progress(self, count, block_size, total_size):
if perc != self._old_perc:
perc = min(perc, 100)
self._old_perc = perc
sp = "%s (%u k) - %u%%" % (
self._downloading_file,
total_size / 1024,
self._old_perc,
)
sp = f"{self._downloading_file} ({total_size / 1024}k) - {self._old_perc:.0f}%"
print(sp, end="\r")
if len(sp) > self._old_print:
# Save the len to delete the line when we change file
self._old_print = len(sp)
else:
# Only the current, we don't know the size
sp = "%s - %u k" % (
self._downloading_file,
c_size / 1024,
)
sp = f"{self._downloading_file} - {c_size / 1024:.0f} k"
print(sp, end="\r")
if len(sp) > self._old_print:
self._old_print = len(sp)
Expand All @@ -837,14 +825,7 @@ def urlretrieve(self, url, filename, reporthook, ssl_ignore_cert=False):
msg = f"Opening {url} ..."
print(msg, end="\r")
with contextlib.closing(urlopen(url, None, context=ssl_ctx)) as fp:
print(
"%*s"
% (
len(msg),
"",
),
end="\r",
)
print(f"{'':>{len(msg)}}", end="\r")
headers = fp.info()

with open(filename, "wb") as tfp:
Expand All @@ -870,7 +851,7 @@ def urlretrieve(self, url, filename, reporthook, ssl_ignore_cert=False):

if size >= 0 and read < size:
raise ContentTooShortError(
"retrieval incomplete: got only %i out of %i bytes" % (read, size),
f"retrieval incomplete: got only {read} out of {size} bytes",
result,
)

Expand Down Expand Up @@ -920,9 +901,7 @@ def __download_one(self, proj):
raise
log.end()

print(
"%-*s" % (self._old_print, f"{proj.archive_file} - Download finished"),
)
print(f"{proj.archive_file:{self._old_print}} - Download finished")
return self.__check_hash(proj)

def __sub_vars(self, s):
Expand Down
25 changes: 4 additions & 21 deletions gvsbuild/utils/simple_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,9 @@ def _create_log(self, file_path, single, max_size_kb, opts):
self._output_val("Vs path", opts.vs_install_path)
self._output_val("Sdk ver", opts.win_sdk_ver)

def _get_delta(self, start, end=None):
if not end:
end = datetime.datetime.now()
def _get_delta(self, start):
dt = datetime.datetime.now() - start
return "%u.%03u" % (
dt.seconds,
dt.microseconds / 1000,
)
return f"{dt.seconds}.{dt.microseconds // 1000:03}"

def _indend_check(self):
if self.operations:
Expand Down Expand Up @@ -205,27 +200,15 @@ def _output(self, msg, add_date=True, check_indent=True):
now_val = datetime.datetime.now()
self.fo.write(f"{now_val.strftime('%Y-%m-%d %H:%M:%S')} {msg}\n")
else:
self.fo.write(
"%19s %s\n"
% (
"",
msg,
)
)
self.fo.write(f"{msg:<20}")
return False
else:
print(msg)
# tell the caller we already print on video
return True

def _output_val(self, msg, val):
self._output(
"%16s: %s"
% (
msg,
val,
)
)
self._output(f"{msg:<16}: {val}")

def message_indent(self, msg):
if self._output(msg, add_date=False):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def test_no_project(typer_app, runner):
assert "Missing argument" in result.output


@pytest.mark.skipif(not sys.platform.startswith("win"), reason="windll only available on Windows")
@pytest.mark.skipif(
not sys.platform.startswith("win"), reason="windll only available on Windows"
)
def test_platform(tmp_dir, typer_app, runner):
assert tmp_dir.exists()
result = runner.invoke(
Expand Down

0 comments on commit f4e502b

Please sign in to comment.