FreeCAD

Форк
0
/
addonmanager_workers_utility.py 
93 строки · 4.3 Кб
1
# SPDX-License-Identifier: LGPL-2.1-or-later
2
# ***************************************************************************
3
# *                                                                         *
4
# *   Copyright (c) 2022-2023 FreeCAD Project Association                   *
5
# *                                                                         *
6
# *   This file is part of FreeCAD.                                         *
7
# *                                                                         *
8
# *   FreeCAD is free software: you can redistribute it and/or modify it    *
9
# *   under the terms of the GNU Lesser General Public License as           *
10
# *   published by the Free Software Foundation, either version 2.1 of the  *
11
# *   License, or (at your option) any later version.                       *
12
# *                                                                         *
13
# *   FreeCAD is distributed in the hope that it will be useful, but        *
14
# *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
15
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
16
# *   Lesser General Public License for more details.                       *
17
# *                                                                         *
18
# *   You should have received a copy of the GNU Lesser General Public      *
19
# *   License along with FreeCAD. If not, see                               *
20
# *   <https://www.gnu.org/licenses/>.                                      *
21
# *                                                                         *
22
# ***************************************************************************
23

24
""" Misc. worker thread classes for the FreeCAD Addon Manager. """
25

26
from typing import Optional
27

28
import FreeCAD
29
from PySide import QtCore
30
import NetworkManager
31
import time
32

33
translate = FreeCAD.Qt.translate
34

35

36
class ConnectionChecker(QtCore.QThread):
37
    """A worker thread for checking the connection to GitHub as a proxy for overall
38
    network connectivity. It has two signals: success() and failure(str). The failure
39
    signal contains a translated error message suitable for display to an end user."""
40

41
    success = QtCore.Signal()
42
    failure = QtCore.Signal(str)
43

44
    def __init__(self):
45
        QtCore.QThread.__init__(self)
46
        self.done = False
47
        self.request_id = None
48
        self.data = None
49

50
    def run(self):
51
        """Not generally called directly: create a new ConnectionChecker object and call start()
52
        on it to spawn a child thread."""
53

54
        FreeCAD.Console.PrintLog("Checking network connection...\n")
55
        url = "https://api.github.com/zen"
56
        self.done = False
57
        NetworkManager.AM_NETWORK_MANAGER.completed.connect(self.connection_data_received)
58
        self.request_id = NetworkManager.AM_NETWORK_MANAGER.submit_unmonitored_get(
59
            url, timeout_ms=10000
60
        )
61
        while not self.done:
62
            if QtCore.QThread.currentThread().isInterruptionRequested():
63
                FreeCAD.Console.PrintLog("Connection check cancelled\n")
64
                NetworkManager.AM_NETWORK_MANAGER.abort(self.request_id)
65
                self.disconnect_network_manager()
66
                return
67
            QtCore.QCoreApplication.processEvents()
68
            time.sleep(0.1)
69
        if not self.data:
70
            self.failure.emit(
71
                translate(
72
                    "AddonsInstaller",
73
                    "Unable to read data from GitHub: check your internet connection and proxy "
74
                    "settings and try again.",
75
                )
76
            )
77
            self.disconnect_network_manager()
78
            return
79
        FreeCAD.Console.PrintLog(f"GitHub's zen message response: {self.data.decode('utf-8')}\n")
80
        self.disconnect_network_manager()
81
        self.success.emit()
82

83
    def connection_data_received(self, id: int, status: int, data: QtCore.QByteArray):
84
        if self.request_id is not None and self.request_id == id:
85
            if status == 200:
86
                self.data = data.data()
87
            else:
88
                FreeCAD.Console.PrintWarning(f"No data received: status returned was {status}\n")
89
                self.data = None
90
            self.done = True
91

92
    def disconnect_network_manager(self):
93
        NetworkManager.AM_NETWORK_MANAGER.completed.disconnect(self.connection_data_received)
94

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.