@@ -65,10 +65,9 @@ def download_icon(profile: Profile, home_page: str, overwrite: bool) -> Optional
65
65
if icon_path :
66
66
# print(f"installed {client.base_url.join(icon.url)}")
67
67
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
72
71
except Exception as e :
73
72
# info(str(e))
74
73
raise e
@@ -78,8 +77,6 @@ def download_icon(profile: Profile, home_page: str, overwrite: bool) -> Optional
78
77
tmp_dir .cleanup ()
79
78
client .close ()
80
79
81
- return None
82
-
83
80
84
81
def icon_for_profile (profile : Profile ) -> Optional [str ]:
85
82
icon_file = next (find_icon_files (profile ), None )
@@ -91,49 +88,48 @@ def icon_for_profile(profile: Profile) -> Optional[str]:
91
88
def install_icon_file (
92
89
profile : Profile , src : Path , overwrite : bool , origin : Optional [str ] = None
93
90
) -> 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 )
95
94
if clean_icons is None :
96
95
return None
97
- icon_format = src .suffix
98
- dest = profile .root / f"icon{ icon_format } "
99
96
if icon_format not in {".png" , ".svg" }:
100
97
dest = dest .with_suffix (".png" )
101
98
try :
102
99
image = Image .open (src )
103
- clean_icons (set ())
104
100
image .save (dest , format = "png" )
105
101
except Exception as e :
106
102
error (str (e ))
107
103
error (f"failed to convert { origin or src } to png" )
108
104
dest .unlink (missing_ok = True )
109
105
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 ()
114
109
print (dest )
115
- return dest . absolute ()
110
+ return dest
116
111
117
112
118
113
def install_icon_by_name (profile : Profile , icon_name : str , overwrite : bool ) -> bool :
119
114
clean_icons = check_for_icons (profile , overwrite )
120
115
if clean_icons is None :
121
116
return False
122
- clean_icons (set () )
117
+ clean_icons ()
123
118
file = profile .root / "icon.name"
124
119
file .write_text (icon_name )
125
120
return True
126
121
127
122
128
123
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 :
131
126
existing_icons = set (find_icon_files (profile ))
132
127
if existing_icons and not overwrite :
133
128
error (f"icon already exists in { profile .root } , pass --overwrite to replace it" )
134
129
return None
135
130
136
- def clean_icons (keep : set [Path ]) -> None :
131
+ def clean_icons () -> None :
132
+ keep = {dest } if dest else set ()
137
133
for icon in existing_icons - keep :
138
134
icon .unlink ()
139
135
0 commit comments