annotate Plugins/Samples/WebSkeleton/Framework/EmbedResources.py @ 1914:501769757bf9

flag to remove network support in dcmtk, removal of unneeded sources in static builds
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 09 Feb 2016 11:47:02 +0100
parents b1291df2f780
children a3a65de1840f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1181
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 # Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1390
diff changeset
2 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1181
diff changeset
3 # Department, University Hospital of Liege, Belgium
1181
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 #
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 # This program is free software: you can redistribute it and/or
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 # modify it under the terms of the GNU General Public License as
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 # published by the Free Software Foundation, either version 3 of the
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 # License, or (at your option) any later version.
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 #
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # This program is distributed in the hope that it will be useful, but
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 # General Public License for more details.
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 #
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 import sys
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 import os
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 import os.path
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 import pprint
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import re
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 UPCASE_CHECK = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 ARGS = []
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 for i in range(len(sys.argv)):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 if not sys.argv[i].startswith('--'):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 ARGS.append(sys.argv[i])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 elif sys.argv[i].lower() == '--no-upcase-check':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 UPCASE_CHECK = False
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 if len(ARGS) < 2 or len(ARGS) % 2 != 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 print ('Usage:')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 print ('python %s [--no-upcase-check] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 exit(-1)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 TARGET_BASE_FILENAME = ARGS[1]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 SOURCES = ARGS[2:]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 try:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 # Make sure the destination directory exists
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 os.makedirs(os.path.normpath(os.path.join(TARGET_BASE_FILENAME, '..')))
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 except:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 pass
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 ## Read each resource file
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 def CheckNoUpcase(s):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 global UPCASE_CHECK
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 if (UPCASE_CHECK and
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 re.search('[A-Z]', s) != None):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 raise Exception("Path in a directory with an upcase letter: %s" % s)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 resources = {}
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 counter = 0
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 i = 0
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 while i < len(SOURCES):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 resourceName = SOURCES[i].upper()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 pathName = SOURCES[i + 1]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 if not os.path.exists(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 raise Exception("Non existing path: %s" % pathName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 if resourceName in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 raise Exception("Twice the same resource: " + resourceName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 if os.path.isdir(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 # The resource is a directory: Recursively explore its files
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 content = {}
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 for root, dirs, files in os.walk(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 base = os.path.relpath(root, pathName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 for f in files:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 if f.find('~') == -1: # Ignore Emacs backup files
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 if base == '.':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 r = f
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 r = os.path.join(base, f)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 CheckNoUpcase(r)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 r = '/' + r.replace('\\', '/')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 if r in content:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 raise Exception("Twice the same filename (check case): " + r)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 content[r] = {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 'Filename' : os.path.join(root, f),
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 'Index' : counter
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 counter += 1
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 resources[resourceName] = {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 'Type' : 'Directory',
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 'Files' : content
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 elif os.path.isfile(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 resources[resourceName] = {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 'Type' : 'File',
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 'Index' : counter,
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 'Filename' : pathName
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 counter += 1
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 raise Exception("Not a regular file, nor a directory: " + pathName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 i += 2
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 #pprint.pprint(resources)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 ## Write .h header
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 header = open(TARGET_BASE_FILENAME + '.h', 'w')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 header.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 #pragma once
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 #include <string>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 #include <list>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 namespace Orthanc
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 namespace EmbeddedResources
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 enum FileResourceId
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 if isFirst:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 isFirst = False
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 header.write(',\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 header.write(' %s' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 header.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 };
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 enum DirectoryResourceId
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 if isFirst:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 isFirst = False
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 header.write(',\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 header.write(' %s' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 header.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 };
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 const void* GetFileResourceBuffer(FileResourceId id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 size_t GetFileResourceSize(FileResourceId id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 void GetFileResource(std::string& result, FileResourceId id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 void ListResources(std::list<std::string>& result, DirectoryResourceId id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 header.close()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 ## Write the resource content in the .cpp source
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 PYTHON_MAJOR_VERSION = sys.version_info[0]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 def WriteResource(cpp, item):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index'])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 f = open(item['Filename'], "rb")
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 content = f.read()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 f.close()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 # http://stackoverflow.com/a/1035360
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 pos = 0
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 for b in content:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 if PYTHON_MAJOR_VERSION == 2:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 c = ord(b[0])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 c = b
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 if pos > 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 cpp.write(', ')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204 if (pos % 16) == 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 cpp.write('\n ')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 if c < 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 raise Exception("Internal error")
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 cpp.write("0x%02x" % c)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 pos += 1
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
1390
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
213 # Zero-size array are disallowed, so we put one single void character in it.
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
214 if pos == 0:
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
215 cpp.write(' 0')
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
216
1181
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 cpp.write(' };\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos))
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 print os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 #include "%s.h"
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 #include <stdint.h>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 #include <string.h>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 #include <stdexcept>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 namespace Orthanc
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 namespace EmbeddedResources
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 """ % (os.path.basename(TARGET_BASE_FILENAME)))
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 WriteResource(cpp, resources[name])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 for f in resources[name]['Files']:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 WriteResource(cpp, resources[name]['Files'][f])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 ## Write the accessors to the file resources in .cpp
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 const void* GetFileResourceBuffer(FileResourceId id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index'])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 throw std::runtime_error("Unknown resource");
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 size_t GetFileResourceSize(FileResourceId id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 cpp.write(' return resource%dSize;\n' % resources[name]['Index'])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 throw std::runtime_error("Unknown resource");
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 ## Write the accessors to the directory resources in .cpp
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 for path in resources[name]['Files']:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 cpp.write(' if (!strcmp(path, "%s"))\n' % path)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index'])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 cpp.write(' throw std::runtime_error("Unknown path in a directory resource");\n\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 cpp.write(""" default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 throw std::runtime_error("Unknown resource");
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 for path in resources[name]['Files']:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 cpp.write(' if (!strcmp(path, "%s"))\n' % path)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index'])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 cpp.write(' throw std::runtime_error("Unknown path in a directory resource");\n\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 cpp.write(""" default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330 throw std::runtime_error("Unknown resource");
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 ## List the resources in a directory
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 void ListResources(std::list<std::string>& result, DirectoryResourceId id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 result.clear();
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 for path in sorted(resources[name]['Files']):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 cpp.write(' result.push_back("%s");\n' % path)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 cpp.write(' break;\n\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 cpp.write(""" default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 throw std::runtime_error("Unknown resource");
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 ## Write the convenience wrappers in .cpp
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 void GetFileResource(std::string& result, FileResourceId id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 size_t size = GetFileResourceSize(id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 result.resize(size);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 if (size > 0)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 memcpy(&result[0], GetFileResourceBuffer(id), size);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 size_t size = GetDirectoryResourceSize(id, path);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 result.resize(size);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 if (size > 0)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 cpp.close()