OSDN Git Service

1e346fb9b9cf8859a615afc0d6b5fa269cd5d810
[handbrake-jp/handbrake-jp-git.git] / gtk / src / create_resources.py
1 #! /bin/python
2 #
3
4 import gtk
5 import types
6 import os
7 import sys
8 import time
9 import datetime
10 import plistlib
11 import getopt
12 from xml.parsers import expat
13
14
15 pl = dict()
16 stack = list()
17 stack.append(pl)
18
19 def top(ss):
20         return ss[len(ss)-1]
21
22 def end_element_handler(tag):
23         global stack
24
25         if tag == "section":
26                 stack.pop()
27
28 def start_element_handler(tag, attr):
29         global pl, stack
30
31         current = top(stack)
32         key = val = None
33         if tag == "section":
34                 key = attr["name"]
35                 if key == "icons":
36                         val = dict()
37                         stack.append(val)
38         elif tag == "icon":
39                 fname = attr["file"]
40                 fname = find_file(fname)
41                 key = attr["name"]
42                 if fname != None and key != None:
43                         val = dict()
44                         pb = gtk.gdk.pixbuf_new_from_file(fname)
45                         val["colorspace"] = pb.get_colorspace()
46                         val["alpha"] = pb.get_has_alpha()
47                         val["bps"] = pb.get_bits_per_sample()
48                         val["width"] = pb.get_width()
49                         val["height"] = pb.get_height()
50                         val["rowstride"] = pb.get_rowstride()
51                         val["data"] = plistlib.Data(pb.get_pixels())
52         elif tag == "plist":
53                 fname = attr["file"]
54                 fname = find_file(fname)
55                 key = attr["name"]
56                 if fname != None and key != None:
57                         val = plistlib.readPlist(fname)
58         elif tag == "string":
59                 fname = attr["file"]
60                 fname = find_file(fname)
61                 key = attr["name"]
62                 if fname != None and key != None:
63                         try:
64                                 ff = open(fname)
65                                 val = ff.read()
66                         except Exception, err:
67                                 print >> sys.stderr, ( "Error: %s"  % str(err) )
68                 
69         if val != None:
70                 if type(current) == types.DictType:
71                         current[key] = val
72                 elif type(current) == types.TupleType:
73                         current.append(val)
74
75
76 def cdata_handler(str):
77         return
78
79 def resource_parse_file(infile):
80         parser = expat.ParserCreate()
81         parser.StartElementHandler = start_element_handler
82         parser.EndElementHandler = end_element_handler
83         parser.CharacterDataHandler = cdata_handler
84         parser.ParseFile(infile)
85
86 def usage():
87         print >> sys.stderr, (
88                 "Usage: %s [-I <inc path>] <resource list> [resource plist]\n"
89                 "Summary:\n"
90                 "    Creates a resource plist from a resource list\n\n"
91                 "Options:\n"
92                 "    I - Include path to search for files\n"
93                 "    <resource list>    Input resources file\n"
94                 "    <resource plist>  Output resources plist file\n"
95                 % sys.argv[0]
96         )
97
98 inc_list = list()
99
100 def find_file(name):
101         global inc_list
102
103         for inc_dir in inc_list:
104                 inc = "%s/%s" % inc_dir, name
105                 if os.path.isfile(inc):
106                         return inc
107
108         if os.path.isfile(name):
109                 return name
110
111         return None
112
113 def main():
114         global inc_list
115
116         OPTS = "I:"
117         try:
118                 opts, args = getopt.gnu_getopt(sys.argv[1:], OPTS)
119         except getopt.GetoptError, err:
120                 print >> sys.stderr, str(err)
121                 usage()
122                 sys.exit(2)
123
124         for o, a in opts:
125                 if o == "-I":
126                         # add to include list
127                         inc_list.append(a)
128                 else:
129                         assert False, "unhandled option"
130
131         if len(args) > 2 or len(args) < 1:
132                 usage()
133                 sys.exit(2)
134
135         try:
136                 infile = open(args[0])
137         except Exception, err:
138                 print >> sys.stderr, ( "Error: %s"  % str(err) )
139                 sys.exit(1)
140
141         try:
142                 infile = open(args[0])
143         except Exception, err:
144                 print >> sys.stderr, ( "Error: %s"  % str(err) )
145                 sys.exit(1)
146
147         if len(args) > 1:
148                 try:
149                         outfile = open(args[1], "w")
150                 except Exception, err:
151                         print >> sys.stderr, ( "Error: %s"  % str(err))
152                         sys.exit(1)
153         else:
154                 outfile = sys.stdout
155
156         resource_parse_file(infile)
157         plistlib.writePlist(pl, outfile)
158
159 main()
160