added new exception PublishXmlValidationError

This commit is contained in:
iLLiCiTiT 2021-12-21 13:21:31 +01:00
parent fdcbd03d90
commit 9b0e2c3e8b
3 changed files with 87 additions and 3 deletions

View file

@ -6,7 +6,9 @@ from .publish_plugins import (
from .lib import (
DiscoverResult,
publish_plugins_discover
publish_plugins_discover,
load_help_content_from_plugin,
load_help_content_from_filepath
)
@ -16,5 +18,7 @@ __all__ = (
"OpenPypePyblishPluginMixin",
"DiscoverResult",
"publish_plugins_discover"
"publish_plugins_discover",
"load_help_content_from_plugin",
"load_help_content_from_filepath"
)

View file

@ -1,6 +1,8 @@
import os
import sys
import types
import inspect
import xml.etree.ElementTree
import six
import pyblish.plugin
@ -28,6 +30,61 @@ class DiscoverResult:
self.plugins[item] = value
class HelpContent:
def __init__(self, title, description, detail=None):
self.title = title
self.description = description
self.detail = detail
def load_help_content_from_filepath(filepath):
"""Load help content from xml file.
Xml file may containt errors and warnings.
"""
errors = {}
warnings = {}
output = {
"errors": errors,
"warnings": warnings
}
if not os.path.exists(filepath):
return output
tree = xml.etree.ElementTree.parse(filepath)
root = tree.getroot()
for child in root:
child_id = child.attrib.get("id")
if child_id is None:
continue
# Make sure ID is string
child_id = str(child_id)
title = child.find("title").text
description = child.find("description").text
detail_node = child.find("detail")
detail = None
if detail_node:
detail = detail_node.text
if child.tag == "error":
errors[child_id] = HelpContent(title, description, detail)
elif child.tag == "warning":
warnings[child_id] = HelpContent(title, description, detail)
return output
def load_help_content_from_plugin(plugin):
cls = plugin
if not inspect.isclass(plugin):
cls = plugin.__class__
plugin_filepath = inspect.getfile(cls)
plugin_dir = os.path.dirname(plugin_filepath)
basename = os.path.splitext(os.path.basename(plugin_filepath))[0]
filename = basename + ".xml"
filepath = os.path.join(plugin_dir, "help", filename)
return load_help_content_from_filepath(filepath)
def publish_plugins_discover(paths=None):
"""Find and return available pyblish plug-ins

View file

@ -1,3 +1,6 @@
from .lib import load_help_content_from_plugin
class PublishValidationError(Exception):
"""Validation error happened during publishing.
@ -12,13 +15,33 @@ class PublishValidationError(Exception):
description(str): Detailed description of an error. It is possible
to use Markdown syntax.
"""
def __init__(self, message, title=None, description=None):
def __init__(self, message, title=None, description=None, detail=None):
self.message = message
self.title = title or "< Missing title >"
self.description = description or message
self.detail = detail
super(PublishValidationError, self).__init__(message)
class PublishXmlValidationError(PublishValidationError):
def __init__(
self, message, plugin, key=None, *formattings_arg, **formatting_kwargs
):
if key is None:
key = "main"
result = load_help_content_from_plugin(plugin)
content_obj = result["errors"][key]
description = content_obj.description.format(
*formattings_arg, **formatting_kwargs
)
detail = content_obj.detail.format(
*formattings_arg, **formatting_kwargs
)
super(PublishXmlValidationError, self).__init__(
message, content_obj.title, description, detail
)
class KnownPublishError(Exception):
"""Publishing crashed because of known error.