-
Notifications
You must be signed in to change notification settings - Fork 17
/
cdl_convert.py
executable file
·287 lines (244 loc) · 10.6 KB
/
cdl_convert.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python
"""
CDL Convert Cdl_Convert
=======================
Contains the script functions for using cdl_convert as a script.
## Public Functions
parse_args()
Uses argparse to parse the command line args provided to cdl_convert.
main()
Main script runner, this calls parse_args, determines the input and
output extensions, calls the correct parser and determines how to
fulfill the output requested.
## License
The MIT License (MIT)
cdl_convert
Copyright (c) 2015 Sean Wallitsch
http://github.com/shidarin/cdl_convert/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
# ==============================================================================
# IMPORTS
# ==============================================================================
from __future__ import absolute_import, print_function
# Standard Imports
from argparse import ArgumentParser
import os
# cdl_convert imports
from . import config, parse, write
from .collection import ColorCollection
from .utils import sanity_check
# Python 3 compatibility
try:
xrange
except NameError: # pragma: no cover
xrange = range # pylint: disable=W0622, C0103
try:
raw_input
except NameError: # pragma: no cover
raw_input = input # pylint: disable=W0622, C0103
# ==============================================================================
# EXPORTS
# ==============================================================================
__all__ = []
# ==============================================================================
# PUBLIC FUNCTIONS
# ==============================================================================
def parse_args():
"""Uses argparse to parse command line arguments"""
parser = ArgumentParser()
parser.add_argument(
"input_file",
help="the file to be converted"
)
parser.add_argument(
"-i",
"--input",
help="specify the filetype to convert from. Use when CDLConvert "
"cannot determine the filetype automatically. Supported input " # pylint: disable=C0330
"formats are: " # pylint: disable=C0330
"{inputs}".format(inputs=str(parse.INPUT_FORMATS.keys())) # pylint: disable=C0330
)
parser.add_argument(
"-o",
"--output",
help="specify the filetype to convert to, comma separated lists are "
"accepted. Defaults to a .cc XML. Supported output formats are: " # pylint: disable=C0330
"{outputs}".format(outputs=str(write.OUTPUT_FORMATS.keys())) # pylint: disable=C0330
)
parser.add_argument(
"-d",
"--destination",
help="specify an output directory to save converted files to. If not "
"provided will default to ./converted/" # pylint: disable=C0330
)
parser.add_argument(
"--halt",
action='store_true',
help="turns off exception handling default behavior. Turn this on if "
"you want the conversion process to fail and not continue," # pylint: disable=C0330
"rather than relying on default behavior for bad values. Examples " # pylint: disable=C0330
"are clipping negative values to 0.0 for Slope, Power and " # pylint: disable=C0330
"Saturation, and automatically generating a new id for a " # pylint: disable=C0330
"ColorCorrect if no or a bad id is given." # pylint: disable=C0330
)
parser.add_argument(
"--no-output",
action='store_true',
help="parses all incoming files but no files will be written. Use this "
"in conjunction with '--halt' and '--check' to try and " # pylint: disable=C0330
"track down any oddities observed in the CDLs." # pylint: disable=C0330
)
parser.add_argument(
"--check",
action='store_true',
help="checks all ColorCorrects that were parsed for odd values. Odd "
"values are any values over 3 or under 0.1 for Slope, Power " # pylint: disable=C0330
"and Saturation. For offset, any value over 1 and under -1 is " # pylint: disable=C0330
"flagged. Note that depending on the look, these still might " # pylint: disable=C0330
"be correct values." # pylint: disable=C0330
)
parser.add_argument(
"--single",
action='store_true',
help="only write a single color decision per file when given collection "
"formats. This means that a single input CDL will export multiple "
"CDL files, one per color decision."
)
args = parser.parse_args()
if args.input:
if args.input.lower() not in parse.INPUT_FORMATS:
raise ValueError(
"The input format: {input} is not supported".format(
input=args.input
)
)
else:
args.input = args.input.lower()
if args.output:
# This might be a string separated list of output types.
# We'll split it, check each against the supported types, convert
# them to lowercase if not already, and place the resulting list back
# into args.output
#
# TODO: Define and add a new argparse type as described in:
# http://stackoverflow.com/questions/9978880/python-argument-parser-list-of-list-or-tuple-of-tuples
output_types = args.output.split(',')
for i in xrange(len(output_types)):
if output_types[i].lower() not in write.OUTPUT_FORMATS.keys():
raise ValueError(
"The output format: {output} is not supported".format(
output=output_types[i]
)
)
else:
output_types[i] = output_types[i].lower()
args.output = output_types
else:
args.output = ['cc', ]
if not args.destination:
args.destination = './converted/'
if args.halt:
config.HALT_ON_ERROR = True
return args
# ==============================================================================
# MAIN
# ==============================================================================
def main(): # pylint: disable=R0912
"""Will figure out input and destination filetypes, then convert"""
args = parse_args()
if args.no_output:
print("Dry run initiated, no files will be written.")
filepath = os.path.abspath(args.input_file)
destination_dir = os.path.abspath(args.destination)
if not os.path.exists(destination_dir):
print(
"Destination directory {dir} does not exist.".format(
dir=destination_dir
)
)
if not args.no_output:
print("Creating destination directory.")
os.makedirs(destination_dir)
else:
print("--no-output argument provided. Skipping directory creation")
if not args.input:
filetype_in = os.path.basename(filepath).split('.')[-1].lower()
else:
filetype_in = args.input
color_decisions = parse.parse_file(filepath, filetype_in)
def write_single_file(cdl, ext):
"""Writes a single color correction file"""
cdl.determine_dest(ext, destination_dir)
print(
"Writing cdl {id} to {path}".format(
id=cdl.id,
path=cdl.file_out
)
)
if not args.no_output:
write.OUTPUT_FORMATS[ext](cdl)
def write_collection_file(col, ext):
"""Writes a collection file"""
col.type = ext
col.determine_dest(destination_dir)
print(
"Writing collection to {path}".format(
path=col.file_out
)
)
if not args.no_output:
write.OUTPUT_FORMATS[ext](col)
if color_decisions:
# Sanity Check
if args.check:
if filetype_in in config.COLLECTION_FORMATS:
for color_correct in color_decisions.color_corrections:
sanity_check(color_correct)
for decision in color_decisions.color_decisions:
if not decision.is_ref:
sanity_check(decision.cc)
else:
sanity_check(color_decisions)
# Writing
for ext in args.output:
if ext in config.SINGLE_FORMATS or args.single:
if filetype_in in config.COLLECTION_FORMATS:
for color_correct in color_decisions.color_corrections:
write_single_file(color_correct, ext)
for decision in color_decisions.color_decisions:
if not decision.is_ref:
write_single_file(decision.cc, ext)
else:
write_single_file(color_decisions, ext)
else:
if filetype_in in config.COLLECTION_FORMATS:
# If we read a collection type, color_decisions is
# already a ColorCollection.
write_collection_file(color_decisions, ext)
else:
# If we read a single, non-collection file, we need to
# create a collection for exporting.
#
# Since we only read a single file, we can safely use that
# filepath as the input_file.
#
# If we read a group of files, we would want to default to
# the generic collection naming.
collection = ColorCollection(input_file=filepath)
collection.append_child(color_decisions)
write_collection_file(collection, ext)