Skip to content

Commit

Permalink
Merge pull request getavalon#501 from BigRoy/patch_499
Browse files Browse the repository at this point in the history
Patch getavalon#499: Refactor find_host_config to lib.find_module_in_config
  • Loading branch information
BigRoy authored Jan 7, 2020
2 parents d380d88 + 241e05d commit 94aee92
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 99 deletions.
50 changes: 29 additions & 21 deletions avalon/fusion/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import contextlib
import importlib
import logging
from pyblish import api as pyblish
from avalon import api as avalon

import pyblish.api

from .. import api
from ..pipeline import AVALON_CONTAINER_ID
from ..lib import find_submodule


class CompLogHandler(logging.Handler):
Expand All @@ -23,32 +25,30 @@ def ls():
assets on disk, it lists assets already loaded in Fusion; once loaded
they are called 'containers'
Yields:
dict: container
"""

comp = get_current_comp()
tools = comp.GetToolList(False, "Loader").values()

has_metadata_collector = False
config_host = find_submodule(api.registered_config(), "fusion")
if hasattr(config_host, "collect_container_metadata"):
has_metadata_collector = True

for tool in tools:
container = parse_container(tool)
if container:
# Collect custom data if attribute is present
config = find_host_config(avalon.registered_config())
if hasattr(config, "collect_container_metadata"):
metadata = config.collect_container_metadata(container)

if has_metadata_collector:
metadata = config_host.collect_container_metadata(container)
container.update(metadata)

yield container


def find_host_config(config):
config_name = config.__name__
try:
config = importlib.import_module(config_name + ".fusion")
except ImportError:
pass

return config


def install(config):
"""Install Fusion-specific functionality of avalon-core.
Expand All @@ -60,7 +60,7 @@ def install(config):
# TODO: Set project
# TODO: Install Fusion menu (this is done with config .fu script actually)

pyblish.register_host("fusion")
pyblish.api.register_host("fusion")

# Remove all handlers associated with the root logger object, because
# that one sometimes logs as "warnings" incorrectly.
Expand All @@ -75,10 +75,18 @@ def install(config):
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

# Trigger install on the config's "fusion" package
config = find_host_config(config)
if hasattr(config, "install"):
config.install()

def uninstall(config):
"""Uninstall Fusion-specific functionality of avalon-core.
This function is called automatically on calling `api.uninstall()`.
Args:
config: configuration module
"""

pyblish.api.deregister_host("fusion")


def imprint_container(tool,
Expand Down
36 changes: 9 additions & 27 deletions avalon/houdini/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

# Local libraries
from . import lib
from ..lib import logger
from avalon import api, schema
from ..lib import logger, find_submodule
from .. import api

from ..pipeline import AVALON_CONTAINER_ID

Expand Down Expand Up @@ -39,10 +39,6 @@ def install(config):

self._has_been_setup = True

config = find_host_config(config)
if hasattr(config, "install"):
config.install()


def uninstall(config):
"""Uninstall Houdini-specific functionality of avalon-core.
Expand All @@ -54,27 +50,11 @@ def uninstall(config):
"""

config = find_host_config(config)
if hasattr(config, "uninstall"):
config.uninstall()

pyblish.api.deregister_host("hython")
pyblish.api.deregister_host("hpython")
pyblish.api.deregister_host("houdini")


def find_host_config(config):
config_name = config.__name__
try:
config = importlib.import_module(config_name + ".houdini")
except ImportError as exc:
if str(exc) != "No module name {}".format(config_name + ".houdini"):
raise
config = None

return config


def get_main_window():
"""Acquire Houdini's main window"""
if self._parent is None:
Expand All @@ -89,8 +69,6 @@ def reload_pipeline(*args):
"""

import importlib

api.uninstall()

for module in ("avalon.io",
Expand Down Expand Up @@ -236,13 +214,17 @@ def ls():
"pyblish.mindbender.container"):
containers += lib.lsattr("id", identifier)

has_metadata_collector = False
config_host = find_submodule(api.registered_config(), "houdini")
if hasattr(config_host, "collect_container_metadata"):
has_metadata_collector = True

for container in sorted(containers):
data = parse_container(container)

# Collect custom data if attribute is present
config = find_host_config(api.registered_config())
if hasattr(config, "collect_container_metadata"):
metadata = config.collect_container_metadata(container)
if has_metadata_collector:
metadata = config_host.collect_container_metadata(container)
data.update(metadata)

yield data
Expand Down
26 changes: 24 additions & 2 deletions avalon/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import datetime
import importlib
import subprocess
import types

Expand Down Expand Up @@ -236,7 +237,7 @@ def modules_from_path(path):
"""Get python scripts as modules from a path.
Arguments:
path (str): Path to python scrips.
path (str): Path to folder containing python scripts.
Returns:
List of modules.
Expand Down Expand Up @@ -275,9 +276,30 @@ def modules_from_path(path):
sys.modules[mod_name] = module

except Exception as err:
print("Skipped: \"%s\" (%s)", mod_name, err)
print("Skipped: \"{0}\" ({1})".format(mod_name, err))
continue

modules.append(module)

return modules


def find_submodule(module, submodule):
"""Find and return submodule of the module.
Args:
module (types.ModuleType): The module to search in.
submodule (str): The submodule name to find.
Returns:
types.ModuleType or None: The module, if found.
"""
name = "{0}.{1}".format(module.__name__, submodule)
try:
return importlib.import_module(name)
except ImportError as exc:
if str(exc) != "No module name {}".format(name):
log_.warning("Could not find '%s' in module: %s",
submodule,
module)
34 changes: 7 additions & 27 deletions avalon/maya/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from pyblish import api as pyblish

from . import lib, compat
from ..lib import logger
from .. import api, schema
from ..lib import logger, find_submodule
from .. import api
from ..tools import workfiles
from ..vendor.Qt import QtCore, QtWidgets

Expand Down Expand Up @@ -58,10 +58,6 @@ def install(config):
pyblish.register_host("mayapy")
pyblish.register_host("maya")

config = find_host_config(config)
if hasattr(config, "install"):
config.install()


def _set_project():
"""Sets the maya project to the current Session's work directory.
Expand All @@ -84,17 +80,6 @@ def _set_project():
cmds.workspace(workdir, openWorkspace=True)


def find_host_config(config):
try:
config = importlib.import_module(config.__name__ + ".maya")
except ImportError as exc:
if str(exc) != "No module name {}".format(config.__name__ + ".maya"):
raise
config = None

return config


def get_main_window():
"""Acquire Maya's main window"""
if self._parent is None:
Expand All @@ -111,9 +96,6 @@ def uninstall(config):
This function is called automatically on calling `api.uninstall()`.
"""
config = find_host_config(config)
if hasattr(config, "uninstall"):
config.uninstall()

_uninstall_menu()

Expand Down Expand Up @@ -230,8 +212,6 @@ def reload_pipeline(*args):
"""

import importlib

api.uninstall()

for module in ("avalon.io",
Expand Down Expand Up @@ -272,13 +252,13 @@ def reload_pipeline(*args):


def _uninstall_menu():

# In Maya 2020+ don't use the QApplication.instance()
# during startup (userSetup.py) as it will return a
# QtCore.QCoreApplication instance which does not have
# the allWidgets method. As such, we call the staticmethod.
all_widgets = QtWidgets.QApplication.allWidgets()

widgets = dict((w.objectName(), w) for w in all_widgets)
menu = widgets.get(self._menu)

Expand Down Expand Up @@ -517,16 +497,16 @@ def ls():
container_names = _ls()

has_metadata_collector = False
config = find_host_config(api.registered_config())
if hasattr(config, "collect_container_metadata"):
config_host = find_submodule(api.registered_config(), "maya")
if hasattr(config_host, "collect_container_metadata"):
has_metadata_collector = True

for container in sorted(container_names):
data = parse_container(container)

# Collect custom data if attribute is present
if has_metadata_collector:
metadata = config.collect_container_metadata(container)
metadata = config_host.collect_container_metadata(container)
data.update(metadata)

yield data
Expand Down
21 changes: 0 additions & 21 deletions avalon/nuke/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ def reload_pipeline():
"""

import importlib

api.uninstall()
_uninstall_menu()

Expand Down Expand Up @@ -227,26 +225,10 @@ def install(config):
_register_events()

pyblish.register_host("nuke")
# Trigger install on the config's "nuke" package
config = find_host_config(config)

if hasattr(config, "install"):
config.install()

log.info("config.nuke installed")


def find_host_config(config):
try:
config = importlib.import_module(config.__name__ + ".nuke")
except ImportError as exc:
if str(exc) != "No module name {}".format(config.__name__ + ".nuke"):
raise
config = None

return config


def get_main_window():
"""Acquire Nuke's main window"""
if self._parent is None:
Expand All @@ -270,9 +252,6 @@ def uninstall(config):
modifying the menu or registered families.
"""
config = find_host_config(config)
if hasattr(config, "uninstall"):
config.uninstall()

_uninstall_menu()

Expand Down
15 changes: 14 additions & 1 deletion avalon/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def install(host):
if hasattr(host, "install"):
host.install(config)

# Optional config.host.install()
host_name = host.__name__.rsplit(".", 1)[-1]
config_host = lib.find_submodule(config, host_name)
if hasattr(config_host, "install"):
config_host.install()

register_host(host)
register_config(config)

Expand All @@ -104,9 +110,16 @@ def find_config():
def uninstall():
"""Undo all of what `install()` did"""
config = registered_config()
host = registered_host()

# Optional config.host.uninstall()
host_name = host.__name__.rsplit(".", 1)[-1]
config_host = lib.find_submodule(config, host_name)
if hasattr(config_host, "uninstall"):
config_host.uninstall()

try:
registered_host().uninstall(config)
host.uninstall(config)
except AttributeError:
pass

Expand Down

0 comments on commit 94aee92

Please sign in to comment.