0.1.76
by Mathieu Trudel-Lapierre
scavenge.py, debian/install: Add and install new scavenge.py script to |
1 |
#!/usr/bin/env python
|
0.16.7
by Mathieu Trudel-Lapierre
debian/copyright: update to DEP-8 format; fix copyright instances to make |
2 |
# Copyright 2011 Canonical Ltd. Distributed under the terms of the GNU GPLv3.
|
0.1.76
by Mathieu Trudel-Lapierre
scavenge.py, debian/install: Add and install new scavenge.py script to |
3 |
# Combines multiple SVG files into a single SVG in batch mode
|
4 |
# 2011-01-04 Mathieu Trudel-Lapierre <mathieu.trudel-lapierre@canonical.com>
|
|
5 |
# 2011-01-05 Paul Sladen <sladen@canonica.com>
|
|
6 |
||
7 |
import xml.dom.minidom |
|
8 |
import sys |
|
9 |
import optparse |
|
10 |
||
11 |
def commandline(): |
|
12 |
# Parse commandline
|
|
13 |
parser = optparse.OptionParser(usage = '%prog [options] INPUT.svg [MOREINPUT.svg] -o OUTPUT.svg') |
|
14 |
parser.add_option("-n", "--dry-run", dest = "do_write", |
|
15 |
default = True, action = "store_false", |
|
16 |
help = "dry run: do not write output file, just test") |
|
17 |
parser.add_option("-o", "--output", dest = "output", |
|
18 |
help = "full output path for processed SVG file") |
|
19 |
options, args = parser.parse_args() |
|
20 |
||
21 |
# Validate
|
|
22 |
if len(args) < 1: |
|
23 |
parser.error("one or more input SVG files required") |
|
24 |
if options.do_write and not options.output: |
|
25 |
parser.error("SVG output filename required") |
|
26 |
||
27 |
return options, args |
|
28 |
||
29 |
def main(): |
|
30 |
options, args = commandline() |
|
31 |
||
32 |
# Open first SVG and then composite others on top as overlays
|
|
33 |
base = xml.dom.minidom.parse(args[0]) |
|
34 |
newline = lambda: base.createTextNode('\n') |
|
35 |
||
36 |
for overlay in args[1:]: |
|
37 |
comment = base.createComment(" imported from '%s' " % overlay) |
|
38 |
dom = xml.dom.minidom.parse(overlay) |
|
39 |
||
40 |
# Pull-in '<svg>' tag, clear attributes and rename to '<g>'
|
|
41 |
g = base.importNode(dom.getElementsByTagName("svg")[0], deep = True) |
|
42 |
[g.removeAttribute(k) for k in g.attributes.keys() if k != 'id'] |
|
43 |
g.tagName = 'g' |
|
44 |
||
45 |
# Insert into first SVG with comments and spacing
|
|
46 |
for node in newline(), comment, newline(), g, newline(): |
|
47 |
base.documentElement.appendChild(node) |
|
48 |
||
49 |
if options.do_write: |
|
50 |
base.writexml(open(options.output, "w")) |
|
51 |
||
52 |
if __name__=='__main__': |
|
53 |
main() |