Skip to content

Commit

Permalink
Stop using imp module it's deprecated since python 3.4 and removed in…
Browse files Browse the repository at this point in the history
… python 3.12
  • Loading branch information
EvanBldy committed Oct 9, 2023
1 parent c369126 commit ff4b511
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions src/py-opentimelineio/opentimelineio/plugins/python_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"""Base class for OTIO plugins that are exposed by manifests."""

import os
import imp
import sys
import importlib.util
import inspect
import collections
import copy
Expand All @@ -14,9 +15,7 @@
exceptions,
)

from . import (
manifest
)
from . import manifest


def plugin_info_map():
Expand All @@ -32,16 +31,17 @@ def plugin_info_map():
try:
type_map[plug.name] = plug.plugin_info_map()
except Exception as err:
type_map[plug.name] = (
"ERROR: could not compute plugin_info_map because:"
" {}".format(err)
type_map[
plug.name
] = "ERROR: could not compute plugin_info_map because:" " {}".format(
err
)

result[pt] = type_map

result['hooks'] = copy.deepcopy(active_manifest.hooks)
result["hooks"] = copy.deepcopy(active_manifest.hooks)

result['manifests'] = copy.deepcopy(active_manifest.source_files)
result["manifests"] = copy.deepcopy(active_manifest.source_files)

return result

Expand Down Expand Up @@ -69,20 +69,19 @@ def __init__(
"filepath",
str,
doc=(
"Absolute path or relative path to adapter module from location of"
" json."
)
"Absolute path or relative path to adapter module from location of" " json."
),
)

def plugin_info_map(self):
"""Returns a map with information about the plugin."""

result = collections.OrderedDict()

result['name'] = self.name
result['doc'] = inspect.getdoc(self.module())
result['path'] = self.module_abs_path()
result['from manifest'] = self._json_path
result["name"] = self.name
result["doc"] = inspect.getdoc(self.module())
result["path"] = self.module_abs_path()
result["from manifest"] = self._json_path

return result

Expand All @@ -94,10 +93,7 @@ def module_abs_path(self):
if not self._json_path:
raise exceptions.MisconfiguredPluginError(
"{} plugin is misconfigured, missing json path. "
"plugin: {}".format(
self.name,
repr(self)
)
"plugin: {}".format(self.name, repr(self))
)

filepath = os.path.join(os.path.dirname(self._json_path), filepath)
Expand All @@ -110,21 +106,15 @@ def _imported_module(self, namespace):
pyname = os.path.splitext(os.path.basename(self.module_abs_path()))[0]
pydir = os.path.dirname(self.module_abs_path())

(file_obj, pathname, description) = imp.find_module(pyname, [pydir])

with file_obj:
# this will reload the module if it has already been loaded.
mod = imp.load_module(
f"opentimelineio.{namespace}.{self.name}",
file_obj,
pathname,
description
)
spec = importlib.util.find_spec(pyname, [pydir])
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)

return mod
return module

def module(self):
"""Return the module object for this adapter. """
"""Return the module object for this adapter."""

if not self._module:
self._module = self._imported_module("adapters")
Expand All @@ -139,4 +129,4 @@ def _execute_function(self, func_name, **kwargs):
raise exceptions.AdapterDoesntSupportFunctionError(
f"Sorry, {self.name} doesn't support {func_name}."
)
return (getattr(self.module(), func_name)(**kwargs))
return getattr(self.module(), func_name)(**kwargs)

0 comments on commit ff4b511

Please sign in to comment.