annotate Resources/EmbedResources.py @ 2212:c02f6ec404d8

OrthancPlugins::FindMatcher
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 10 Dec 2016 10:05:16 +0100
parents 8453d3a26421
children a3a65de1840f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1467
5068de14eef1 Inject version information into Windows binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1377
diff changeset
1 #!/usr/bin/python
5068de14eef1 Inject version information into Windows binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1377
diff changeset
2
143
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
3 # Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1584
diff changeset
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1204
diff changeset
5 # Department, University Hospital of Liege, Belgium
143
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
6 #
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
7 # This program is free software: you can redistribute it and/or
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
8 # modify it under the terms of the GNU General Public License as
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
9 # published by the Free Software Foundation, either version 3 of the
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
10 # License, or (at your option) any later version.
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
11 #
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
12 # In addition, as a special exception, the copyright holders of this
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
13 # program give permission to link the code of its release with the
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
14 # OpenSSL project's "OpenSSL" library (or with modified versions of it
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
15 # that use the same license as the "OpenSSL" library), and distribute
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
16 # the linked executables. You must obey the GNU General Public License
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
17 # in all respects for all of the code used other than "OpenSSL". If you
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
18 # modify file(s) with this exception, you may extend this exception to
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
19 # your version of the file(s), but you are not obligated to do so. If
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
20 # you do not wish to do so, delete this exception statement from your
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
21 # version. If you delete this exception statement from all source files
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
22 # in the program, then also delete it here.
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
23 #
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
24 # This program is distributed in the hope that it will be useful, but
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
25 # WITHOUT ANY WARRANTY; without even the implied warranty of
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
27 # General Public License for more details.
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
28 #
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
29 # You should have received a copy of the GNU General Public License
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
30 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
31
f1aeec53795b missing copyright
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 129
diff changeset
32
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 import sys
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 import os
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 import os.path
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 import pprint
129
5133cfc8db86 fix standalone build
jodogne
parents: 64
diff changeset
37 import re
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
325
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
39 UPCASE_CHECK = True
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
40 USE_SYSTEM_EXCEPTION = False
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
41 EXCEPTION_CLASS = 'OrthancException'
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
42 OUT_OF_RANGE_EXCEPTION = 'OrthancException(ErrorCode_ParameterOutOfRange)'
1584
39ecd34fb1c7 fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1469
diff changeset
43 INEXISTENT_PATH_EXCEPTION = 'OrthancException(ErrorCode_InexistentItem)'
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
44 NAMESPACE = 'Orthanc'
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
45
325
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
46 ARGS = []
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
47 for i in range(len(sys.argv)):
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
48 if not sys.argv[i].startswith('--'):
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
49 ARGS.append(sys.argv[i])
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
50 elif sys.argv[i].lower() == '--no-upcase-check':
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
51 UPCASE_CHECK = False
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
52 elif sys.argv[i].lower() == '--system-exception':
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
53 USE_SYSTEM_EXCEPTION = True
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
54 EXCEPTION_CLASS = '::std::runtime_error'
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
55 OUT_OF_RANGE_EXCEPTION = '%s("Parameter out of range")' % EXCEPTION_CLASS
1584
39ecd34fb1c7 fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1469
diff changeset
56 INEXISTENT_PATH_EXCEPTION = '%s("Unknown path in a directory resource")' % EXCEPTION_CLASS
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
57 elif sys.argv[i].startswith('--namespace='):
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
58 NAMESPACE = sys.argv[i][sys.argv[i].find('=') + 1 : ]
325
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
59
326
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 325
diff changeset
60 if len(ARGS) < 2 or len(ARGS) % 2 != 0:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 325
diff changeset
61 print ('Usage:')
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
62 print ('python %s [--no-upcase-check] [--system-exception] [--namespace=<Namespace>] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0])
326
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 325
diff changeset
63 exit(-1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 325
diff changeset
64
325
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
65 TARGET_BASE_FILENAME = ARGS[1]
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
66 SOURCES = ARGS[2:]
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 try:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 # Make sure the destination directory exists
325
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
70 os.makedirs(os.path.normpath(os.path.join(TARGET_BASE_FILENAME, '..')))
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 except:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 pass
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 ## Read each resource file
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78
129
5133cfc8db86 fix standalone build
jodogne
parents: 64
diff changeset
79 def CheckNoUpcase(s):
325
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
80 global UPCASE_CHECK
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
81 if (UPCASE_CHECK and
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
82 re.search('[A-Z]', s) != None):
129
5133cfc8db86 fix standalone build
jodogne
parents: 64
diff changeset
83 raise Exception("Path in a directory with an upcase letter: %s" % s)
5133cfc8db86 fix standalone build
jodogne
parents: 64
diff changeset
84
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 resources = {}
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 counter = 0
325
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
88 i = 0
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
89 while i < len(SOURCES):
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
90 resourceName = SOURCES[i].upper()
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
91 pathName = SOURCES[i + 1]
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 if not os.path.exists(pathName):
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 raise Exception("Non existing path: %s" % pathName)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 if resourceName in resources:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 raise Exception("Twice the same resource: " + resourceName)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 if os.path.isdir(pathName):
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 # The resource is a directory: Recursively explore its files
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 content = {}
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 for root, dirs, files in os.walk(pathName):
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 base = os.path.relpath(root, pathName)
1204
6babe0c7aa27 Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
104
6babe0c7aa27 Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
105 # Fix issue #24 (Build fails on OSX when directory has .DS_Store files):
6babe0c7aa27 Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
106 # Ignore folders whose name starts with a dot (".")
6babe0c7aa27 Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
107 if base.find('/.') != -1:
6babe0c7aa27 Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
108 print('Ignoring folder: %s' % root)
6babe0c7aa27 Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
109 continue
6babe0c7aa27 Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
110
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 for f in files:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 if f.find('~') == -1: # Ignore Emacs backup files
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 if base == '.':
129
5133cfc8db86 fix standalone build
jodogne
parents: 64
diff changeset
114 r = f
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 else:
129
5133cfc8db86 fix standalone build
jodogne
parents: 64
diff changeset
116 r = os.path.join(base, f)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
129
5133cfc8db86 fix standalone build
jodogne
parents: 64
diff changeset
118 CheckNoUpcase(r)
20
a56b6a8b89ba fix for windows
jodogne
parents: 0
diff changeset
119 r = '/' + r.replace('\\', '/')
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 if r in content:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 raise Exception("Twice the same filename (check case): " + r)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 content[r] = {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 'Filename' : os.path.join(root, f),
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 'Index' : counter
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 counter += 1
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 resources[resourceName] = {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 'Type' : 'Directory',
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 'Files' : content
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 elif os.path.isfile(pathName):
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 resources[resourceName] = {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 'Type' : 'File',
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 'Index' : counter,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 'Filename' : pathName
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 counter += 1
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 else:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 raise Exception("Not a regular file, nor a directory: " + pathName)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 i += 2
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 #pprint.pprint(resources)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 ## Write .h header
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153
325
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
154 header = open(TARGET_BASE_FILENAME + '.h', 'w')
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 header.write("""
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 #pragma once
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 #include <string>
323
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
160 #include <list>
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161
2174
8453d3a26421 disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
162 #if defined(_MSC_VER)
8453d3a26421 disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
163 # pragma warning(disable: 4065) // "Switch statement contains 'default' but no 'case' labels"
8453d3a26421 disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
164 #endif
8453d3a26421 disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
165
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
166 namespace %s
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 namespace EmbeddedResources
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 enum FileResourceId
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 {
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
172 """ % NAMESPACE)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 isFirst = True
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 for name in resources:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 if resources[name]['Type'] == 'File':
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 if isFirst:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 isFirst = False
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 else:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 header.write(',\n')
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 header.write(' %s' % name)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 header.write("""
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 enum DirectoryResourceId
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 """)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 isFirst = True
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 for name in resources:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 if resources[name]['Type'] == 'Directory':
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 if isFirst:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 isFirst = False
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 else:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 header.write(',\n')
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 header.write(' %s' % name)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 header.write("""
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 const void* GetFileResourceBuffer(FileResourceId id);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 size_t GetFileResourceSize(FileResourceId id);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 void GetFileResource(std::string& result, FileResourceId id);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path);
323
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
209
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
210 void ListResources(std::list<std::string>& result, DirectoryResourceId id);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 """)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 header.close()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 ## Write the resource content in the .cpp source
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221
55
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 49
diff changeset
222 PYTHON_MAJOR_VERSION = sys.version_info[0]
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 49
diff changeset
223
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 def WriteResource(cpp, item):
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index'])
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 f = open(item['Filename'], "rb")
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 content = f.read()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 f.close()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 # http://stackoverflow.com/a/1035360
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 pos = 0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 for b in content:
55
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 49
diff changeset
234 if PYTHON_MAJOR_VERSION == 2:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 49
diff changeset
235 c = ord(b[0])
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 49
diff changeset
236 else:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 49
diff changeset
237 c = b
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 49
diff changeset
238
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 if pos > 0:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 cpp.write(', ')
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 if (pos % 16) == 0:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 cpp.write('\n ')
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
55
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 49
diff changeset
245 if c < 0:
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 raise Exception("Internal error")
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247
55
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 49
diff changeset
248 cpp.write("0x%02x" % c)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 pos += 1
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250
1377
601d34afdab9 patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
251 # Zero-size array are disallowed, so we put one single void character in it.
601d34afdab9 patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
252 if pos == 0:
601d34afdab9 patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
253 cpp.write(' 0')
601d34afdab9 patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
254
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 cpp.write(' };\n')
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos))
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258
325
71d30b9ce801 ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 323
diff changeset
259 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w')
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
261 cpp.write('#include "%s.h"\n' % os.path.basename(TARGET_BASE_FILENAME))
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
262
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
263 if USE_SYSTEM_EXCEPTION:
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
264 cpp.write('#include <stdexcept>')
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
265 else:
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
266 cpp.write('#include "%s/Core/OrthancException.h"' % os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
267
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 cpp.write("""
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 #include <stdint.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 #include <string.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
272 namespace %s
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 namespace EmbeddedResources
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 {
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
276 """ % NAMESPACE)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 for name in resources:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 if resources[name]['Type'] == 'File':
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 WriteResource(cpp, resources[name])
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 else:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 for f in resources[name]['Files']:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 WriteResource(cpp, resources[name]['Files'][f])
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 ## Write the accessors to the file resources in .cpp
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 cpp.write("""
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 const void* GetFileResourceBuffer(FileResourceId id)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 switch (id)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 """)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 for name in resources:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 if resources[name]['Type'] == 'File':
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 cpp.write(' case %s:\n' % name)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index'])
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 cpp.write("""
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 default:
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
305 throw %s;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 size_t GetFileResourceSize(FileResourceId id)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 switch (id)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 {
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
313 """ % OUT_OF_RANGE_EXCEPTION)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 for name in resources:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 if resources[name]['Type'] == 'File':
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 cpp.write(' case %s:\n' % name)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 cpp.write(' return resource%dSize;\n' % resources[name]['Index'])
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 cpp.write("""
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 default:
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
322 throw %s;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 }
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
325 """ % OUT_OF_RANGE_EXCEPTION)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 ## Write the accessors to the directory resources in .cpp
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 cpp.write("""
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336 switch (id)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 """)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 for name in resources:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 if resources[name]['Type'] == 'Directory':
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 cpp.write(' case %s:\n' % name)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 isFirst = True
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 for path in resources[name]['Files']:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 cpp.write(' if (!strcmp(path, "%s"))\n' % path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index'])
1584
39ecd34fb1c7 fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1469
diff changeset
347 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 cpp.write(""" default:
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
350 throw %s;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 switch (id)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 {
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
358 """ % OUT_OF_RANGE_EXCEPTION)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 for name in resources:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 if resources[name]['Type'] == 'Directory':
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 cpp.write(' case %s:\n' % name)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 isFirst = True
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364 for path in resources[name]['Files']:
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 cpp.write(' if (!strcmp(path, "%s"))\n' % path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index'])
1584
39ecd34fb1c7 fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1469
diff changeset
367 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 cpp.write(""" default:
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
370 throw %s;
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 }
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
373 """ % OUT_OF_RANGE_EXCEPTION)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 #####################################################################
323
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
379 ## List the resources in a directory
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
380 #####################################################################
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
381
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
382 cpp.write("""
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
383 void ListResources(std::list<std::string>& result, DirectoryResourceId id)
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
384 {
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
385 result.clear();
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
386
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
387 switch (id)
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
388 {
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
389 """)
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
390
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
391 for name in resources:
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
392 if resources[name]['Type'] == 'Directory':
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
393 cpp.write(' case %s:\n' % name)
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
394 for path in sorted(resources[name]['Files']):
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
395 cpp.write(' result.push_back("%s");\n' % path)
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
396 cpp.write(' break;\n\n')
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
397
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
398 cpp.write(""" default:
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
399 throw %s;
323
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
400 }
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
401 }
1469
bf9b8bfea0e7 more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1467
diff changeset
402 """ % OUT_OF_RANGE_EXCEPTION)
323
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
403
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
404
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
405
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
406
052dede32761 list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 143
diff changeset
407 #####################################################################
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 ## Write the convenience wrappers in .cpp
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 #####################################################################
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 cpp.write("""
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412 void GetFileResource(std::string& result, FileResourceId id)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 size_t size = GetFileResourceSize(id);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 result.resize(size);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 if (size > 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417 memcpy(&result[0], GetFileResourceBuffer(id), size);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422 size_t size = GetDirectoryResourceSize(id, path);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 result.resize(size);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 if (size > 0)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 """)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 cpp.close()