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

Patch #499: Refactor find_host_config to lib.find_module_in_config #501

Merged
merged 11 commits into from
Jan 7, 2020
43 changes: 25 additions & 18 deletions avalon/fusion/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from avalon import api as avalon

from ..pipeline import AVALON_CONTAINER_ID
from ..lib import find_module_in_config


class CompLogHandler(logging.Handler):
Expand All @@ -23,32 +24,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_module_in_config(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 @@ -74,11 +73,19 @@ def install(config):
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)


def uninstall(config):
"""Uninstall Fusion-specific functionality of avalon-core.

This function is called automatically on calling `api.uninstall()`.

Args:
config: configuration module

"""

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


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

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

from ..pipeline import AVALON_CONTAINER_ID
Expand Down Expand Up @@ -38,11 +38,7 @@ def install(config):
pyblish.api.register_host("hpython")

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 Down Expand Up @@ -236,13 +216,17 @@ def ls():
"pyblish.mindbender.container"):
containers += lib.lsattr("id", identifier)

for container in sorted(containers):
has_metadata_collector = False
config_host = find_module_in_config(api.registered_config(), "houdini")
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
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
20 changes: 20 additions & 0 deletions avalon/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,23 @@ def modules_from_path(path):
modules.append(module)

return modules


BigRoy marked this conversation as resolved.
Show resolved Hide resolved
def find_module_in_config(config, module):
"""Find and return submodule of the config.

Args:
config (types.ModuleType): The config to search in.
module (str): The config's submodule to search.

Returns:
types.ModuleType or None: The module, if found.

"""
config_name = config.__name__
module_name = "%s.%s" % (config_name, module)
try:
return importlib.import_module(module_name)
davidlatwe marked this conversation as resolved.
Show resolved Hide resolved
except ImportError as exc:
if str(exc) != "No module name {}".format(module_name):
log_.warning("Config has no '%s' module." % module_name)
26 changes: 4 additions & 22 deletions avalon/maya/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pyblish import api as pyblish

from . import lib, compat
from ..lib import logger
from ..lib import logger, find_module_in_config
from .. import api, schema
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 @@ -517,16 +499,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_module_in_config(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
19 changes: 0 additions & 19 deletions avalon/nuke/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,26 +227,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 +254,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
17 changes: 15 additions & 2 deletions avalon/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ def install(host):
# Optional host install function
if hasattr(host, "install"):
host.install(config)

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

register_host(host)
register_config(config)
Expand Down Expand Up @@ -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_module_in_config(config, host_name)
if hasattr(config_host, "uninstall"):
config_host.uninstall()

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

Expand Down