Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor utils.ocp_plugins to a module property to init on reference instead of on import #56

Merged
merged 5 commits into from
Mar 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion ovos_plugin_common_play/ocp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from ovos_plugin_manager.ocp import StreamHandler
from ovos_plugin_common_play.ocp.status import TrackState, PlaybackType
from ovos_ocp_files_plugin.plugin import OCPFilesMetadataExtractor
ocp_plugins = StreamHandler()

_ocp_plugins = None


def is_qtav_available():
Expand Down Expand Up @@ -56,3 +57,34 @@ def create_desktop_file():
dst_icon = join(icon_path, "OCP.png")
if not isfile(dst_icon):
shutil.copy(src_icon, dst_icon)


def module_property(func):
NeonDaniel marked this conversation as resolved.
Show resolved Hide resolved
"""
Decorator to turn module functions into properties.
Function names must be prefixed with an underscore.
:param func: function to decorate
"""
import sys
module = sys.modules[func.__module__]

def fallback_getattr(name):
raise AttributeError(
f"module '{module.__name__}' has no attribute '{name}'")

default_getattr = getattr(module, '__getattr__', fallback_getattr)

def patched_getattr(name):
if f'_{name}' == func.__name__:
return func()
return default_getattr(name)

module.__getattr__ = patched_getattr
return func


@module_property
def ocp_plugins():
global _ocp_plugins
_ocp_plugins = _ocp_plugins or StreamHandler()
return _ocp_plugins