Skip to content

Commit ad24ece

Browse files
committed
Eliminate all FreeCAD imports
1 parent cb01494 commit ad24ece

34 files changed

+579
-707
lines changed

AddonManager.py

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
#!/usr/bin/env python3
2-
31
# SPDX-License-Identifier: LGPL-2.1-or-later
42
# ***************************************************************************
53
# * *
6-
# * Copyright (c) 2022-2023 FreeCAD Project Association *
4+
# * Copyright (c) 2022-2025 The FreeCAD project association AISBL *
75
# * Copyright (c) 2015 Yorik van Havre <yorik@uncreated.net> *
86
# * *
97
# * This file is part of FreeCAD. *
@@ -32,12 +30,7 @@
3230
from datetime import date
3331
from typing import Dict
3432

35-
from PySide import QtGui, QtCore, QtWidgets
36-
from PySide.QtGui import QDesktopServices
37-
from PySide.QtCore import QUrl
38-
39-
import FreeCAD
40-
import FreeCADGui
33+
from PySideWrapper import QtGui, QtCore, QtWidgets
4134

4235
from addonmanager_workers_startup import (
4336
CreateAddonListWorker,
@@ -75,7 +68,7 @@
7568

7669
from AddonManagerOptions import AddonManagerOptions
7770

78-
translate = FreeCAD.Qt.translate
71+
translate = fci.translate
7972

8073

8174
def QT_TRANSLATE_NOOP(_, txt):
@@ -129,7 +122,7 @@ def get_icon(repo: Addon, update: bool = False) -> QtGui.QIcon:
129122
path = os.path.join(os.path.dirname(repo.macro.src_filename), repo.macro.icon)
130123
default_icon = QtGui.QIcon(":/icons/document-python.svg")
131124
elif repo.macro and repo.macro.xpm:
132-
cache_path = FreeCAD.getUserCachePath()
125+
cache_path = fci.DataPaths().cache_dir
133126
am_path = os.path.join(cache_path, "AddonManager", "MacroIcons")
134127
os.makedirs(am_path, exist_ok=True)
135128
path = os.path.join(am_path, repo.name + "_icon.xpm")
@@ -188,10 +181,11 @@ def __init__(self):
188181
super().__init__()
189182

190183
QT_TRANSLATE_NOOP("QObject", "Addon Manager")
191-
FreeCADGui.addPreferencePage(
192-
AddonManagerOptions,
193-
"Addon Manager",
194-
)
184+
if fci.GuiUp:
185+
fci.addPreferencePage(
186+
AddonManagerOptions,
187+
"Addon Manager",
188+
)
195189

196190
self.item_model = None
197191
self.developer_mode = None
@@ -254,28 +248,25 @@ def launch(self) -> None:
254248
"""Shows the Addon Manager UI"""
255249

256250
# create the dialog
257-
self.dialog = FreeCADGui.PySideUic.loadUi(
258-
os.path.join(os.path.dirname(__file__), "AddonManager.ui")
259-
)
251+
self.dialog = fci.loadUi(os.path.join(os.path.dirname(__file__), "AddonManager.ui"))
260252
self.dialog.setObjectName("AddonManager_Main_Window")
261253
# self.dialog.setWindowFlag(QtCore.Qt.WindowStaysOnTopHint, True)
262254

263255
# cleanup the leftovers from previous runs
264-
self.macro_repo_dir = FreeCAD.getUserMacroDir(True)
256+
self.macro_repo_dir = fci.DataPaths().macro_dir
265257
self.packages_with_updates = set()
266258
self.startup_sequence = []
267259
self.cleanup_workers()
268260
self.update_cache = local_cache_needs_update()
269261

270262
# restore window geometry from stored state
271-
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons")
272-
w = pref.GetInt("WindowWidth", 800)
273-
h = pref.GetInt("WindowHeight", 600)
263+
w = fci.Preferences().get("WindowWidth")
264+
h = fci.Preferences().get("WindowHeight")
274265
self.composite_view = CompositeView(self.dialog)
275266
self.button_bar = WidgetGlobalButtonBar(self.dialog)
276267

277268
# If we are checking for updates automatically, hide the Check for updates button:
278-
autocheck = pref.GetBool("AutoCheck", True)
269+
autocheck = fci.Preferences().get("AutoCheck")
279270
if autocheck:
280271
self.button_bar.check_for_updates.hide()
281272
else:
@@ -290,8 +281,7 @@ def launch(self) -> None:
290281
# set nice icons to everything, by theme with fallback to FreeCAD icons
291282
self.dialog.setWindowIcon(QtGui.QIcon(":/icons/AddonManager.svg"))
292283

293-
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons")
294-
dev_mode_active = pref.GetBool("developerMode", False)
284+
dev_mode_active = fci.Preferences().get("developerMode")
295285

296286
# enable/disable stuff
297287
self.button_bar.update_all_addons.setEnabled(False)
@@ -323,12 +313,13 @@ def launch(self) -> None:
323313
self.composite_view.update.connect(self.update)
324314
self.composite_view.update_status.connect(self.status_updated)
325315

326-
# center the dialog over the FreeCAD window
316+
# center the dialog over the FreeCAD window, if it exists
327317
self.dialog.resize(w, h)
328-
mw = FreeCADGui.getMainWindow()
329-
self.dialog.move(
330-
mw.frameGeometry().topLeft() + mw.rect().center() - self.dialog.rect().center()
331-
)
318+
if fci.FreeCADGui:
319+
mw = fci.FreeCADGui.getMainWindow()
320+
self.dialog.move(
321+
mw.frameGeometry().topLeft() + mw.rect().center() - self.dialog.rect().center()
322+
)
332323

333324
# begin populating the table in a set of sub-threads
334325
self.startup()
@@ -353,7 +344,7 @@ def cleanup_workers(self) -> None:
353344
if not thread.isFinished():
354345
finished = thread.wait(500)
355346
if not finished:
356-
FreeCAD.Console.PrintWarning(
347+
fci.Console.PrintWarning(
357348
translate(
358349
"AddonsInstaller",
359350
"Worker process {} is taking a long time to stop...",
@@ -368,9 +359,8 @@ def reject(self) -> None:
368359
"""called when the window has been closed"""
369360

370361
# save window geometry for next use
371-
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons")
372-
pref.SetInt("WindowWidth", self.dialog.width())
373-
pref.SetInt("WindowHeight", self.dialog.height())
362+
fci.Preferences().set("WindowWidth", self.dialog.width())
363+
fci.Preferences().set("WindowHeight", self.dialog.height())
374364

375365
# ensure all threads are finished before closing
376366
ok_to_close = True
@@ -407,7 +397,7 @@ def reject(self) -> None:
407397
self.write_macro_cache()
408398
else:
409399
self.write_cache_stopfile()
410-
FreeCAD.Console.PrintLog(
400+
fci.Console.PrintLog(
411401
"Not writing the cache because a process was forcibly terminated and the state is "
412402
"unknown.\n"
413403
)
@@ -478,8 +468,7 @@ def startup(self) -> None:
478468
self.fetch_addon_score,
479469
self.select_addon,
480470
]
481-
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons")
482-
if pref.GetBool("DownloadMacros", True):
471+
if fci.Preferences().get("DownloadMacros"):
483472
self.startup_sequence.append(self.load_macro_metadata)
484473
self.number_of_progress_regions = len(self.startup_sequence)
485474
self.current_progress_region = 0
@@ -499,8 +488,7 @@ def do_next_startup_phase(self) -> None:
499488
self.button_bar.refresh_local_cache.setText(
500489
translate("AddonsInstaller", "Refresh local cache")
501490
)
502-
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons")
503-
pref.SetString("LastCacheUpdate", date.today().isoformat())
491+
fci.Preferences().set("LastCacheUpdate", date.today().isoformat())
504492
self.composite_view.package_list.item_filter.invalidateFilter()
505493

506494
def populate_packages_table(self) -> None:
@@ -595,7 +583,7 @@ def cache_macro(self, repo: Addon):
595583
if repo.macro is not None:
596584
self.macro_cache.append(repo.macro.to_cache())
597585
else:
598-
FreeCAD.Console.PrintError(
586+
fci.Console.PrintError(
599587
f"Addon Manager: Internal error, cache_macro called on non-macro {repo.name}\n"
600588
)
601589

@@ -621,7 +609,7 @@ def update_metadata_cache(self) -> None:
621609

622610
def on_button_update_cache_clicked(self) -> None:
623611
self.update_cache = True
624-
cache_path = FreeCAD.getUserCachePath()
612+
cache_path = fci.DataPaths().cache_dir
625613
am_path = os.path.join(cache_path, "AddonManager")
626614
utils.rmdir(am_path)
627615
self.button_bar.refresh_local_cache.setEnabled(False)
@@ -663,10 +651,9 @@ def select_addon(self) -> None:
663651
def check_updates(self) -> None:
664652
"""checks every installed addon for available updates"""
665653

666-
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons")
667-
autocheck = pref.GetBool("AutoCheck", True)
654+
autocheck = fci.Preferences().get("AutoCheck")
668655
if not autocheck:
669-
FreeCAD.Console.PrintLog(
656+
fci.Console.PrintLog(
670657
"Addon Manager: Skipping update check because AutoCheck user preference is False\n"
671658
)
672659
self.do_next_startup_phase()
@@ -740,8 +727,7 @@ def show_python_updates_dialog(self) -> None:
740727

741728
def fetch_addon_stats(self) -> None:
742729
"""Fetch the Addon Stats JSON data from a URL"""
743-
pref = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Addons")
744-
url = pref.GetString("AddonsStatsURL", "https://freecad.org/addon_stats.json")
730+
url = fci.Preferences().get("AddonsStatsURL")
745731
if url and url != "NONE":
746732
self.get_basic_addon_stats_worker = GetBasicAddonStatsWorker(
747733
url, self.item_model.repos, self.dialog
@@ -815,7 +801,7 @@ def mark_repo_update_available(self, repo: Addon, available: bool) -> None:
815801

816802
def launch_installer_gui(self, addon: Addon) -> None:
817803
if self.installer_gui is not None:
818-
FreeCAD.Console.PrintError(
804+
fci.Console.PrintError(
819805
translate(
820806
"AddonsInstaller",
821807
"Cannot launch a new installer until the previous one has finished.",
@@ -841,7 +827,7 @@ def update_all(self) -> None:
841827
stop other updates"""
842828

843829
if self.installer_gui is not None:
844-
FreeCAD.Console.PrintError(
830+
fci.Console.PrintError(
845831
translate(
846832
"AddonsInstaller",
847833
"Cannot launch a new installer until the previous one has finished.",
@@ -912,32 +898,36 @@ def on_package_status_changed(self, repo: Addon) -> None:
912898
def execute_macro(self, repo: Addon) -> None:
913899
"""executes a selected macro"""
914900

901+
if not fci.FreeCADGui:
902+
fci.Console.PrintError("Cannot execute a FreeCAD Macro outside FreeCAD")
903+
return
904+
915905
macro = repo.macro
916906
if not macro or not macro.code:
917907
return
918908

919909
if macro.is_installed():
920910
macro_path = os.path.join(self.macro_repo_dir, macro.filename)
921-
FreeCADGui.open(str(macro_path))
911+
fci.FreeCADGui.open(str(macro_path))
922912
self.dialog.hide()
923-
FreeCADGui.SendMsgToActiveView("Run")
913+
fci.FreeCADGui.SendMsgToActiveView("Run")
924914
else:
925915
with tempfile.TemporaryDirectory() as temp_dir:
926916
temp_install_succeeded = macro.install(temp_dir)
927917
if not temp_install_succeeded:
928-
FreeCAD.Console.PrintError(
918+
fci.Console.PrintError(
929919
translate("AddonsInstaller", "Temporary installation of macro failed.")
930920
)
931921
return
932922
macro_path = os.path.join(temp_dir, macro.filename)
933-
FreeCADGui.open(str(macro_path))
923+
fci.FreeCADGui.open(str(macro_path))
934924
self.dialog.hide()
935-
FreeCADGui.SendMsgToActiveView("Run")
925+
fci.FreeCADGui.SendMsgToActiveView("Run")
936926

937927
def remove(self, addon: Addon) -> None:
938928
"""Remove this addon."""
939929
if self.installer_gui is not None:
940-
FreeCAD.Console.PrintError(
930+
fci.Console.PrintError(
941931
translate(
942932
"AddonsInstaller",
943933
"Cannot launch a new installer until the previous one has finished.",
@@ -953,7 +943,7 @@ def remove(self, addon: Addon) -> None:
953943

954944
def open_addons_folder(self):
955945
addons_folder = fci.DataPaths().mod_dir
956-
QDesktopServices.openUrl(QUrl.fromLocalFile(addons_folder))
946+
QtGui.QDesktopServices.openUrl(QtCore.QUrl.fromLocalFile(addons_folder))
957947
return
958948

959949

0 commit comments

Comments
 (0)