Mercurial > hg > orthanc
comparison OrthancFramework/Resources/CodeGeneration/GenerateErrorCodes.py @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Resources/GenerateErrorCodes.py@94f4a18a79cc |
children | 0327421506ad |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 # Orthanc - A Lightweight, RESTful DICOM Store | |
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
5 # Department, University Hospital of Liege, Belgium | |
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium | |
7 # | |
8 # This program is free software: you can redistribute it and/or | |
9 # modify it under the terms of the GNU General Public License as | |
10 # published by the Free Software Foundation, either version 3 of the | |
11 # License, or (at your option) any later version. | |
12 # | |
13 # In addition, as a special exception, the copyright holders of this | |
14 # program give permission to link the code of its release with the | |
15 # OpenSSL project's "OpenSSL" library (or with modified versions of it | |
16 # that use the same license as the "OpenSSL" library), and distribute | |
17 # the linked executables. You must obey the GNU General Public License | |
18 # in all respects for all of the code used other than "OpenSSL". If you | |
19 # modify file(s) with this exception, you may extend this exception to | |
20 # your version of the file(s), but you are not obligated to do so. If | |
21 # you do not wish to do so, delete this exception statement from your | |
22 # version. If you delete this exception statement from all source files | |
23 # in the program, then also delete it here. | |
24 # | |
25 # This program is distributed in the hope that it will be useful, but | |
26 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
28 # General Public License for more details. | |
29 # | |
30 # You should have received a copy of the GNU General Public License | |
31 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
32 | |
33 | |
34 import json | |
35 import os | |
36 import re | |
37 import sys | |
38 | |
39 START_PLUGINS = 1000000 | |
40 BASE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | |
41 | |
42 | |
43 | |
44 ## | |
45 ## Read all the available error codes and HTTP status | |
46 ## | |
47 | |
48 with open(os.path.join(BASE, 'Resources', 'ErrorCodes.json'), 'r') as f: | |
49 ERRORS = json.loads(re.sub('/\*.*?\*/', '', f.read())) | |
50 | |
51 for error in ERRORS: | |
52 if error['Code'] >= START_PLUGINS: | |
53 print('ERROR: Error code must be below %d, but "%s" is set to %d' % (START_PLUGINS, error['Name'], error['Code'])) | |
54 sys.exit(-1) | |
55 | |
56 with open(os.path.join(BASE, 'Core', 'Enumerations.h'), 'r') as f: | |
57 a = f.read() | |
58 | |
59 HTTP = {} | |
60 for i in re.findall('(HttpStatus_([0-9]+)_\w+)', a): | |
61 HTTP[int(i[1])] = i[0] | |
62 | |
63 | |
64 | |
65 ## | |
66 ## Generate the "ErrorCode" enumeration in "Enumerations.h" | |
67 ## | |
68 | |
69 path = os.path.join(BASE, 'Core', 'Enumerations.h') | |
70 with open(path, 'r') as f: | |
71 a = f.read() | |
72 | |
73 s = ',\n'.join(map(lambda x: ' ErrorCode_%s = %d /*!< %s */' % (x['Name'], int(x['Code']), x['Description']), ERRORS)) | |
74 | |
75 s += ',\n ErrorCode_START_PLUGINS = %d' % START_PLUGINS | |
76 a = re.sub('(enum ErrorCode\s*{)[^}]*?(\s*};)', r'\1\n%s\2' % s, a, re.DOTALL) | |
77 | |
78 with open(path, 'w') as f: | |
79 f.write(a) | |
80 | |
81 | |
82 | |
83 ## | |
84 ## Generate the "OrthancPluginErrorCode" enumeration in "OrthancCPlugin.h" | |
85 ## | |
86 | |
87 path = os.path.join(BASE, 'Plugins', 'Include', 'orthanc', 'OrthancCPlugin.h') | |
88 with open(path, 'r') as f: | |
89 a = f.read() | |
90 | |
91 s = ',\n'.join(map(lambda x: ' OrthancPluginErrorCode_%s = %d /*!< %s */' % (x['Name'], int(x['Code']), x['Description']), ERRORS)) | |
92 s += ',\n\n _OrthancPluginErrorCode_INTERNAL = 0x7fffffff\n ' | |
93 a = re.sub('(typedef enum\s*{)[^}]*?(} OrthancPluginErrorCode;)', r'\1\n%s\2' % s, a, re.DOTALL) | |
94 | |
95 with open(path, 'w') as f: | |
96 f.write(a) | |
97 | |
98 | |
99 | |
100 ## | |
101 ## Generate the "EnumerationToString(ErrorCode)" and | |
102 ## "ConvertErrorCodeToHttpStatus(ErrorCode)" functions in | |
103 ## "Enumerations.cpp" | |
104 ## | |
105 | |
106 path = os.path.join(BASE, 'Core', 'Enumerations.cpp') | |
107 with open(path, 'r') as f: | |
108 a = f.read() | |
109 | |
110 s = '\n\n'.join(map(lambda x: ' case ErrorCode_%s:\n return "%s";' % (x['Name'], x['Description']), ERRORS)) | |
111 a = re.sub('(EnumerationToString\(ErrorCode.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)', | |
112 r'\1\n%s\2' % s, a, re.DOTALL) | |
113 | |
114 def GetHttpStatus(x): | |
115 s = HTTP[x['HttpStatus']] | |
116 return ' case ErrorCode_%s:\n return %s;' % (x['Name'], s) | |
117 | |
118 s = '\n\n'.join(map(GetHttpStatus, filter(lambda x: 'HttpStatus' in x, ERRORS))) | |
119 a = re.sub('(ConvertErrorCodeToHttpStatus\(ErrorCode.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)', | |
120 r'\1\n%s\2' % s, a, re.DOTALL) | |
121 | |
122 with open(path, 'w') as f: | |
123 f.write(a) | |
124 | |
125 | |
126 | |
127 ## | |
128 ## Generate the "ErrorCode" enumeration in "OrthancSQLiteException.h" | |
129 ## | |
130 | |
131 path = os.path.join(BASE, 'Core', 'SQLite', 'OrthancSQLiteException.h') | |
132 with open(path, 'r') as f: | |
133 a = f.read() | |
134 | |
135 e = filter(lambda x: 'SQLite' in x and x['SQLite'], ERRORS) | |
136 s = ',\n'.join(map(lambda x: ' ErrorCode_%s' % x['Name'], e)) | |
137 a = re.sub('(enum ErrorCode\s*{)[^}]*?(\s*};)', r'\1\n%s\2' % s, a, re.DOTALL) | |
138 | |
139 s = '\n\n'.join(map(lambda x: ' case ErrorCode_%s:\n return "%s";' % (x['Name'], x['Description']), e)) | |
140 a = re.sub('(EnumerationToString\(ErrorCode.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)', | |
141 r'\1\n%s\2' % s, a, re.DOTALL) | |
142 | |
143 with open(path, 'w') as f: | |
144 f.write(a) | |
145 | |
146 | |
147 | |
148 ## | |
149 ## Generate the "PrintErrors" function in "main.cpp" | |
150 ## | |
151 | |
152 path = os.path.join(BASE, 'OrthancServer', 'main.cpp') | |
153 with open(path, 'r') as f: | |
154 a = f.read() | |
155 | |
156 s = '\n'.join(map(lambda x: ' PrintErrorCode(ErrorCode_%s, "%s");' % (x['Name'], x['Description']), ERRORS)) | |
157 a = re.sub('(static void PrintErrors[^{}]*?{[^{}]*?{)([^}]*?)}', r'\1\n%s\n }' % s, a, re.DOTALL) | |
158 | |
159 with open(path, 'w') as f: | |
160 f.write(a) |