Skip to content

Commit 360551d

Browse files
committed
[wip]
1 parent 43c93cc commit 360551d

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/qbpm/icons.py

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,9 @@ def download_icon(profile: Profile, home_page: str, overwrite: bool) -> Optional
6565
if icon_path:
6666
# print(f"installed {client.base_url.join(icon.url)}")
6767
return icon_path
68-
if not icon_path:
69-
# TODO pretty print
70-
info(f"no favicons found matching one of {PREFERRED_ICONS}")
71-
return None
68+
# TODO pretty print
69+
info(f"no favicons found matching one of {PREFERRED_ICONS}")
70+
return None
7271
except Exception as e:
7372
# info(str(e))
7473
raise e
@@ -78,8 +77,6 @@ def download_icon(profile: Profile, home_page: str, overwrite: bool) -> Optional
7877
tmp_dir.cleanup()
7978
client.close()
8079

81-
return None
82-
8380

8481
def icon_for_profile(profile: Profile) -> Optional[str]:
8582
icon_file = next(find_icon_files(profile), None)
@@ -91,49 +88,48 @@ def icon_for_profile(profile: Profile) -> Optional[str]:
9188
def install_icon_file(
9289
profile: Profile, src: Path, overwrite: bool, origin: Optional[str] = None
9390
) -> Optional[Path]:
94-
clean_icons = check_for_icons(profile, overwrite)
91+
icon_format = src.suffix
92+
dest = (profile.root / f"icon{icon_format}").absolute()
93+
clean_icons = check_for_icons(profile, overwrite, dest)
9594
if clean_icons is None:
9695
return None
97-
icon_format = src.suffix
98-
dest = profile.root / f"icon{icon_format}"
9996
if icon_format not in {".png", ".svg"}:
10097
dest = dest.with_suffix(".png")
10198
try:
10299
image = Image.open(src)
103-
clean_icons(set())
104100
image.save(dest, format="png")
105101
except Exception as e:
106102
error(str(e))
107103
error(f"failed to convert {origin or src} to png")
108104
dest.unlink(missing_ok=True)
109105
return None
110-
else:
111-
if src.resolve() != dest:
112-
shutil.copy(src, dest)
113-
clean_icons({dest})
106+
elif src.resolve() != dest:
107+
shutil.copy(src, dest)
108+
clean_icons()
114109
print(dest)
115-
return dest.absolute()
110+
return dest
116111

117112

118113
def install_icon_by_name(profile: Profile, icon_name: str, overwrite: bool) -> bool:
119114
clean_icons = check_for_icons(profile, overwrite)
120115
if clean_icons is None:
121116
return False
122-
clean_icons(set())
117+
clean_icons()
123118
file = profile.root / "icon.name"
124119
file.write_text(icon_name)
125120
return True
126121

127122

128123
def check_for_icons(
129-
profile: Profile, overwrite: bool
130-
) -> Callable[[set[Path]], None] | None:
124+
profile: Profile, overwrite: bool, dest: Path | None = None
125+
) -> Callable[[], None] | None:
131126
existing_icons = set(find_icon_files(profile))
132127
if existing_icons and not overwrite:
133128
error(f"icon already exists in {profile.root}, pass --overwrite to replace it")
134129
return None
135130

136-
def clean_icons(keep: set[Path]) -> None:
131+
def clean_icons() -> None:
132+
keep = {dest} if dest else set()
137133
for icon in existing_icons - keep:
138134
icon.unlink()
139135

0 commit comments

Comments
 (0)