Skip to content

Commit

Permalink
Don't use deprecated load_module
Browse files Browse the repository at this point in the history
Closes #227
  • Loading branch information
otsaloma committed Jun 21, 2024
1 parent 143dafa commit 9e5710d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 1 addition & 3 deletions gaupol/extensionman.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import aeidon
import gaupol
import importlib.machinery
import inspect
import os
import re
Expand Down Expand Up @@ -122,8 +121,7 @@ def setup_extension(self, module, slave=False):
directory = os.path.dirname(metadata.path)
name = "gaupol.extension.{:s}".format(module)
path = os.path.join(directory, "{}.py".format(module))
loader = importlib.machinery.SourceFileLoader(name, path)
module_object = loader.load_module(name)
module_object = gaupol.util.load_module_from_file(name, path)
for attribute in dir(module_object):
if attribute.startswith("_"): continue
value = getattr(module_object, attribute)
Expand Down
9 changes: 9 additions & 0 deletions gaupol/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import aeidon
import gaupol
import importlib.util
import inspect
import sys
import traceback
Expand Down Expand Up @@ -227,6 +228,14 @@ def lines_to_px(nlines, font=None):
height = label.get_preferred_height()[1]
return int(round(nlines * height))

def load_module_from_file(name, path):
# https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module

def new_hbox(spacing):
"""Return a new horizontal :class:`Gtk.Box`."""
return Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL,
Expand Down

0 comments on commit 9e5710d

Please sign in to comment.