This repository has been archived by the owner on Nov 21, 2018. It is now read-only.
/
app.py
182 lines (157 loc) · 5.94 KB
/
app.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
# Standard imports
import os
import cgi
import json
import shutil
import urllib
import zipfile
import tempfile
# Report Lab
import reportlab.lib.styles
from reportlab.lib.units import inch
from reportlab.lib import utils, colors
from reportlab.lib.pagesizes import letter
from reportlab.platypus import Table, Paragraph, Image, Frame
from reportlab.platypus import TableStyle, FrameBreak
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
# Shotgun API
import shotgun_api3
def get_image(path, max_width, max_height):
if (path is None):
styles = reportlab.lib.styles.getSampleStyleSheet()
return Paragraph('', styles['BodyText'])
img = utils.ImageReader(path)
img_width, img_height = img.getSize()
if (img_width > max_width):
ratio = float(max_width) / float(img_width)
img_height = img_height * ratio
img_width = max_width
if (img_height > max_height):
ratio = float(max_height) / float(img_height)
img_width = img_width * ratio
img_height = max_height
return Image(path, width=img_width, height=img_height)
def report(fname, shot, versions):
# pdf properties
title = "Report"
author = "Rob Blau"
# define frames for page layout
page = PageTemplate(frames=[
Frame(x1=0.1*inch, y1=8.9*inch, width=3*inch, height=2*inch), # shot thumbnail
Frame(x1=0.1*inch, y1=0.1*inch, width=8.3*inch, height=4*inch), # versions table
])
# create the doc
doc = BaseDocTemplate(
fname,
pagesize=letter,
pageTemplates=page,
title=title,
author=author,
)
# styles
styles = reportlab.lib.styles.getSampleStyleSheet()
# content
story = []
# shot thumbnail
data = [['Shot Thumbnail'], [get_image(shot['image'], 3*inch, 1.5*inch)]]
shot_thumbnail = Table(data, colWidths=3*inch)
shot_thumbnail.setStyle(TableStyle([
('BOX', (0,0), (-1,-1), 0.25, colors.black),
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('TEXTCOLOR', (0,0), (0,0), colors.white),
('BACKGROUND', (0,0), (0,0), colors.black),
('ALIGN', (0,1), (0,1), 'CENTER'),
]))
story.append(shot_thumbnail)
story.append(FrameBreak())
# versions grid
data = [
['Versions', '', '', ''],
[Paragraph('<b>%s</b>' % t, styles['BodyText']) for t in ['Status', 'Name', 'Notes', 'Frame Range']],
]
for v in versions:
data.append([
Paragraph(v['sg_status_list'] or '', styles['BodyText']),
Paragraph(v['code'] or '', styles['BodyText']),
Paragraph(v['description'] or '', styles['BodyText']),
Paragraph(v['frame_range'] or '', styles['BodyText']),
])
version_table = Table(data)
version_table.setStyle(TableStyle([
('BOX', (0,0), (-1,-1), 0.25, colors.black),
('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
('TEXTCOLOR', (0,0), (0,0), colors.white),
('BACKGROUND', (0,0), (0,0), colors.black),
('SPAN', (0,0), (-1, 0)),
]))
story.append(version_table)
story.append(FrameBreak)
# and save
doc.build(story)
class App(object):
def __call__(self, environ, start_response):
# parse params
params = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
shot_ids = [int(i) for i in params.getvalue('selected_ids', []).split(',')]
server_hostname = params.getvalue('server_hostname')
# connect to Shotgun
sg = shotgun_api3.shotgun.Shotgun(
'https://%s' % server_hostname,
os.environ['SHOTGUN_SCRIPT_NAME'],
os.environ['SHOTGUN_SCRIPT_KEY'],
)
# grab shots
filter = {
'logical_operator': 'or',
'conditions': [{'path': 'id', 'relation': 'is', 'values': [id]} for id in shot_ids],
}
fields = ['sequence.Sequence.code', 'image', 'code']
shots = sg.find('Shot', filter, fields=fields)
# grab versions
filter = {
'logical_operator': 'or',
'conditions': [{'path': 'entity', 'relation': 'is', 'values': [shot]} for shot in shots],
}
fields = ['code', 'image', 'entity', 'description', 'frame_range', 'sg_status_list']
versions = sg.find('Version', filter, fields=fields)
versions_by_shot = dict()
for version in versions:
versions_by_shot.setdefault(version['entity']['id'], []).append(version)
tmpdir = tempfile.mkdtemp(prefix='report')
old_dir = os.getcwd()
os.chdir(tmpdir)
try:
shot_pdfs = []
for shot in shots:
# grab thumbnails
tmp_thumb = '%s.jpg' % shot['code']
if shot['image'] is not None:
urllib.urlretrieve(shot['image'], tmp_thumb)
shot['image'] = tmp_thumb
# create pdf frames
tmpfile = '%s.pdf' % shot['code']
report(tmpfile, shot, versions_by_shot[shot['id']])
shot_pdfs.append(tmpfile)
# zip them up
tmpzip = 'reports.zip'
archive = zipfile.ZipFile(tmpzip, mode='w')
archive.comment = 'This is a report.'
for shot_pdf in shot_pdfs:
archive.write(shot_pdf, compress_type=zipfile.ZIP_DEFLATED)
archive.close()
# start response
status = '200 OK'
response_headers = [
('Content-Type', 'application/zip'),
('Content-Disposition', 'attachment; filename="reports.zip"'),
]
start_response(status, response_headers)
f_obj = open(tmpzip, "rb")
if 'wsgi.file_wrapper' in environ:
return environ['wsgi.file_wrapper'](f_obj, 1024)
else:
return iter(lambda: f_obj.read(1024), '')
finally:
os.chdir(old_dir)
shutil.rmtree(tmpdir)
app = App()