Mercurial > hg > orthanc
annotate Resources/EmbedResources.py @ 64:71c4a4abe90b orthanc-renaming
renaming of resources
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sun, 16 Sep 2012 09:35:31 +0200 |
parents | 601ee9b7f2c7 |
children | 5133cfc8db86 |
rev | line source |
---|---|
0 | 1 import sys |
2 import os | |
3 import os.path | |
4 import pprint | |
5 | |
6 if len(sys.argv) < 2 or len(sys.argv) % 2 != 0: | |
7 print ('Usage:') | |
8 print ('python %s <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0]) | |
9 exit(-1) | |
10 | |
11 | |
12 try: | |
13 # Make sure the destination directory exists | |
14 os.makedirs(os.path.normpath(os.path.join(sys.argv[1], '..'))) | |
15 except: | |
16 pass | |
17 | |
18 | |
19 ##################################################################### | |
20 ## Read each resource file | |
21 ##################################################################### | |
22 | |
23 resources = {} | |
24 | |
25 counter = 0 | |
26 i = 2 | |
27 while i < len(sys.argv): | |
28 resourceName = sys.argv[i].upper() | |
29 pathName = sys.argv[i + 1] | |
30 | |
31 if not os.path.exists(pathName): | |
32 raise Exception("Non existing path: %s" % pathName) | |
33 | |
34 if resourceName in resources: | |
35 raise Exception("Twice the same resource: " + resourceName) | |
36 | |
37 if os.path.isdir(pathName): | |
38 # The resource is a directory: Recursively explore its files | |
39 content = {} | |
40 for root, dirs, files in os.walk(pathName): | |
41 base = os.path.relpath(root, pathName) | |
42 for f in files: | |
43 if f.find('~') == -1: # Ignore Emacs backup files | |
44 if base == '.': | |
45 r = f.lower() | |
46 else: | |
47 r = os.path.join(base, f).lower() | |
48 | |
20 | 49 r = '/' + r.replace('\\', '/') |
0 | 50 if r in content: |
51 raise Exception("Twice the same filename (check case): " + r) | |
52 | |
53 content[r] = { | |
54 'Filename' : os.path.join(root, f), | |
55 'Index' : counter | |
56 } | |
57 counter += 1 | |
58 | |
59 resources[resourceName] = { | |
60 'Type' : 'Directory', | |
61 'Files' : content | |
62 } | |
63 | |
64 elif os.path.isfile(pathName): | |
65 resources[resourceName] = { | |
66 'Type' : 'File', | |
67 'Index' : counter, | |
68 'Filename' : pathName | |
69 } | |
70 counter += 1 | |
71 | |
72 else: | |
73 raise Exception("Not a regular file, nor a directory: " + pathName) | |
74 | |
75 i += 2 | |
76 | |
77 #pprint.pprint(resources) | |
78 | |
79 | |
80 ##################################################################### | |
81 ## Write .h header | |
82 ##################################################################### | |
83 | |
84 header = open(sys.argv[1] + '.h', 'w') | |
85 | |
86 header.write(""" | |
87 #pragma once | |
88 | |
89 #include <string> | |
90 | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
91 namespace Orthanc |
0 | 92 { |
93 namespace EmbeddedResources | |
94 { | |
95 enum FileResourceId | |
96 { | |
97 """) | |
98 | |
99 isFirst = True | |
100 for name in resources: | |
101 if resources[name]['Type'] == 'File': | |
102 if isFirst: | |
103 isFirst = False | |
104 else: | |
105 header.write(',\n') | |
106 header.write(' %s' % name) | |
107 | |
108 header.write(""" | |
109 }; | |
110 | |
111 enum DirectoryResourceId | |
112 { | |
113 """) | |
114 | |
115 isFirst = True | |
116 for name in resources: | |
117 if resources[name]['Type'] == 'Directory': | |
118 if isFirst: | |
119 isFirst = False | |
120 else: | |
121 header.write(',\n') | |
122 header.write(' %s' % name) | |
123 | |
124 header.write(""" | |
125 }; | |
126 | |
127 const void* GetFileResourceBuffer(FileResourceId id); | |
128 size_t GetFileResourceSize(FileResourceId id); | |
129 void GetFileResource(std::string& result, FileResourceId id); | |
130 | |
131 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
132 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
133 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path); | |
134 } | |
135 } | |
136 """) | |
137 header.close() | |
138 | |
139 | |
140 | |
141 ##################################################################### | |
142 ## Write the resource content in the .cpp source | |
143 ##################################################################### | |
144 | |
55 | 145 PYTHON_MAJOR_VERSION = sys.version_info[0] |
146 | |
0 | 147 def WriteResource(cpp, item): |
148 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
149 | |
150 f = open(item['Filename'], "rb") | |
151 content = f.read() | |
152 f.close() | |
153 | |
154 # http://stackoverflow.com/a/1035360 | |
155 pos = 0 | |
156 for b in content: | |
55 | 157 if PYTHON_MAJOR_VERSION == 2: |
158 c = ord(b[0]) | |
159 else: | |
160 c = b | |
161 | |
0 | 162 if pos > 0: |
163 cpp.write(', ') | |
164 | |
165 if (pos % 16) == 0: | |
166 cpp.write('\n ') | |
167 | |
55 | 168 if c < 0: |
0 | 169 raise Exception("Internal error") |
170 | |
55 | 171 cpp.write("0x%02x" % c) |
0 | 172 pos += 1 |
173 | |
174 cpp.write(' };\n') | |
175 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
176 | |
177 | |
178 cpp = open(sys.argv[1] + '.cpp', 'w') | |
179 | |
180 cpp.write(""" | |
181 #include "%s.h" | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
182 #include "%s/Core/OrthancException.h" |
0 | 183 |
184 #include <stdint.h> | |
185 #include <string.h> | |
186 | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
187 namespace Orthanc |
0 | 188 { |
189 namespace EmbeddedResources | |
190 { | |
191 """ % (os.path.basename(sys.argv[1]), | |
192 os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))) | |
193 | |
194 | |
195 for name in resources: | |
196 if resources[name]['Type'] == 'File': | |
197 WriteResource(cpp, resources[name]) | |
198 else: | |
199 for f in resources[name]['Files']: | |
200 WriteResource(cpp, resources[name]['Files'][f]) | |
201 | |
202 | |
203 | |
204 ##################################################################### | |
205 ## Write the accessors to the file resources in .cpp | |
206 ##################################################################### | |
207 | |
208 cpp.write(""" | |
209 const void* GetFileResourceBuffer(FileResourceId id) | |
210 { | |
211 switch (id) | |
212 { | |
213 """) | |
214 for name in resources: | |
215 if resources[name]['Type'] == 'File': | |
216 cpp.write(' case %s:\n' % name) | |
217 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
218 | |
219 cpp.write(""" | |
220 default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
221 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 222 } |
223 } | |
224 | |
225 size_t GetFileResourceSize(FileResourceId id) | |
226 { | |
227 switch (id) | |
228 { | |
229 """) | |
230 | |
231 for name in resources: | |
232 if resources[name]['Type'] == 'File': | |
233 cpp.write(' case %s:\n' % name) | |
234 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
235 | |
236 cpp.write(""" | |
237 default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
238 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 239 } |
240 } | |
241 """) | |
242 | |
243 | |
244 | |
245 ##################################################################### | |
246 ## Write the accessors to the directory resources in .cpp | |
247 ##################################################################### | |
248 | |
249 cpp.write(""" | |
250 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
251 { | |
252 switch (id) | |
253 { | |
254 """) | |
255 | |
256 for name in resources: | |
257 if resources[name]['Type'] == 'Directory': | |
258 cpp.write(' case %s:\n' % name) | |
259 isFirst = True | |
260 for path in resources[name]['Files']: | |
261 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
262 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index']) | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
263 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') |
0 | 264 |
265 cpp.write(""" default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
266 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 267 } |
268 } | |
269 | |
270 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
271 { | |
272 switch (id) | |
273 { | |
274 """) | |
275 | |
276 for name in resources: | |
277 if resources[name]['Type'] == 'Directory': | |
278 cpp.write(' case %s:\n' % name) | |
279 isFirst = True | |
280 for path in resources[name]['Files']: | |
281 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
282 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index']) | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
283 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') |
0 | 284 |
285 cpp.write(""" default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
286 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 287 } |
288 } | |
289 """) | |
290 | |
291 | |
292 | |
293 | |
294 ##################################################################### | |
295 ## Write the convenience wrappers in .cpp | |
296 ##################################################################### | |
297 | |
298 cpp.write(""" | |
299 void GetFileResource(std::string& result, FileResourceId id) | |
300 { | |
301 size_t size = GetFileResourceSize(id); | |
302 result.resize(size); | |
303 if (size > 0) | |
304 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
305 } | |
306 | |
307 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
308 { | |
309 size_t size = GetDirectoryResourceSize(id, path); | |
310 result.resize(size); | |
311 if (size > 0) | |
312 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
313 } | |
314 } | |
315 } | |
316 """) | |
317 cpp.close() |