forked from pyblish/pyblish-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
188 lines (133 loc) · 5.34 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""Conveinence functions for general publishing"""
from __future__ import absolute_import
# Standard library
import logging
import warnings
# Local library
from . import api, logic, plugin, lib
log = logging.getLogger("pyblish.util")
def publish(context=None, plugins=None):
"""Publish everything
This function will process all available plugins of the
currently running host, publishing anything picked up
during collection.
Arguments:
context (Context, optional): Context, defaults to
creating a new context
plugins (list, optional): Plug-ins to include,
defaults to results of discover()
Usage:
>> context = plugin.Context()
>> publish(context) # Pass..
>> context = publish() # ..or receive a new
"""
# Must check against None, as objects be emptys
context = api.Context() if context is None else context
plugins = api.discover() if plugins is None else plugins
# Do not consider inactive plug-ins
plugins = list(p for p in plugins if p.active)
collectors = list(p for p in plugins if lib.inrange(
number=p.order,
base=api.CollectorOrder)
)
# First pass, collection
for Plugin, instance in logic.Iterator(collectors, context):
plugin.process(Plugin, context, instance)
# Exclude collectors from further processing
plugins = list(p for p in plugins if p not in collectors)
# Exclude plug-ins that do not have at
# least one compatible instance.
for Plugin in list(plugins):
if Plugin.__instanceEnabled__:
if not logic.instances_by_plugin(context, Plugin):
plugins.remove(Plugin)
# Keep track of state, so we can cancel on failed validation
state = {
"nextOrder": None,
"ordersWithError": set()
}
test = api.registered_test()
# Second pass, the remainder
for Plugin, instance in logic.Iterator(plugins, context):
state["nextOrder"] = Plugin.order
if test(**state):
log.error("Stopped due to: %s" % test(**state))
break
try:
result = plugin.process(Plugin, context, instance)
except:
# This exception is unexpected
log.error("An exception occurred.\n")
raise
else:
# Make note of the order at which the
# potential error error occured.
if result["error"]:
state["ordersWithError"].add(Plugin.order)
if isinstance(result, Exception):
log.error("An unexpected error happened: %s" % result)
break
error = result["error"]
if error is not None:
print(error)
api.emit("published", context=context)
return context
def collect(context=None, plugins=None):
"""Convenience function for collection-only
_________ . . . . . . . . . . . . . . . . . . .
| | . . . . . .
| Collect |-->. Validate .-->. Extract .-->. Integrate .
|_________| . . . . . . . . . . . . . . . . . . .
"""
context = _convenience(api.CollectorOrder, context, plugins)
api.emit("collected", context=context)
return context
def validate(context=None, plugins=None):
"""Convenience function for validation-only
. . . . . . __________ . . . . . . . . . . . . .
. . | | . . . .
. Collect .-->| Validate |-->. Extract .-->. Integrate .
. . . . . . |__________| . . . . . . . . . . . . .
"""
context = _convenience(api.ValidatorOrder, context, plugins)
api.emit("validated", context=context)
return context
def extract(context=None, plugins=None):
"""Convenience function for extraction-only
. . . . . . . . . . . . _________ . . . . . . .
. . . . | | . .
. Collect .-->. Validate .-->| Extract |-->. Integrate .
. . . . . . . . . . . . |_________| . . . . . . .
"""
context = _convenience(api.ExtractorOrder, context, plugins)
api.emit("extracted", context=context)
return context
def integrate(context=None, plugins=None):
"""Convenience function for integration-only
. . . . . . . . . . . . . . . . . . ___________
. . . . . . | |
. Collect .-->. Validate .-->. Extract .-->| Integrate |
. . . . . . . . . . . . . . . . . . |___________|
"""
context = _convenience(api.IntegratorOrder, context, plugins)
api.emit("integrated", context=context)
return context
def _convenience(order, context=None, plugins=None):
plugins = list(
p for p in (api.discover() if plugins is None else plugins)
if lib.inrange(p.order, order)
)
return publish(context, plugins)
# Backwards compatibility
select = collect
conform = integrate
run = publish # Alias
def publish_all(context=None, plugins=None):
warnings.warn("pyblish.util.publish_all has been "
"deprecated; use publish()")
return publish(context, plugins)
def validate_all(context=None, plugins=None):
warnings.warn("pyblish.util.validate_all has been "
"deprecated; use collect() followed by validate()")
context = collect(context, plugins)
return validate(context, plugins)