annotate OrthancServer/Plugins/Samples/WebSkeleton/Framework/EmbedResources.py @ 5237:cd2258ca7894 db-protobuf

log about missing support for labels
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Apr 2023 09:07:47 +0200
parents 0ea402b4d901
children 42ea61cf8386
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
5185
0ea402b4d901 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
4 # Copyright (C) 2017-2023 Osimis S.A., Belgium
0ea402b4d901 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
5 # Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1181
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 #
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 # 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
8 # 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
9 # 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
10 # 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
11 #
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 # 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
13 # 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
14 # 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
15 # General Public License for more details.
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 #
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 # 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
18 # 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
19
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 import sys
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 import os
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import os.path
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import pprint
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import re
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 UPCASE_CHECK = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 ARGS = []
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 for i in range(len(sys.argv)):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 if not sys.argv[i].startswith('--'):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 ARGS.append(sys.argv[i])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 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
33 UPCASE_CHECK = False
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 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
36 print ('Usage:')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 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
38 exit(-1)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 TARGET_BASE_FILENAME = ARGS[1]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 SOURCES = ARGS[2:]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 try:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 # Make sure the destination directory exists
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 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
46 except:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 pass
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 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 ## Read each resource file
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 def CheckNoUpcase(s):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 global UPCASE_CHECK
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 if (UPCASE_CHECK and
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 re.search('[A-Z]', s) != None):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 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
59
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 resources = {}
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 counter = 0
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 i = 0
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 while i < len(SOURCES):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 resourceName = SOURCES[i].upper()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 pathName = SOURCES[i + 1]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 if not os.path.exists(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 raise Exception("Non existing path: %s" % pathName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 if resourceName in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 raise Exception("Twice the same resource: " + resourceName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 if os.path.isdir(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 # 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
76 content = {}
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 for root, dirs, files in os.walk(pathName):
3587
76b3228f99b0 Sort file lists
Bernhard M. Wiedemann <bernhard+hgcommit@lsmod.de>
parents: 3060
diff changeset
78 dirs.sort()
76b3228f99b0 Sort file lists
Bernhard M. Wiedemann <bernhard+hgcommit@lsmod.de>
parents: 3060
diff changeset
79 files.sort()
1181
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 base = os.path.relpath(root, pathName)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 for f in files:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 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
83 if base == '.':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 r = f
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 r = os.path.join(base, f)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 CheckNoUpcase(r)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 r = '/' + r.replace('\\', '/')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 if r in content:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 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
92
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 content[r] = {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 'Filename' : os.path.join(root, f),
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 'Index' : counter
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 counter += 1
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 resources[resourceName] = {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 'Type' : 'Directory',
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 'Files' : content
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 elif os.path.isfile(pathName):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 resources[resourceName] = {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 'Type' : 'File',
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 'Index' : counter,
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 'Filename' : pathName
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 }
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 counter += 1
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 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
114
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 i += 2
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 #pprint.pprint(resources)
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 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 ## Write .h header
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 header = open(TARGET_BASE_FILENAME + '.h', 'w')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 header.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 #pragma once
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 #include <string>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 #include <list>
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 namespace Orthanc
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 namespace EmbeddedResources
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 enum FileResourceId
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 if isFirst:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 isFirst = False
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 header.write(',\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 header.write(' %s' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 header.write("""
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 enum DirectoryResourceId
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 if isFirst:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 isFirst = False
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 header.write(',\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 header.write(' %s' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 header.write("""
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 const void* GetFileResourceBuffer(FileResourceId id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 size_t GetFileResourceSize(FileResourceId id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 void GetFileResource(std::string& result, FileResourceId id);
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 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
173 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
174 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
175
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 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
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 header.close()
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 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 ## 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
186 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 PYTHON_MAJOR_VERSION = sys.version_info[0]
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 def WriteResource(cpp, item):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 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
192
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 f = open(item['Filename'], "rb")
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 content = f.read()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 f.close()
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 # http://stackoverflow.com/a/1035360
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 pos = 0
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 for b in content:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 if PYTHON_MAJOR_VERSION == 2:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 c = ord(b[0])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 c = b
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 if pos > 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 cpp.write(', ')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 if (pos % 16) == 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 cpp.write('\n ')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 if c < 0:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 raise Exception("Internal error")
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 cpp.write("0x%02x" % c)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 pos += 1
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216
1390
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
217 # 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
218 if pos == 0:
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
219 cpp.write(' 0')
92da9e1c2daa improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1375
diff changeset
220
1181
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 cpp.write(' };\n')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 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
223
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 = open(TARGET_BASE_FILENAME + '.cpp', 'w')
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 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
228
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 #include "%s.h"
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 #include <stdint.h>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 #include <string.h>
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 #include <stdexcept>
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 namespace Orthanc
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 namespace EmbeddedResources
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 """ % (os.path.basename(TARGET_BASE_FILENAME)))
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 WriteResource(cpp, resources[name])
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246 else:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 for f in resources[name]['Files']:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 WriteResource(cpp, resources[name]['Files'][f])
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 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 ## 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
254 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 const void* GetFileResourceBuffer(FileResourceId id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 switch (id)
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 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 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
266
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 throw std::runtime_error("Unknown resource");
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 size_t GetFileResourceSize(FileResourceId id)
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 switch (id)
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 if resources[name]['Type'] == 'File':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 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
283
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 throw std::runtime_error("Unknown resource");
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 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 ## 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
295 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 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
299 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 switch (id)
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 for path in resources[name]['Files']:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 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
310 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
311 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
312
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 cpp.write(""" default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314 throw std::runtime_error("Unknown resource");
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 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
319 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320 switch (id)
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 isFirst = True
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 for path in resources[name]['Files']:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 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
330 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
331 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
332
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 cpp.write(""" default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 throw std::runtime_error("Unknown resource");
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 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343 ## List the resources in a directory
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 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
348 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 result.clear();
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 switch (id)
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 for name in resources:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 if resources[name]['Type'] == 'Directory':
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357 cpp.write(' case %s:\n' % name)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 for path in sorted(resources[name]['Files']):
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 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
360 cpp.write(' break;\n\n')
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 cpp.write(""" default:
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 throw std::runtime_error("Unknown resource");
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 #####################################################################
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 ## Write the convenience wrappers in .cpp
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 cpp.write("""
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 void GetFileResource(std::string& result, FileResourceId id)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 size_t size = GetFileResourceSize(id);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 result.resize(size);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 if (size > 0)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 memcpy(&result[0], GetFileResourceBuffer(id), size);
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
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 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
385 {
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 size_t size = GetDirectoryResourceSize(id, path);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 result.resize(size);
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 if (size > 0)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size);
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 """)
17302d83abfd Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 cpp.close()