annotate Resources/GenerateErrorCodes.py @ 1625:c17b1142caab

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 18 Sep 2015 12:58:51 +0200
parents 9a3e03d6a4d5
children 8dc468f44661
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1576
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 # Orthanc - A Lightweight, RESTful DICOM Store
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 # Department, University Hospital of Liege, Belgium
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 #
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 # modify it under the terms of the GNU General Public License as
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 # published by the Free Software Foundation, either version 3 of the
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # License, or (at your option) any later version.
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 #
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 # In addition, as a special exception, the copyright holders of this
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 # program give permission to link the code of its release with the
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # OpenSSL project's "OpenSSL" library (or with modified versions of it
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # that use the same license as the "OpenSSL" library), and distribute
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # the linked executables. You must obey the GNU General Public License
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 # in all respects for all of the code used other than "OpenSSL". If you
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 # modify file(s) with this exception, you may extend this exception to
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 # your version of the file(s), but you are not obligated to do so. If
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 # you do not wish to do so, delete this exception statement from your
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 # version. If you delete this exception statement from all source files
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 # in the program, then also delete it here.
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 # This program is distributed in the hope that it will be useful, but
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 # WITHOUT ANY WARRANTY; without even the implied warranty of
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 # General Public License for more details.
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 # You should have received a copy of the GNU General Public License
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 # along with this program. If not, see <http://www.gnu.org/licenses/>.
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 import json
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 import os
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 import re
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 BASE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
1577
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
40
1576
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 ##
1577
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
42 ## Read all the available error codes and HTTP status
1576
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 ##
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 with open(os.path.join(BASE, 'Resources', 'ErrorCodes.json'), 'r') as f:
1582
bd1889029cbb encoding of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1580
diff changeset
46 ERRORS = json.loads(re.sub('/\*.*?\*/', '', f.read()))
1576
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
1577
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
48 with open(os.path.join(BASE, 'Core', 'Enumerations.h'), 'r') as f:
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
49 a = f.read()
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
50
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
51 HTTP = {}
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
52 for i in re.findall('(HttpStatus_([0-9]+)_\w+)', a):
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
53 HTTP[int(i[1])] = i[0]
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
54
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
55
1576
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 ##
1578
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
58 ## Generate the "ErrorCode" enumeration in "Enumerations.h"
1576
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 ##
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
1578
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
61 path = os.path.join(BASE, 'Core', 'Enumerations.h')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
62 with open(path, 'r') as f:
1576
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 a = f.read()
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
1578
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
65 s = ',\n'.join(map(lambda x: ' ErrorCode_%s = %d /*!< %s */' % (x['Name'], int(x['Code']), x['Description']), ERRORS))
1576
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 a = re.sub('(enum ErrorCode\s*{)[^}]*?(\s*};)', r'\1\n%s\2' % s, a, re.DOTALL)
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
1578
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
68 with open(path, 'w') as f:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
69 f.write(a)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
70
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
71
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
72
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
73 ##
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
74 ## Generate the "OrthancPluginErrorCode" enumeration in "OrthancCPlugin.h"
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
75 ##
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
76
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
77 path = os.path.join(BASE, 'Plugins', 'Include', 'orthanc', 'OrthancCPlugin.h')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
78 with open(path, 'r') as f:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
79 a = f.read()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
80
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
81 s = ',\n'.join(map(lambda x: ' OrthancPluginErrorCode_%s = %d /*!< %s */' % (x['Name'], int(x['Code']), x['Description']), ERRORS))
1580
bf502300c52e force the size of the enumerations in the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1578
diff changeset
82 s += ',\n\n _OrthancPluginErrorCode_INTERNAL = 0x7fffffff\n '
bf502300c52e force the size of the enumerations in the plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1578
diff changeset
83 a = re.sub('(typedef enum\s*{)[^}]*?(} OrthancPluginErrorCode;)', r'\1\n%s\2' % s, a, re.DOTALL)
1578
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
84
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
85 with open(path, 'w') as f:
1576
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 f.write(a)
de54c19fc44d refactoring OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
1577
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
88
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
89
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
90 ##
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
91 ## Generate the "EnumerationToString(ErrorCode)" and
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
92 ## "ConvertErrorCodeToHttpStatus(ErrorCode)" functions in
1578
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
93 ## "Enumerations.cpp"
1577
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
94 ##
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
95
1578
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
96 path = os.path.join(BASE, 'Core', 'Enumerations.cpp')
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
97 with open(path, 'r') as f:
1577
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
98 a = f.read()
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
99
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
100 s = '\n\n'.join(map(lambda x: ' case ErrorCode_%s:\n return "%s";' % (x['Name'], x['Description']), ERRORS))
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
101 a = re.sub('(EnumerationToString\(ErrorCode.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)',
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
102 r'\1\n%s\2' % s, a, re.DOTALL)
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
103
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
104 def GetHttpStatus(x):
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
105 s = HTTP[x['HttpStatus']]
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
106 return ' case ErrorCode_%s:\n return %s;' % (x['Name'], s)
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
107
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
108 s = '\n\n'.join(map(GetHttpStatus, filter(lambda x: 'HttpStatus' in x, ERRORS)))
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
109 a = re.sub('(ConvertErrorCodeToHttpStatus\(ErrorCode.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)',
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
110 r'\1\n%s\2' % s, a, re.DOTALL)
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
111
1578
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1577
diff changeset
112 with open(path, 'w') as f:
1577
7aac0cddd42e generation of the error codes from a Python script
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1576
diff changeset
113 f.write(a)
1585
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
114
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
115
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
116
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
117 ##
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
118 ## Generate the "ErrorCode" enumeration in "OrthancSQLiteException.h"
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
119 ##
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
120
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
121 path = os.path.join(BASE, 'Core', 'SQLite', 'OrthancSQLiteException.h')
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
122 with open(path, 'r') as f:
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
123 a = f.read()
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
124
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
125 e = filter(lambda x: 'SQLite' in x and x['SQLite'], ERRORS)
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
126 s = ',\n'.join(map(lambda x: ' ErrorCode_%s' % x['Name'], e))
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
127 a = re.sub('(enum ErrorCode\s*{)[^}]*?(\s*};)', r'\1\n%s\2' % s, a, re.DOTALL)
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
128
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
129 s = '\n\n'.join(map(lambda x: ' case ErrorCode_%s:\n return "%s";' % (x['Name'], x['Description']), e))
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
130 a = re.sub('(EnumerationToString\(ErrorCode.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)',
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
131 r'\1\n%s\2' % s, a, re.DOTALL)
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
132
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
133 with open(path, 'w') as f:
9a3e03d6a4d5 fix sqlite standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1582
diff changeset
134 f.write(a)