Mercurial > hg > orthanc
annotate Plugins/Samples/WebSkeleton/Framework/EmbedResources.py @ 2798:5c83e5cf9d79
fix decoding of MONOCHROME1 & MONOCHROME2 images
author | am@osimis.io |
---|---|
date | Tue, 21 Aug 2018 10:42:16 +0200 |
parents | 878b59270859 |
children | 4e43e67f8ecf |
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 | 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 |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
4 # Copyright (C) 2017-2018 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): |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 base = os.path.relpath(root, pathName) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 for f in files: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 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
|
80 if base == '.': |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 r = f |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 else: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 r = os.path.join(base, f) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 CheckNoUpcase(r) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 r = '/' + r.replace('\\', '/') |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
87 if r in content: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 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
|
89 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 content[r] = { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 'Filename' : os.path.join(root, f), |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 'Index' : counter |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 counter += 1 |
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 resources[resourceName] = { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 'Type' : 'Directory', |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 'Files' : content |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
99 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
100 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
101 elif os.path.isfile(pathName): |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
102 resources[resourceName] = { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
103 'Type' : 'File', |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
104 'Index' : counter, |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
105 'Filename' : pathName |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
106 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
107 counter += 1 |
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 else: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 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
|
111 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 i += 2 |
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 #pprint.pprint(resources) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
115 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
116 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
117 ##################################################################### |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
118 ## Write .h header |
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 header = open(TARGET_BASE_FILENAME + '.h', 'w') |
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.write(""" |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
124 #pragma once |
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 #include <string> |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
127 #include <list> |
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 namespace Orthanc |
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 EmbeddedResources |
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 enum FileResourceId |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
134 { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
135 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
136 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
137 isFirst = True |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
138 for name in resources: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
139 if resources[name]['Type'] == 'File': |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
140 if isFirst: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
141 isFirst = False |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
142 else: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
143 header.write(',\n') |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
144 header.write(' %s' % name) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
145 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
146 header.write(""" |
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 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
149 enum DirectoryResourceId |
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 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
153 isFirst = True |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
154 for name in resources: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
155 if resources[name]['Type'] == 'Directory': |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
156 if isFirst: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
157 isFirst = False |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
158 else: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
159 header.write(',\n') |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
160 header.write(' %s' % name) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
161 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
162 header.write(""" |
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 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
165 const void* GetFileResourceBuffer(FileResourceId id); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
166 size_t GetFileResourceSize(FileResourceId id); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
167 void GetFileResource(std::string& result, FileResourceId id); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
168 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
169 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
|
170 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
|
171 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
|
172 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
173 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
|
174 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
175 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
176 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
177 header.close() |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
178 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
179 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
180 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
181 ##################################################################### |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
182 ## 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
|
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 PYTHON_MAJOR_VERSION = sys.version_info[0] |
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 def WriteResource(cpp, item): |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
188 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
|
189 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
190 f = open(item['Filename'], "rb") |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
191 content = f.read() |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
192 f.close() |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
193 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
194 # http://stackoverflow.com/a/1035360 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
195 pos = 0 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
196 for b in content: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
197 if PYTHON_MAJOR_VERSION == 2: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
198 c = ord(b[0]) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
199 else: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
200 c = b |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
201 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
202 if pos > 0: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
203 cpp.write(', ') |
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 % 16) == 0: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
206 cpp.write('\n ') |
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 c < 0: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
209 raise Exception("Internal error") |
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 cpp.write("0x%02x" % c) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
212 pos += 1 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
213 |
1390
92da9e1c2daa
improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1375
diff
changeset
|
214 # 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
|
215 if pos == 0: |
92da9e1c2daa
improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1375
diff
changeset
|
216 cpp.write(' 0') |
92da9e1c2daa
improvement of samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1375
diff
changeset
|
217 |
1181
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
218 cpp.write(' };\n') |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
219 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
|
220 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
221 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
222 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w') |
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 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
|
225 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
226 cpp.write(""" |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
227 #include "%s.h" |
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 #include <stdint.h> |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
230 #include <string.h> |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
231 #include <stdexcept> |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
232 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
233 namespace Orthanc |
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 EmbeddedResources |
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 """ % (os.path.basename(TARGET_BASE_FILENAME))) |
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 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
240 for name in resources: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
241 if resources[name]['Type'] == 'File': |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
242 WriteResource(cpp, resources[name]) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
243 else: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
244 for f in resources[name]['Files']: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
245 WriteResource(cpp, resources[name]['Files'][f]) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
246 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
247 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
248 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
249 ##################################################################### |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
250 ## 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
|
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 cpp.write(""" |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
254 const void* GetFileResourceBuffer(FileResourceId id) |
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 switch (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 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
259 for name in resources: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
260 if resources[name]['Type'] == 'File': |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
261 cpp.write(' case %s:\n' % name) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
262 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
|
263 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
264 cpp.write(""" |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
265 default: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
266 throw std::runtime_error("Unknown resource"); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
267 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
268 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
269 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
270 size_t GetFileResourceSize(FileResourceId id) |
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 switch (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 """) |
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 for name in resources: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
277 if resources[name]['Type'] == 'File': |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
278 cpp.write(' case %s:\n' % name) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
279 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
|
280 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
281 cpp.write(""" |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
282 default: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
283 throw std::runtime_error("Unknown resource"); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
284 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
285 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
286 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
287 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
288 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
289 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
290 ##################################################################### |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
291 ## 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
|
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 cpp.write(""" |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
295 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
|
296 { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
297 switch (id) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
298 { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
299 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
300 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
301 for name in resources: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
302 if resources[name]['Type'] == 'Directory': |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
303 cpp.write(' case %s:\n' % name) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
304 isFirst = True |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
305 for path in resources[name]['Files']: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
306 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
|
307 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
|
308 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
|
309 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
310 cpp.write(""" default: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
311 throw std::runtime_error("Unknown resource"); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
312 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
313 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
314 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
315 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
|
316 { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
317 switch (id) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
318 { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
319 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
320 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
321 for name in resources: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
322 if resources[name]['Type'] == 'Directory': |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
323 cpp.write(' case %s:\n' % name) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
324 isFirst = True |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
325 for path in resources[name]['Files']: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
326 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
|
327 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
|
328 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
|
329 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
330 cpp.write(""" default: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
331 throw std::runtime_error("Unknown resource"); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
332 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
333 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
334 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
335 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
336 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
337 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
338 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
339 ##################################################################### |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
340 ## List the resources in a directory |
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 cpp.write(""" |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
344 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
|
345 { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
346 result.clear(); |
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 switch (id) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
349 { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
350 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
351 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
352 for name in resources: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
353 if resources[name]['Type'] == 'Directory': |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
354 cpp.write(' case %s:\n' % name) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
355 for path in sorted(resources[name]['Files']): |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
356 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
|
357 cpp.write(' break;\n\n') |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
358 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
359 cpp.write(""" default: |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
360 throw std::runtime_error("Unknown resource"); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
361 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
362 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
363 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
364 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
365 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
366 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
367 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
368 ##################################################################### |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
369 ## Write the convenience wrappers in .cpp |
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 cpp.write(""" |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
373 void GetFileResource(std::string& result, FileResourceId id) |
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 size_t size = GetFileResourceSize(id); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
376 result.resize(size); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
377 if (size > 0) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
378 memcpy(&result[0], GetFileResourceBuffer(id), size); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
379 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
380 |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
381 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
|
382 { |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
383 size_t size = GetDirectoryResourceSize(id, path); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
384 result.resize(size); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
385 if (size > 0) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
386 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
387 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
388 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
389 } |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
390 """) |
17302d83abfd
Sample plugin framework to serve static resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
391 cpp.close() |