1# usage:
2#    droid.py -d <directory>
3#
4#    <directory> is a folder, with subfolders, containing .svg files.  In each svg file in the directory or its children
5#    replace 'Droid Sans' with DroidSans
6
7import getopt, sys, os, re
8
9def usage():
10    print """
11usage:
12    droid.py -d [directory]
13
14    directory is a folder containing .svg files.
15    In each svg file in the directory or its subfolders,
16    replace 'Droid Sans' with DroidSans.
17    """
18
19
20
21def main():
22    try:
23        opts, args = getopt.getopt(sys.argv[1:], "hd:", ["help", "directory"])
24    except getopt.GetoptError, err:
25        # print help information and exit:
26        print str(err) # will print something like "option -a not recognized"
27        usage()
28        sys.exit(2)
29    outputDir = None
30
31    for o, a in opts:
32        #print o
33        #print a
34        if o in ("-d", "--directory"):
35            outputDir = a
36        elif o in ("-h", "--help"):
37            usage()
38            sys.exit(2)
39        else:
40            assert False, "unhandled option"
41
42    if(not(outputDir)):
43        usage()
44        sys.exit(2)
45
46
47    for root, dirs, files in os.walk(outputDir, topdown=False):
48        for filename in files:
49            if (filename.endswith(".svg")):
50                infile = open(os.path.join(root, filename), "r")
51                svg = infile.read();
52                infile.close();
53                svg1 = svg.replace("'Droid Sans'", "DroidSans");
54                svg2 = svg1.replace("Droid Sans", "DroidSans");
55                svg3 = svg2.replace("'DroidSans'", "DroidSans");
56
57                if (len(svg3) != len(svg)):
58                    outfile = open(os.path.join(root, filename), "w")
59                    outfile.write(svg3);
60                    outfile.close()
61                    print "{0}".format(os.path.join(root, filename))
62
63
64
65
66if __name__ == "__main__":
67    main()
68
69