/
swf2svg.py
51 lines (37 loc) · 1.71 KB
/
swf2svg.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
import argparse
from swf.movie import SWF
from swf.export import SVGExporter, SingleShapeSVGExporterMixin, FrameSVGExporterMixin, NamesSVGExporterMixin
parser = argparse.ArgumentParser(description="Convert an SWF file into an SVG")
parser.add_argument("--swf", type=argparse.FileType('rb'),
help="Location of SWG file to convert", required=True)
parser.add_argument("--svg", type=argparse.FileType('wb'),
help="Location of converted SVG file", required=True)
parser.add_argument("--shape", type=int,
help="Only export shape SHAPE (integer)", required=False)
parser.add_argument("--frame", type=int,
help="Export frame FRAME (0-based index) instead of frame 0", required=False)
parser.add_argument("--names", action='store_true',
help='For each element, extract SWF instanceName to class="n-<name>"', required=False)
options = parser.parse_args()
argparse.swf_file = options.swf
# load and parse the SWF
swf = SWF(options.swf)
export_opts = {}
export_mixins = []
# process the optional arguments
if options.shape is not None:
export_mixins.append(SingleShapeSVGExporterMixin)
export_opts['shape'] = options.shape
if options.frame is not None:
export_mixins.append(FrameSVGExporterMixin)
export_opts['frame'] = options.frame
if options.names:
export_mixins.append(NamesSVGExporterMixin)
# create the SVG exporter
svg_exporter = SVGExporter()
# NB: Construct the class dynamically, since the chosen options dictate which mixins to use.
svg_exporter.__class__ = type('ThisExporter', tuple(export_mixins + [SVGExporter]), {})
# export!
svg = svg_exporter.export(swf, **export_opts)
# save the SVG
options.svg.write(svg.read())