annotate Resources/GenerateTransferSyntaxes.py @ 3727:090022f1b5e1

auto-generation of primitives to handle transfer syntaxes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 06 Mar 2020 17:10:03 +0100
parents
children ae31ba2b09a6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3727
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 # Orthanc - A Lightweight, RESTful DICOM Store
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 # Department, University Hospital of Liege, Belgium
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 #
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 # This program is free software: you can redistribute it and/or
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 # modify it under the terms of the GNU General Public License as
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # published by the Free Software Foundation, either version 3 of the
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 # License, or (at your option) any later version.
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 #
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 # In addition, as a special exception, the copyright holders of this
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # program give permission to link the code of its release with the
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # OpenSSL project's "OpenSSL" library (or with modified versions of it
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # that use the same license as the "OpenSSL" library), and distribute
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 # the linked executables. You must obey the GNU General Public License
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 # in all respects for all of the code used other than "OpenSSL". If you
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 # modify file(s) with this exception, you may extend this exception to
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 # your version of the file(s), but you are not obligated to do so. If
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 # you do not wish to do so, delete this exception statement from your
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 # version. If you delete this exception statement from all source files
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 # in the program, then also delete it here.
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 # This program is distributed in the hope that it will be useful, but
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 # WITHOUT ANY WARRANTY; without even the implied warranty of
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 # General Public License for more details.
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 # You should have received a copy of the GNU General Public License
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 # along with this program. If not, see <http://www.gnu.org/licenses/>.
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 import json
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 import os
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 import re
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 import sys
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 BASE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 ## https://www.dicomlibrary.com/dicom/transfer-syntax/
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 ## https://cedocs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=EDICOM_transfer_syntax
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 with open(os.path.join(BASE, 'Resources', 'DicomTransferSyntaxes.json'), 'r') as f:
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 SYNTAXES = json.loads(f.read())
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 ##
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 ## Generate the "DicomTransferSyntax" enumeration in "Enumerations.h"
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 ##
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 path = os.path.join(BASE, 'Core', 'Enumerations.h')
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 with open(path, 'r') as f:
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 a = f.read()
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 s = ',\n'.join(map(lambda x: ' DicomTransferSyntax_%s /*!< %s */' % (x['Value'], x['Name']), SYNTAXES))
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 a = re.sub('(enum DicomTransferSyntax\s*{)[^}]*?(\s*};)', r'\1\n%s\2' % s, a, re.DOTALL)
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 with open(path, 'w') as f:
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 f.write(a)
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 ##
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 ## Generate the "GetTransferSyntaxUid()" function in
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 ## "Enumerations.cpp"
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 ##
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 path = os.path.join(BASE, 'Core', 'Enumerations.cpp')
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 with open(path, 'r') as f:
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 a = f.read()
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 s = '\n\n'.join(map(lambda x: ' case DicomTransferSyntax_%s:\n return "%s";' % (x['Value'], x['UID']), SYNTAXES))
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 a = re.sub('(GetTransferSyntaxUid\(DicomTransferSyntax.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)',
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 r'\1\n%s\2' % s, a, re.DOTALL)
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 with open(path, 'w') as f:
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 f.write(a)
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 ##
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 ## Generate the "GetDcmtkTransferSyntax()" function in
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 ## "FromDcmtkBridge.cpp"
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 ##
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 path = os.path.join(BASE, 'Core', 'DicomParsing', 'FromDcmtkBridge.cpp')
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 with open(path, 'r') as f:
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 a = f.read()
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 def Format(x):
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 t = ' case DicomTransferSyntax_%s:\n target = %s;\n return true;' % (x['Value'], x['DCMTK'])
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if 'SinceDCMTK' in x:
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 return '#if DCMTK_VERSION_NUMBER >= %s\n%s\n#endif' % (x['SinceDCMTK'], t)
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 else:
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 return t
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 s = '\n\n'.join(map(Format, filter(lambda x: 'DCMTK' in x, SYNTAXES)))
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 a = re.sub('(GetDcmtkTransferSyntax\(E_TransferSyntax.*?\s*DicomTransferSyntax.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)',
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 r'\1\n%s\2' % s, a, re.DOTALL)
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 with open(path, 'w') as f:
090022f1b5e1 auto-generation of primitives to handle transfer syntaxes
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 f.write(a)