Skip to content

Commit

Permalink
Fix falling back to default zeroconf version on older python installs...
Browse files Browse the repository at this point in the history
... and handle case where zeroconf is not available at all
  • Loading branch information
fieldOfView committed Jan 2, 2023
1 parent b380868 commit 67a4e08
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions OctoPrintOutputDevicePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@

from typing import Any, Dict, List, Union, Optional, TYPE_CHECKING

try:
ìmport_exceptions = (SyntaxError, FileNotFoundError, ModuleNotFoundError, ImportError)
except NameError:
# Python 3.5 does not know the ModuleNotFoundError
ìmport_exceptions = (SyntaxError, FileNotFoundError, ImportError)

if TYPE_CHECKING:
# for MYPY, fall back to the system-installed version
from zeroconf import (
Expand All @@ -45,13 +51,14 @@
import importlib.util

original_path = list(sys.path)
original_zeroconf_module = None

if "zeroconf" in sys.modules:
Logger.log(
"d",
"The zeroconf module is already imported; flush it so we can import our own version",
"The zeroconf module is already imported; flush it to use a newer version",
)
sys.modules.pop("zeroconf")
original_zeroconf_module = sys.modules.pop("zeroconf")

plugin_path = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(plugin_path, "ifaddr"))
Expand Down Expand Up @@ -81,25 +88,39 @@
sys.path = original_path

Logger.log("d", "Using included Zeroconf module version %s" % zeroconf_version)
except (FileNotFoundError, ModuleNotFoundError, ImportError) as exception:
except ìmport_exceptions as exception:
# fall back to the system-installed version, or what comes with Cura
Logger.logException("e", "Failed to load included version of Zeroconf module")

# restore original path
sys.path = original_path
if original_zeroconf_module:
sys.modules["zeroconf"] = original_zeroconf_module

from zeroconf import (
Zeroconf,
ServiceBrowser,
ServiceStateChange,
ServiceInfo,
DNSAddress,
__version__ as zeroconf_version,
)
try:
from zeroconf import (
Zeroconf,
ServiceBrowser,
ServiceStateChange,
ServiceInfo,
DNSAddress,
__version__ as zeroconf_version,
)

Logger.log(
"w", "Falling back to default Zeroconf module version %s" % zeroconf_version
)
except ImportError:
Zeroconf = None
ServiceBrowser = None
ServiceStateChange = None
ServiceInfo = None
DNSAddress = None

Logger.log(
"w", "Zeroconf could not be loaded; Auto-discovery is not available"
)

Logger.log(
"w", "Falling back to default Zeroconf module version %s" % zeroconf_version
)

if TYPE_CHECKING:
from cura.PrinterOutput.PrinterOutputModel import PrinterOutputModel
Expand Down

0 comments on commit 67a4e08

Please sign in to comment.