annotate Plugins/Samples/WebSkeleton/Framework/EmbedResources.py @ 3807:2f28c7eb2776

fix civetweb 1.12 archive
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 02 Apr 2020 18:08:08 +0200
parents 94f4a18a79cc
children
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
3640
94f4a18a79cc upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3587
diff changeset
4 # Copyright (C) 2017-2020 Osimis S.A., Belgium
1181
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 #
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 # 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
7 # 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
8 # 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
9 # 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
10 #
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 # 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
12 # 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
13 # 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
14 # General Public License for more details.
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 #
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # 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
17 # 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
18
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 import sys
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 import os
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 import os.path
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import pprint
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import re
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 UPCASE_CHECK = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 ARGS = []
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 for i in range(len(sys.argv)):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 if not sys.argv[i].startswith('--'):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 ARGS.append(sys.argv[i])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 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
32 UPCASE_CHECK = False
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 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
35 print ('Usage:')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 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
37 exit(-1)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 TARGET_BASE_FILENAME = ARGS[1]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 SOURCES = ARGS[2:]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 try:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 # Make sure the destination directory exists
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 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
45 except:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 pass
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 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 ## Read each resource file
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 def CheckNoUpcase(s):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 global UPCASE_CHECK
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 if (UPCASE_CHECK and
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 re.search('[A-Z]', s) != None):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 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
58
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 resources = {}
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 counter = 0
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 i = 0
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 while i < len(SOURCES):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 resourceName = SOURCES[i].upper()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 pathName = SOURCES[i + 1]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 if not os.path.exists(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 raise Exception("Non existing path: %s" % pathName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 if resourceName in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 raise Exception("Twice the same resource: " + resourceName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 if os.path.isdir(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 # 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
75 content = {}
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 for root, dirs, files in os.walk(pathName):
3587
76b3228f99b0 Sort file lists
Bernhard M. Wiedemann <bernhard+hgcommit@lsmod.de>
parents: 3060
diff changeset
77 dirs.sort()
76b3228f99b0 Sort file lists
Bernhard M. Wiedemann <bernhard+hgcommit@lsmod.de>
parents: 3060
diff changeset
78 files.sort()
1181
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 base = os.path.relpath(root, pathName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 for f in files:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 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
82 if base == '.':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 r = f
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 r = os.path.join(base, f)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 CheckNoUpcase(r)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 r = '/' + r.replace('\\', '/')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 if r in content:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 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
91
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 content[r] = {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 'Filename' : os.path.join(root, f),
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 'Index' : counter
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 counter += 1
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 resources[resourceName] = {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 'Type' : 'Directory',
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 'Files' : content
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 elif os.path.isfile(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 resources[resourceName] = {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 'Type' : 'File',
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 'Index' : counter,
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 'Filename' : pathName
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 counter += 1
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 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 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
113
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 i += 2
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 #pprint.pprint(resources)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
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 ## Write .h header
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 header = open(TARGET_BASE_FILENAME + '.h', 'w')
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 header.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 #pragma once
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 #include <string>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 #include <list>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 namespace Orthanc
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 namespace EmbeddedResources
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 enum FileResourceId
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 if isFirst:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 isFirst = False
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 header.write(',\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 header.write(' %s' % name)
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 header.write("""
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 enum DirectoryResourceId
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 if isFirst:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 isFirst = False
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 header.write(',\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 header.write(' %s' % name)
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 header.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 };
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 const void* GetFileResourceBuffer(FileResourceId id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 size_t GetFileResourceSize(FileResourceId id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 void GetFileResource(std::string& result, FileResourceId id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171 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
172 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
173 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
174
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 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
176 }
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 header.close()
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
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 ## 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
185 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 PYTHON_MAJOR_VERSION = sys.version_info[0]
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 def WriteResource(cpp, item):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 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
191
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 f = open(item['Filename'], "rb")
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 content = f.read()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 f.close()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 # http://stackoverflow.com/a/1035360
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 pos = 0
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 for b in content:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 if PYTHON_MAJOR_VERSION == 2:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 c = ord(b[0])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 c = b
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 > 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 cpp.write(', ')
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 (pos % 16) == 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 cpp.write('\n ')
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 if c < 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 raise Exception("Internal error")
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 cpp.write("0x%02x" % c)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 pos += 1
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
1390
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
216 # 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
217 if pos == 0:
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
218 cpp.write(' 0')
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
219
1181
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 cpp.write(' };\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 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
222
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226 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
227
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 #include "%s.h"
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 #include <stdint.h>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 #include <string.h>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 #include <stdexcept>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 namespace Orthanc
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 namespace EmbeddedResources
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 """ % (os.path.basename(TARGET_BASE_FILENAME)))
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 WriteResource(cpp, resources[name])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 for f in resources[name]['Files']:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 WriteResource(cpp, resources[name]['Files'][f])
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
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 ## 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
253 #####################################################################
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 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 const void* GetFileResourceBuffer(FileResourceId id)
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 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 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
265
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 throw std::runtime_error("Unknown resource");
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 }
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 size_t GetFileResourceSize(FileResourceId id)
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 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 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
282
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 throw std::runtime_error("Unknown resource");
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
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 ## 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
294 #####################################################################
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 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 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
298 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 for path in resources[name]['Files']:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 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
309 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
310 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
311
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312 cpp.write(""" default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 throw std::runtime_error("Unknown resource");
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 }
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 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
318 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 for path in resources[name]['Files']:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 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
329 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
330 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
331
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 cpp.write(""" default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 throw std::runtime_error("Unknown resource");
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
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 ## List the resources in a directory
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 #####################################################################
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 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 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
347 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 result.clear();
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 switch (id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 for path in sorted(resources[name]['Files']):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 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
359 cpp.write(' break;\n\n')
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 cpp.write(""" default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 throw std::runtime_error("Unknown resource");
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
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 ## Write the convenience wrappers in .cpp
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 #####################################################################
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 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 void GetFileResource(std::string& result, FileResourceId id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 size_t size = GetFileResourceSize(id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 result.resize(size);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 if (size > 0)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 memcpy(&result[0], GetFileResourceBuffer(id), size);
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 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
384 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385 size_t size = GetDirectoryResourceSize(id, path);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 result.resize(size);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 if (size > 0)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size);
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 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 cpp.close()