Addon Manager: Create new uninstaller

This commit is contained in:
Chris Hennes
2022-12-18 13:56:36 -06:00
parent f9951c58d9
commit 2adbcc9199
8 changed files with 861 additions and 107 deletions

View File

@@ -34,7 +34,7 @@ import FreeCAD
import NetworkManager
from PySide import QtCore
from addonmanager_utilities import remove_directory_if_empty, is_float
from addonmanager_utilities import is_float
translate = FreeCAD.Qt.translate
@@ -509,89 +509,6 @@ class Macro:
).format(other_file, src_file)
)
def remove(self) -> bool:
"""Remove a macro and all its related files
Returns True if the macro was removed correctly.
"""
if not self.is_installed():
# Macro not installed, nothing to do.
return True
macro_dir = FreeCAD.getUserMacroDir(True)
try:
self._remove_core_macro_file(macro_dir)
self._remove_xpm_data(macro_dir)
self._remove_other_files(macro_dir)
except IsADirectoryError:
FreeCAD.Console.PrintError(
translate(
"AddonsInstaller",
"Tried to remove a directory when a file was expected\n",
)
)
return False
except FileNotFoundError:
FreeCAD.Console.PrintError(
translate(
"AddonsInstaller",
"Macro file could not be found, nothing to remove\n",
)
)
return False
return True
def _remove_other_files(self, macro_dir):
# Remove related files, which are supposed to be given relative to
# self.src_filename.
for other_file in self.other_files:
if not other_file:
continue
FreeCAD.Console.PrintMessage(other_file + "...")
dst_file = os.path.join(macro_dir, other_file)
if not dst_file or not os.path.exists(dst_file):
FreeCAD.Console.PrintMessage("X\n")
continue
try:
os.remove(dst_file)
remove_directory_if_empty(os.path.dirname(dst_file))
FreeCAD.Console.PrintMessage("\n")
except IsADirectoryError:
FreeCAD.Console.PrintMessage(" is a directory, not removed\n")
except FileNotFoundError:
FreeCAD.Console.PrintMessage(" could not be found, nothing to remove\n")
if os.path.isabs(self.icon):
dst_file = os.path.normpath(
os.path.join(macro_dir, os.path.basename(self.icon))
)
if os.path.exists(dst_file):
try:
FreeCAD.Console.PrintMessage(os.path.basename(self.icon) + "...")
os.remove(dst_file)
FreeCAD.Console.PrintMessage("\n")
except IsADirectoryError:
FreeCAD.Console.PrintMessage(" is a directory, not removed\n")
except FileNotFoundError:
FreeCAD.Console.PrintMessage(
" could not be found, nothing to remove\n"
)
return True
def _remove_core_macro_file(self, macro_dir):
macro_path = os.path.join(macro_dir, self.filename)
macro_path_with_macro_prefix = os.path.join(macro_dir, "Macro_" + self.filename)
if os.path.exists(macro_path):
os.remove(macro_path)
elif os.path.exists(macro_path_with_macro_prefix):
os.remove(macro_path_with_macro_prefix)
def _remove_xpm_data(self, macro_dir):
if self.xpm:
xpm_file = os.path.join(macro_dir, self.name + "_icon.xpm")
if os.path.exists(xpm_file):
os.remove(xpm_file)
def parse_wiki_page_for_icon(self, page_data: str) -> None:
"""Attempt to find a url for the icon in the wiki page. Sets self.icon if found."""