Skip to content

Commit

Permalink
Addon Manager: Create NetworkManager class
Browse files Browse the repository at this point in the history
To enable single-login authenticated proxy use, and simplified multi-threaded
network accesses, this commit adds a new wrapper around a QNetworkAccessManager
and includes a global instantiation of the class intended to exist for the
lifetime of the program. This instance can be used to enqueue any number of
network requests, which the manager will send out to the networking subsystem
in an appropriate manner.
  • Loading branch information
chennes committed Jan 25, 2022
1 parent 4d87039 commit 2b0a4dc
Show file tree
Hide file tree
Showing 8 changed files with 1,057 additions and 662 deletions.
17 changes: 13 additions & 4 deletions src/Mod/AddonManager/AddonManager.py
Expand Up @@ -42,6 +42,8 @@
from package_details import PackageDetails
from AddonManagerRepo import AddonManagerRepo

from NetworkManager import HAVE_QTNETWORK

__title__ = "FreeCAD Addon Manager Module"
__author__ = "Yorik van Havre", "Jonathan Wiedemann", "Kurt Kremitzki", "Chris Hennes"
__url__ = "http://www.freecad.org"
Expand Down Expand Up @@ -228,9 +230,16 @@ def network_connection_failed(self, message: str) -> None:
# This must run on the main GUI thread
if hasattr(self, "connection_check_message") and self.connection_check_message:
self.connection_check_message.close()
QtWidgets.QMessageBox.critical(
None, translate("AddonsInstaller", "Connection failed"), message
)
if HAVE_QTNETWORK:
QtWidgets.QMessageBox.critical(
None, translate("AddonsInstaller", "Connection failed"), message
)
else:
QtWidgets.QMessageBox.critical(
None,
translate("AddonsInstaller", "Missing dependency"),
translate("AddonsInstaller", "Could not import QtNetwork -- see Report View for details. Addon Manager unavailable."),
)

def launch(self) -> None:
"""Shows the Addon Manager UI"""
Expand Down Expand Up @@ -821,7 +830,7 @@ def table_row_activated(self, selected_repo: AddonManagerRepo) -> None:
self.packageDetails.show_repo(selected_repo)

def show_information(self, message: str) -> None:
"""shows generic text in the information pane (which might be collapsed)"""
"""shows generic text in the information pane"""

self.dialog.labelStatusInfo.setText(message)
self.dialog.labelStatusInfo.repaint()
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/AddonManager/CMakeLists.txt
Expand Up @@ -8,7 +8,6 @@ SET(AddonManager_SRCS
AddonManager.py
AddonManagerRepo.py
addonmanager_macro.py
addonmanager_metadata.py
addonmanager_utilities.py
addonmanager_workers.py
AddonManager.ui
Expand All @@ -18,6 +17,7 @@ SET(AddonManager_SRCS
compact_view.py
dependency_resolution_dialog.ui
expanded_view.py
NetworkManager.py
package_list.py
package_details.py
)
Expand Down
552 changes: 552 additions & 0 deletions src/Mod/AddonManager/NetworkManager.py

Large diffs are not rendered by default.

33 changes: 10 additions & 23 deletions src/Mod/AddonManager/addonmanager_macro.py
Expand Up @@ -30,9 +30,10 @@
from typing import Dict, Tuple, List, Union

import FreeCAD
from NetworkManager import AM_NETWORK_MANAGER

translate = FreeCAD.Qt.translate

from addonmanager_utilities import translate
from addonmanager_utilities import urlopen
from addonmanager_utilities import remove_directory_if_empty

try:
Expand Down Expand Up @@ -165,28 +166,25 @@ def fill_details_from_code(self, code: str) -> None:

def fill_details_from_wiki(self, url):
code = ""
u = urlopen(url)
if u is None:
p = AM_NETWORK_MANAGER.blocking_get(url)
if not p:
FreeCAD.Console.PrintWarning(
translate(
"AddonsInstaller",
f"Could not connect to {url} - check connection and proxy settings",
f"Unable to open macro wiki page at {url}",
)
+ "\n"
)
return
p = u.read()
if isinstance(p, bytes):
p = p.decode("utf-8")
u.close()
p = p.data().decode("utf8")
# check if the macro page has its code hosted elsewhere, download if
# needed
if "rawcodeurl" in p:
rawcodeurl = re.findall('rawcodeurl.*?href="(http.*?)">', p)
if rawcodeurl:
rawcodeurl = rawcodeurl[0]
u2 = urlopen(rawcodeurl)
if u2 is None:
u2 = AM_NETWORK_MANAGER.blocking_get(rawcodeurl)
if not u2:
FreeCAD.Console.PrintWarning(
translate(
"AddonsInstaller",
Expand All @@ -195,18 +193,7 @@ def fill_details_from_wiki(self, url):
+ "\n"
)
return
response = ""
block = 8192
while True:
data = u2.read(block)
if not data:
break
if isinstance(data, bytes):
data = data.decode("utf-8")
response += data
if response:
code = response
u2.close()
code = u2.data().decode("utf8")
if not code:
code = re.findall(r"<pre>(.*?)</pre>", p.replace("\n", "--endl--"))
if code:
Expand Down

0 comments on commit 2b0a4dc

Please sign in to comment.