Skip to content

Commit

Permalink
Fix DeprecationWarnings for zmq and pkg_resources
Browse files Browse the repository at this point in the history
- Use tornado IOLoop instead of zmq.ioloop
- Use importlib instead of pkg_resources

Fixes #38 and #39
  • Loading branch information
tonyroberts committed Dec 4, 2023
1 parent 8df96f7 commit 49bab67
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
42 changes: 32 additions & 10 deletions pyxll_jupyter/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@
from .magic import ExcelMagics
from ipykernel.kernelapp import IPKernelApp
from ipykernel.embed import embed_kernel
from zmq.eventloop import ioloop
from pyxll import schedule_call, get_config
import pyxll
import importlib.util
import pkg_resources
import subprocess
import threading
import logging
import ctypes
import atexit
import queue
import zmq
import sys
import os
import re

if zmq.pyzmq_version_info()[0] >= 17:
from tornado.ioloop import IOLoop
else:
from zmq.eventloop.ioloop import IOLoop


# Command line args for the different Jupyter subcommands
_subcommand_jupyter_args = {
# subcommand
Expand Down Expand Up @@ -182,7 +187,7 @@ def _IPKernelApp_start(self):
self.kernel.start()

# set up a timer to periodically poll the zmq ioloop
self.loop = ioloop.IOLoop.current()
self.loop = IOLoop.current()

def poll_ioloop():
try:
Expand Down Expand Up @@ -255,6 +260,29 @@ def poll_ioloop():
return ipy


def _check_requirement(requirement):
"""Return True if a required package version is installed, False otherwise"""
if sys.version_info[:2] >= (3, 10):
from packaging.requirements import Requirement
import importlib.metadata

try:
requirement = Requirement(requirement)
version = importlib.metadata.version(requirement.name)
return version in requirement.specifier
except importlib.metadata.PackageNotFoundError:
return False

else:
import pkg_resources

try:
pkg_resources.require(requirement)
return True
except (pkg_resources.VersionConflict, pkg_resources.DistributionNotFound):
return


def _get_jupyter_args(subcommand):
"""Get the args for a Jupyter subcommand."""
if subcommand not in _subcommand_jupyter_args:
Expand All @@ -263,15 +291,9 @@ def _get_jupyter_args(subcommand):
# Find the args for the installed Jupyter version
requirement = "unknown"
for requirement, args in _subcommand_jupyter_args[subcommand]:
if requirement is None:
if requirement is None or _check_requirement(requirement):
return args

try:
pkg_resources.require(requirement)
return args
except (pkg_resources.VersionConflict, pkg_resources.DistributionNotFound):
pass

raise RuntimeError(f"Requirements for Jupyter {subcommand} not satisfied ({requirement}).")


Expand Down
17 changes: 15 additions & 2 deletions pyxll_jupyter/pyxll.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@
from pyxll import xlcAlert, get_config, xl_app, xl_macro, schedule_call
from functools import partial
import ctypes.wintypes
import pkg_resources
import logging
import sys
import os

_log = logging.getLogger(__name__)


if sys.version_info[:2] >= (3, 7):
import importlib.resources

def _resource_bytes(package, resource_name):
ref = importlib.resources.files(package).joinpath(resource_name)
with ref.open('rb') as fp:
return fp.read()
else:
import pkg_resources

def _resource_bytes(package, resource_name):
return pkg_resources.resource_stream(package, resource_name).read()


def _get_qt_app():
"""Get or create the Qt application"""
app = QApplication.instance()
Expand Down Expand Up @@ -335,7 +348,7 @@ def ribbon():
if disable_ribbon:
return []

ribbon = pkg_resources.resource_string(__name__, "resources/ribbon.xml")
ribbon = _resource_bytes("pyxll_jupyter", "resources/ribbon.xml").decode("utf-8")
return [
(None, ribbon)
]
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"jupyter >= 1.0.0",
"jupyter-client >= 6.0.0",
"notebook >= 6.0.0",
"packaging; python_version >= '3.10'",
"PySide2; python_version < '3.10'",
"PySide6 != 6.4.2; python_version >= '3.10' and platform_machine == 'x86_64'",
"PyQt5; python_version >= '3.10' and platform_machine != 'x86_64'",
Expand Down

0 comments on commit 49bab67

Please sign in to comment.