Report IV
Visualising results right away is useful, but sometimes you need to go back in time. So how about instead of printing the report, we write it to a file? Better yet, why not develop a plug-in to write the file such that it happens along with every publish?
1
└── logs
2
├── 20150612-112011.txt
3
├── 20150612-112021.txt
4
├── 20150612-112051.txt
5
├── 20150612-112118.txt
6
└── 20150612-112158.txt
Copied!
Let's get cracking.
1
import os
2
import datetime
3
import pyblish.api
4
5
class ArchiveValidation(pyblish.api.ContextPlugin):
6
# Run after all validators have finished
7
order = pyblish.api.ValidatorOrder + 0.1
8
9
def process(self, context):
10
formatted_results = self.format_results(context)
11
12
# Compute output directory
13
date = datetime.datetime.today().ctime()
14
date = pyblish.api.format_filename(date)
15
output_dir = os.path.join(os.path.expanduser("~"), "logs")
16
output_path = os.path.join(output_dir, date + ".txt")
17
18
# Write to disk
19
if not os.path.exists(output_dir):
20
os.makedirs(output_dir)
21
22
with open(output_path, "w") as f:
23
# E.g. c:\users\marcus\Documents\logs\20150612-110000.txt
24
f.write(formatted_report)
Copied!
We copy/paste the formatting from our previous example and chuck it into format_results, this time returning as opposed to printing.
1
def format_results(self, context):
2
header = "{:<10}{:<40} -> {}".format("Success", "Plug-in", "Instance")
3
result = "{success:<10}{plugin.__name__:<40} -> {instance}"
4
error = "{:<10}+-- EXCEPTION: {:<70}"
5
record = "{:<10}+-- {level}: {message:<70}"
6
7
results = list()
8
for r in context.data["results"]:
9
# Format summary
10
results.append(result.format(**r))
11
12
# Format log records
13
for lr in r["records"]:
14
results.append(record.format("", level=lr.levelname, message=lr.msg))
15
16
# Format exception (if any)
17
if r["error"]:
18
results.append(error.format("", r["error"]))
19
20
report = """
21
{header}
22
{line}
23
{results}
24
"""
25
26
return report.format(
27
header=header,
28
results="\n".join(results),
29
line="-" * 70))
Copied!
Putting it all together, here is the full source code.
1
import os
2
import datetime
3
import pyblish.api
4
5
class CollectCaptainAmerica(pyblish.api.ContextPlugin):
6
order = pyblish.api.CollectorOrder
7
8
def process(self, context):
9
context.create_instance("Captain America", isHero=False)
10
11
class ValidateCaptainAmerica(pyblish.api.InstancePlugin):
12
order = pyblish.api.ValidatorOrder
13
14
def process(self, instance):
15
self.log.info("Entering validator..")
16
self.log.info("About to validate instance: %s" % instance)
17
18
if not instance.data["isHero"]:
19
self.log.warning("Something is not right.. aborting")
20
raise Exception("%s must be a hero" % instance)
21
22
class ArchiveValidation(pyblish.api.ContextPlugin):
23
# Run after all validators have finished
24
order = pyblish.api.ValidatorOrder + 0.1
25
26
def process(self, context):
27
formatted_results = self.format_results(context)
28
29
# Compute output directory
30
date = datetime.datetime.today().ctime()
31
date = pyblish.api.format_filename(date)
32
output_dir = os.path.join(os.path.expanduser("~"), "logs")
33
output_path = os.path.join(output_dir, date + ".txt")
34
35
# Write to disk
36
if not os.path.exists(output_dir):
37
os.makedirs(output_dir)
38
39
with open(output_path, "w") as f:
40
# E.g. c:\users\marcus\Documents\logs\20150612-110000.txt
41
f.write(formatted_results)
42
43
# Print rather than log, as this plug-in
44
# won't be included in the results.
45
print("Outputted to: %s" % output_path)
46
47
def format_results(self, context):
48
header = "{:<10}{:<40} -> {}".format("Success", "Plug-in", "Instance")
49
result = "{success:<10}{plugin.__name__:<40} -> {instance}"
50
error = "{:<10}+-- EXCEPTION: {:<70}"
51
record = "{:<10}+-- {level}: {message:<70}"
52
53
results = list()
54
for r in context.data["results"]:
55
# Format summary
56
results.append(result.format(**r))
57
58
# Format log records
59
for lr in r["records"]:
60
results.append(record.format("", level=lr.levelname, message=lr.msg))
61
62
# Format exception (if any)
63
if r["error"]:
64
results.append(error.format("", r["error"]))
65
66
report = """
67
{header}
68
{line}
69
{results}
70
"""
71
72
return report.format(
73
header=header,
74
results="\n".join(results),
75
line="-" * 70)
76
77
78
pyblish.api.register_plugin(CollectCaptainAmerica)
79
pyblish.api.register_plugin(ValidateCaptainAmerica)
80
pyblish.api.register_plugin(ArchiveValidation)
81
82
import pyblish.util
83
pyblish.util.publish()
Copied!
And the results can be found in your home directory.
Last modified 1yr ago
Copy link