~tiheum/ubuntu-themes/bug-sprint-082015

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
#!/usr/bin/env python
# Copyright 2011 Canonical Ltd.  Distributed under the terms of the GNU GPLv3.
# Combines multiple SVG files into a single SVG in batch mode
# 2011-01-04 Mathieu Trudel-Lapierre <mathieu.trudel-lapierre@canonical.com>
# 2011-01-05 Paul Sladen <sladen@canonica.com>

import xml.dom.minidom
import sys
import optparse

def commandline():
    # Parse commandline
    parser = optparse.OptionParser(usage = '%prog [options] INPUT.svg [MOREINPUT.svg] -o OUTPUT.svg')
    parser.add_option("-n", "--dry-run", dest = "do_write",
                      default = True, action = "store_false",
                      help = "dry run: do not write output file, just test")
    parser.add_option("-o", "--output", dest = "output",
                      help = "full output path for processed SVG file")
    options, args = parser.parse_args()

    # Validate
    if len(args) < 1:
        parser.error("one or more input SVG files required")
    if options.do_write and not options.output:
        parser.error("SVG output filename required")

    return options, args

def main():
    options, args = commandline()

    # Open first SVG and then composite others on top as overlays
    base = xml.dom.minidom.parse(args[0])
    newline = lambda: base.createTextNode('\n')

    for overlay in args[1:]:
        comment = base.createComment(" imported from '%s' " % overlay)
        dom = xml.dom.minidom.parse(overlay)

        # Pull-in '<svg>' tag, clear attributes and rename to '<g>'
        g = base.importNode(dom.getElementsByTagName("svg")[0], deep = True)
        [g.removeAttribute(k) for k in g.attributes.keys() if k != 'id']
        g.tagName = 'g'

        # Insert into first SVG with comments and spacing
        for node in newline(), comment, newline(), g, newline():
            base.documentElement.appendChild(node)

    if options.do_write:
        base.writexml(open(options.output, "w"))

if __name__=='__main__':
    main()