File: class_generator.py

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (759 lines) | stat: -rw-r--r-- 29,031 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
import os
import sys

import dicom_parser

# cspell: ignore Wundefined afterloader angio autorefraction autosequence boli bscan cadsr ciexy ctdi ctdivol decf
# cspell: ignore deidentification dermoscopic diconde dicos dvhroi exif fiducials fluence fnumber frameof gpsdop hicpp
# cspell: ignore hpgl imager inms inua inus iods isop ivol ivus keratoconus keratometer keratometric kerma lensometry
# cspell: ignore lookat meterset metersets mrfov multiway nmpet nmtomo nolintbegin nolintend octb octz oecf olink
# cspell: ignore postamble powerline prcs presaturation radiofluoroscopic relaxivity reprojection rgblut rtdvh rtroi
# cspell: ignore sddn sdhn sdvn softcopy soundpath stereometric stowrs tlhc tomo tomosynthesis tomotherapeutic toric
# cspell: ignore tractography xaxrf

g_root_dir = os.path.dirname(__file__)
g_test_dir = os.path.join(os.path.dirname(g_root_dir), "test", "tu", "dicom")
g_header_extension = ".hpp"
g_source_extension = ".cpp"
g_test = "Test"

g_attribute_class_name = "Attribute"
g_sop_class_name = "Sop"


def generate_cspell_ignore(max_line_length: int = 120) -> str:
    cspell_words = {
        "bscan", "frameof", "isop", "nmpet", "nmtomo", "rtdvh", "afterloader", "angio", "autorefraction",
        "autosequence", "boli", "cadsr", "ciexy", "ctdi", "ctdivol", "decf", "deidentification", "dermoscopic",
        "diconde", "dicos", "dvhroi", "exif", "fiducials", "fluence", "fnumber", "gpsdop", "hicpp", "hpgl", "imager",
        "inms", "inua", "inus", "ivol", "ivus", "keratoconus", "keratometer", "keratometric", "kerma", "lensometry",
        "lookat", "meterset", "metersets", "mrfov", "multiway", "nolintbegin", "nolintend", "octb", "octz", "oecf",
        "postamble", "powerline", "prcs", "presaturation", "radiofluoroscopic", "relaxivity", "reprojection", "rgblut",
        "rtroi", "sddn", "sdhn", "sdvn", "softcopy", "soundpath", "stereometric", "stowrs", "tlhc", "tomo",
        "tomosynthesis", "tomotherapeutic", "toric", "tractography", "Wundefined", "xaxrf", "olink", "iods"
    }

    cspell_preamble = "// cspell: ignore"
    cspell_lines = cspell_preamble
    cspell_line_length = len(cspell_lines)

    for cspell_word in sorted(cspell_words):
        if cspell_line_length + len(cspell_word) >= max_line_length:
            new_line = cspell_preamble + " " + cspell_word
            cspell_line_length = len(new_line)
            cspell_lines += "\n" + new_line
        else:
            new_word = " " + cspell_word
            cspell_line_length += len(new_word)
            cspell_lines += new_word

    return cspell_lines


g_cspell_ignore = generate_cspell_ignore()


def find_sight_root_dir() -> str:
    """Find the SIGHT root directory"""
    sight_root_dir = g_root_dir
    while not os.path.exists(os.path.join(sight_root_dir, ".sight")):
        sight_root_dir = os.path.dirname(sight_root_dir)
    return sight_root_dir


g_script_path = __file__.replace(find_sight_root_dir(), "").replace("\\", "/")


def generate_preamble(includes: str, header: bool = True, namespace: str = None) -> str:
    """Generate the preamble for the generated files"""
    preamble = f"// This file was automatically generated by the '{g_script_path}' script. DO NOT EDIT !\n"
    preamble += f"// Used arguments: '{dicom_parser.g_arguments}'\n\n"

    if header:
        preamble += "#pragma once\n\n"

    if len(includes) > 0:
        preamble += f"{includes}\n\n"

    preamble += f"{g_cspell_ignore}\n\n"

    preamble += "// NOLINTBEGIN\n\n"

    if namespace is None:
        preamble += "namespace sight::data::dicom::ut\n{\n\n"
    else:
        preamble += f"namespace sight::data::dicom::{namespace}\n{{\n\n"

    return preamble


def generate_footer(namespace: str = None) -> str:
    """Generate the footer for the generated files"""
    if namespace is None:
        string = "} // namespace sight::data::dicom::ut\n\n"
    else:
        string = f"}} // namespace sight::data::dicom::{namespace}\n\n"

    string += "// NOLINTEND\n"

    return string


def tag_to_uint16(tag: dicom_parser.Tag) -> dicom_parser.Tag:
    """Convert a tag to a uint16_t hex string"""
    return dicom_parser.Tag(f"0x{tag.group.replace('x', 'F')}", f"0x{tag.element.replace('x', 'F')}")


def write_attribute_hpp() -> None:
    """Write the attribute.hpp file."""
    with open(os.path.join(g_root_dir, g_attribute_class_name + g_header_extension), mode='w',
              encoding='utf-8') as attribute_hpp:
        # Preamble
        attribute_hpp.write(
            generate_preamble(
                includes=(
                    "#include \"data/config.hpp\"\n\n"
                    "#include <cstdint>\n"
                    "#include <string_view>"
                ),
                header=True,
                namespace=g_attribute_class_name.lower()
            )
        )

        # enum Keyword
        attribute_hpp.write(
            f"enum class Keyword : std::uint16_t\n"
            "{\n"
            f"    {dicom_parser.g_invalid} = 0,\n"
        )

        for tag in sorted(dicom_parser.g_filtered_attributes):
            attribute = dicom_parser.g_filtered_attributes[tag]
            attribute_hpp.write(f"    {attribute.keyword},\n")

        attribute_hpp.write(
            "    _SIZE\n"
            "};\n\n"
        )

        # enum VR
        attribute_hpp.write(
            "enum class VR : std::uint8_t\n"
            "{\n"
            f"    {dicom_parser.g_invalid} = 0,\n"
        )

        for vr in sorted(dicom_parser.g_vrs):
            attribute_hpp.write(f"    {vr},\n")

        attribute_hpp.write(
            "    _SIZE\n"
            "};\n\n"
        )

        # enum VM
        attribute_hpp.write(
            "enum class VM : std::uint8_t\n"
            "{\n"
            f"    {dicom_parser.g_invalid} = 0,\n"
        )

        for vm in sorted(dicom_parser.g_vms):
            attribute_hpp.write(f"    {vm},\n")

        attribute_hpp.write(
            "    _SIZE\n"
            "};\n\n"
        )

        # IAttribute class
        attribute_hpp.write(
            f"struct DATA_CLASS_API I{g_attribute_class_name}\n"
            "{\n"
            f"    constexpr I{g_attribute_class_name}(\n"
            f"        const Keyword& keyword,\n"
            "        const std::uint16_t& group,\n"
            "        const std::uint16_t& element,\n"
            "        const std::string_view& name,\n"
            "        const VR& vr,\n"
            "        const VM& vm\n"
            "    ) noexcept :\n"
            "        m_keyword(keyword),\n"
            "        m_group(group),\n"
            "        m_element(element),\n"
            "        m_name(name),\n"
            "        m_vr(vr),\n"
            "        m_vm(vm)\n"
            "    {\n"
            "    }\n\n"
            f"    constexpr operator Keyword() const noexcept\n"
            "    {\n"
            "        return m_keyword;\n"
            "    }\n\n"
            f"    constexpr auto operator==(const I{g_attribute_class_name}& other) const noexcept\n"
            "    {\n"
            "       return m_keyword == other.m_keyword;\n"
            "    }\n\n"
            f"    const Keyword& m_keyword;\n"
            "    const std::uint16_t& m_group;\n"
            "    const std::uint16_t& m_element;\n"
            "    const std::string_view& m_name;\n"
            "    const VR& m_vr;\n"
            "    const VM& m_vm;\n"
            "};\n\n"
        )

        # Attribute class
        attribute_hpp.write(
            f"template<Keyword>\n"
            f"struct DATA_CLASS_API {g_attribute_class_name} final : public I{g_attribute_class_name}\n"
            "{\n"
            f"    constexpr {g_attribute_class_name}() noexcept :\n"
            f"        I{g_attribute_class_name}(s_keyword, s_group, s_element, s_name, s_vr, s_vm)\n"
            "    {\n"
            "    }\n\n"
            f"    SIGHT_DATA_API static const Keyword s_keyword;\n"
            "    SIGHT_DATA_API static const std::uint16_t s_group;\n"
            "    SIGHT_DATA_API static const std::uint16_t s_element;\n"
            "    SIGHT_DATA_API static const std::string_view s_name;\n"
            "    SIGHT_DATA_API static const VR s_vr;\n"
            "    SIGHT_DATA_API static const VM s_vm;\n"
            "};\n\n"
        )

        # attribute(attribute::Keyword) and attribute(group, element) methods
        attribute_hpp.write(
            f"/// @return a static const reference to an {g_attribute_class_name}\n"
            "/// @{\n"
            f"SIGHT_DATA_API const I{g_attribute_class_name}& get(Keyword keyword);\n"
            f"SIGHT_DATA_API const I{g_attribute_class_name}& get(std::uint16_t group, std::uint16_t element);\n"
            "/// @}\n\n"
        )

        attribute_hpp.write(generate_footer(namespace=g_attribute_class_name.lower()))


def write_attribute_cpp() -> None:
    """Write the Attribute.cpp file."""
    with open(os.path.join(g_root_dir, g_attribute_class_name + g_source_extension), mode='w',
              encoding='utf-8') as attribute_cpp:
        attribute_cpp.write(
            generate_preamble(
                includes=(
                    f"#include \"data/dicom/{g_attribute_class_name + g_header_extension}\"\n"
                    "#include <core/exceptionmacros.hpp>"
                ),
                header=False,
                namespace=g_attribute_class_name.lower()
            )
        )

        # get<Keyword::xxx>()
        attribute_cpp.write(
            f"template<Keyword keyword>\n"
            f"inline static const {g_attribute_class_name}<keyword>& get()\n"
            "{\n"
            f"    static constexpr {g_attribute_class_name}<keyword> instance;\n"
            "    return instance;\n"
            "}\n"
        )

        attribute_cpp.write(
            "\n//------------------------------------------------------------------------------\n\n"
        )

        # Attribute<Keyword::xxx>::s_yyy = zzz
        for tag in sorted(dicom_parser.g_filtered_attributes):
            attribute = dicom_parser.g_filtered_attributes[tag]
            uint16_tag = tag_to_uint16(tag)

            attribute_cpp.write(
                f"template<> SIGHT_DATA_API const Keyword {g_attribute_class_name}<Keyword::{attribute.keyword}>::s_keyword = Keyword::{attribute.keyword};\n"
                f"template<> SIGHT_DATA_API const std::uint16_t {g_attribute_class_name}<Keyword::{attribute.keyword}>::s_group = {uint16_tag.group};\n"
                f"template<> SIGHT_DATA_API const std::uint16_t {g_attribute_class_name}<Keyword::{attribute.keyword}>::s_element = {uint16_tag.element};\n"
                f"template<> SIGHT_DATA_API const std::string_view {g_attribute_class_name}<Keyword::{attribute.keyword}>::s_name = \"{attribute.name}\";\n"
                f"template<> SIGHT_DATA_API const VR {g_attribute_class_name}<Keyword::{attribute.keyword}>::s_vr = VR::{attribute.vr};\n"
                f"template<> SIGHT_DATA_API const VM {g_attribute_class_name}<Keyword::{attribute.keyword}>::s_vm = VM::{attribute.vm};\n"
            )

        attribute_cpp.write(
            "\n//------------------------------------------------------------------------------\n\n"
        )

        # get(Keyword)
        attribute_cpp.write(
            f"const I{g_attribute_class_name}& get(Keyword keyword)\n"
            "{\n"
            "    switch(keyword)\n"
            "    {\n"
        )

        for tag in sorted(dicom_parser.g_filtered_attributes):
            attribute = dicom_parser.g_filtered_attributes[tag]
            attribute_cpp.write(
                f"        case Keyword::{attribute.keyword}: return get<Keyword::{attribute.keyword}>();\n"
            )

        attribute_cpp.write(
            "        default:\n"
            f"            SIGHT_THROW(\"Unknown {g_attribute_class_name} Keyword: '\" << std::uint16_t(keyword) << \"'.\");\n"
            "    }\n"
        )

        attribute_cpp.write(
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
        )

        # get(group, element)
        attribute_cpp.write(
            f"const I{g_attribute_class_name}& get(std::uint16_t group, std::uint16_t element)\n"
            "{\n"
            "    // Special case for groups in the range 0x6000-0x60FF for attribute (60xx, xxxx)\n"
            "    if(group > 0x6000 && group <= 0x60FF)\n"
            "    {\n"
            "        group = 0x6000;\n"
            "    }\n\n"
            "    switch(group)\n"
            "    {\n"
        )

        # Order by group and then by element
        attribute_by_group_by_element = dict[str, dict[str, dicom_parser.Attribute]]()
        for tag in sorted(dicom_parser.g_filtered_attributes):
            attribute = dicom_parser.g_filtered_attributes[tag]
            uint16_tag = tag_to_uint16(tag)

            if uint16_tag.group not in attribute_by_group_by_element:
                attribute_by_group_by_element[uint16_tag.group] = dict[str, dicom_parser.Attribute]()

            attribute_by_group_by_element[uint16_tag.group][uint16_tag.element] = attribute

        # For each group
        for group in sorted(attribute_by_group_by_element):
            elements = attribute_by_group_by_element[group]

            attribute_cpp.write(
                f"        case {group}:\n"
                "        {\n"
                "            switch(element)\n"
                "            {\n"
            )

            for element in sorted(elements):
                attribute = elements[element]
                attribute_cpp.write(
                    f"            case {element}: return get<Keyword::{attribute.keyword}>();\n"
                )

            attribute_cpp.write(
                "            default:\n"
                "                SIGHT_THROW(\"Unknown element: '\" << element << \"'.\");\n"
                "            }\n"
                "        }\n\n"
            )

        attribute_cpp.write(
            "        default:\n"
            "            SIGHT_THROW(\"Unknown group: '\" << group << \"'.\");\n"
            "    }\n"
        )

        attribute_cpp.write(
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
        )

        attribute_cpp.write(generate_footer(namespace=g_attribute_class_name.lower()))


def write_sop_hpp() -> None:
    """Write the sop.hpp file."""
    with open(os.path.join(g_root_dir, g_sop_class_name + g_header_extension), mode='w', encoding='utf-8') as sop_hpp:
        # Preamble
        sop_hpp.write(
            generate_preamble(
                includes=(
                    "#include \"data/config.hpp\"\n"
                    "#include <cstdint>\n"
                    "#include <string_view>"
                ),
                header=True,
                namespace=g_sop_class_name.lower()
            )
        )

        # enum Keyword
        sop_hpp.write(
            f"enum class Keyword : std::uint8_t\n"
            "{\n"
            f"    {dicom_parser.g_invalid} = 0,\n"
        )

        for uid in sorted(dicom_parser.g_filtered_sops):
            sop_hpp.write(f"    {uid.keyword},\n")

        sop_hpp.write(
            "    _SIZE\n"
            "};\n\n"
        )

        # ISop
        sop_hpp.write(
            f"struct DATA_CLASS_API I{g_sop_class_name}\n"
            "{\n"
            f"    constexpr I{g_sop_class_name}(\n"
            f"        const Keyword& keyword,\n"
            "        const std::string_view& name,\n"
            "        const std::string_view& iud,\n"
            "        const std::string_view& iod\n"
            "    ) noexcept :\n"
            "        m_keyword(keyword),\n"
            "        m_name(name),\n"
            "        m_uid(iud),\n"
            "        m_iod(iod)\n"
            "    {\n"
            "    }\n\n"
            f"    constexpr operator Keyword() const noexcept\n"
            "    {\n"
            "        return m_keyword;\n"
            "    }\n\n"
            f"    constexpr auto operator==(const I{g_sop_class_name}& other) const noexcept\n"
            "    {\n"
            "       return m_keyword == other.m_keyword;\n"
            "    };\n\n"
            f"    const Keyword& m_keyword;\n"
            "    const std::string_view& m_name;\n"
            "    const std::string_view& m_uid;\n"
            "    const std::string_view& m_iod;\n"
            "};\n\n"
        )

        # Sop
        sop_hpp.write(
            f"template<Keyword>\n"
            f"struct DATA_CLASS_API {g_sop_class_name} final : public I{g_sop_class_name}\n"
            "{\n"
            f"    constexpr {g_sop_class_name}() noexcept :\n"
            f"        I{g_sop_class_name}(s_keyword, s_name, s_uid, s_iod)\n"
            "    {\n"
            "    }\n\n"
            f"    SIGHT_DATA_API static const Keyword s_keyword;\n"
            "    SIGHT_DATA_API static const std::string_view s_name;\n"
            "    SIGHT_DATA_API static const std::string_view s_uid;\n"
            "    SIGHT_DATA_API static const std::string_view s_iod;\n"
            "};\n\n"
        )

        # get(Keyword) and keyword(uid)
        sop_hpp.write(
            f"/// @return a static const reference to an {g_sop_class_name}\n"
            "/// @{\n"
            f"SIGHT_DATA_API const I{g_sop_class_name}& get(Keyword keyword);\n"
            f"SIGHT_DATA_API const I{g_sop_class_name}& get(const std::string_view& uid);\n"
            "/// @}\n\n"
            "/// convert a uid directly to a keyword\n"
            "SIGHT_DATA_API const Keyword& keyword(const std::string_view& uid) noexcept;\n\n"
        )

        sop_hpp.write(generate_footer(namespace=g_sop_class_name.lower()))


def write_sop_cpp() -> None:
    """Write the Sop.cpp file."""
    with open(os.path.join(g_root_dir, g_sop_class_name + g_source_extension), mode='w', encoding='utf-8') as sop_cpp:
        sop_cpp.write(
            generate_preamble(
                includes=(
                    f"#include \"data/dicom/{g_sop_class_name + g_header_extension}\"\n"
                    "#include <core/exceptionmacros.hpp>\n"
                    "#include <unordered_map>"
                ),
                header=False,
                namespace=g_sop_class_name.lower()
            )
        )

        # get<Keyword::xxx>()
        sop_cpp.write(
            f"template<Keyword keyword>\n"
            f"inline static const {g_sop_class_name}<keyword>& get()\n"
            "{\n"
            f"    static constexpr {g_sop_class_name}<keyword> instance;\n"
            "    return instance;\n"
            "}\n"
        )

        sop_cpp.write(
            "\n//------------------------------------------------------------------------------\n\n"
        )

        # Sop<Keyword::xxx>::s_yyy = zzz
        for uid in sorted(dicom_parser.g_filtered_sops):
            sop = dicom_parser.g_filtered_sops[uid]
            iod = dicom_parser.g_filtered_iods[sop.iod.keyword]
            sop_cpp.write(
                f"template<> SIGHT_DATA_API const Keyword {g_sop_class_name}<Keyword::{uid.keyword}>::s_keyword = Keyword::{uid.keyword};\n"
                f"template<> SIGHT_DATA_API const std::string_view {g_sop_class_name}<Keyword::{uid.keyword}>::s_name = \"{uid.name}\";\n"
                f"template<> SIGHT_DATA_API const std::string_view {g_sop_class_name}<Keyword::{uid.keyword}>::s_uid = \"{uid.value}\";\n"
                f"template<> SIGHT_DATA_API const std::string_view {g_sop_class_name}<Keyword::{uid.keyword}>::s_iod = \"{iod.name}\";\n"
            )

        sop_cpp.write(
            "\n//------------------------------------------------------------------------------\n\n"
        )

        # get(Keyword)
        sop_cpp.write(
            f"const I{g_sop_class_name}& get(Keyword keyword)\n"
            "{\n"
            "    switch(keyword)\n"
            "    {\n"
        )

        for uid in sorted(dicom_parser.g_filtered_sops):
            sop_cpp.write(
                f"        case Keyword::{uid.keyword}: return get<Keyword::{uid.keyword}>();\n"
            )

        sop_cpp.write(
            "        default:\n"
            f"            SIGHT_THROW(\"Unknown {g_sop_class_name} Keyword: '\" << std::uint8_t(keyword) << \"'.\");\n"
            "    }\n"
        )

        sop_cpp.write(
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
        )

        # get(uid)
        sop_cpp.write(
            f"const I{g_sop_class_name}& get(const std::string_view& uid)\n"
            "{\n"
            f"    static const std::unordered_map<std::string_view, const I{g_sop_class_name}&> {g_sop_class_name.upper()}_MAP {{\n"
        )

        for uid in sorted(dicom_parser.g_filtered_sops):
            sop_cpp.write(
                f"        {{{g_sop_class_name}<Keyword::{uid.keyword}>::s_uid, get<Keyword::{uid.keyword}>()}},\n"
            )

        sop_cpp.write(
            "    };\n\n"
            f"    return {g_sop_class_name.upper()}_MAP.at(uid);\n"
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
        )

        sop_cpp.write(
            f"const Keyword& keyword(const std::string_view& uid) noexcept\n"
            "{\n"
            "    try\n"
            "    {\n"
            f"        return get(uid).m_keyword;\n"
            "    }\n"
            "    catch(...)\n"
            "    {\n"
            "        static const auto invalid = Keyword::INVALID;\n"
            "        return invalid;\n"
            "    }\n"
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
        )

        sop_cpp.write(generate_footer(namespace=g_sop_class_name.lower()))


def write_attribute_test_hpp() -> None:
    """Write the AttributeTest.hpp file."""
    with open(
            os.path.join(g_test_dir, g_attribute_class_name + g_test + g_header_extension),
            mode='w',
            encoding='utf-8'
    ) as attribute_test_hpp:
        attribute_test_hpp.write(
            generate_preamble(
                includes=(
                    "#include <cppunit/extensions/HelperMacros.h>"
                ),
                header=True
            )
        )

        # Test suite / methods
        attribute_test_hpp.write(
            f"class {g_attribute_class_name}{g_test} : public CPPUNIT_NS::TestFixture\n"
            "{\n"
            f"    CPPUNIT_TEST_SUITE({g_attribute_class_name}{g_test});\n"
            f"    CPPUNIT_TEST({g_attribute_class_name.lower()}{g_test});\n"
            "    CPPUNIT_TEST_SUITE_END();\n\n"
            "public:\n"
            "    void setUp() override;\n"
            "    void tearDown() override;\n\n"
            f"    static void {g_attribute_class_name.lower()}{g_test}();\n"
            "};\n\n"
        )

        attribute_test_hpp.write(generate_footer())


def write_attribute_test_cpp() -> None:
    """Write the AttributeTest.cpp file."""
    with open(
            os.path.join(g_test_dir, g_attribute_class_name + g_test + g_source_extension),
            mode='w',
            encoding='utf-8'
    ) as attribute_test_cpp:
        attribute_test_cpp.write(
            generate_preamble(
                includes=(
                    f"#include \"{g_attribute_class_name}{g_test}{g_header_extension}\"\n\n"
                    f"#include <data/dicom/{g_attribute_class_name}{g_header_extension}>\n\n"
                    f"CPPUNIT_TEST_SUITE_REGISTRATION(sight::data::dicom::ut::{g_attribute_class_name}{g_test});"
                ),
                header=False
            )
        )

        attribute_test_cpp.write(
            f"void {g_attribute_class_name}{g_test}::setUp()\n"
            "{\n"
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
            f"void {g_attribute_class_name}{g_test}::tearDown()\n"
            "{\n"
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
        )

        attribute_test_cpp.write(
            f"void {g_attribute_class_name}{g_test}::{g_attribute_class_name.lower()}{g_test}()\n"
            "{\n"
        )

        for tag in sorted(dicom_parser.g_filtered_attributes):
            attribute = dicom_parser.g_filtered_attributes[tag]
            uint16_tag = tag_to_uint16(tag)

            attribute_test_cpp.write(
                f"    CPPUNIT_ASSERT_EQUAL({g_attribute_class_name.lower()}::{g_attribute_class_name}<{g_attribute_class_name.lower()}::Keyword::{attribute.keyword}>::s_keyword, {g_attribute_class_name.lower()}::get({uint16_tag.group}, {uint16_tag.element}).m_keyword);\n"
                f"    CPPUNIT_ASSERT_EQUAL({g_attribute_class_name.lower()}::{g_attribute_class_name}<{g_attribute_class_name.lower()}::Keyword::{attribute.keyword}>::s_keyword, {g_attribute_class_name.lower()}::get({g_attribute_class_name.lower()}::Keyword::{attribute.keyword}).m_keyword);\n"
            )

        attribute_test_cpp.write(
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
        )

        attribute_test_cpp.write(generate_footer())


def write_sop_test_hpp() -> None:
    """Write the SopTest.hpp file."""
    with open(
            os.path.join(g_test_dir, g_sop_class_name + g_test + g_header_extension),
            mode='w',
            encoding='utf-8'
    ) as sop_test_hpp:
        sop_test_hpp.write(
            generate_preamble(
                includes=(
                    "#include <cppunit/extensions/HelperMacros.h>"
                ),
                header=True
            )
        )

        # Test suite / methods
        sop_test_hpp.write(
            f"class {g_sop_class_name}{g_test} : public CPPUNIT_NS::TestFixture\n"
            "{\n"
            f"    CPPUNIT_TEST_SUITE({g_sop_class_name}{g_test});\n"
            f"    CPPUNIT_TEST({g_sop_class_name.lower()}{g_test});\n"
            "    CPPUNIT_TEST_SUITE_END();\n\n"
            "public:\n"
            "    void setUp() override;\n"
            "    void tearDown() override;\n\n"
            f"    static void {g_sop_class_name.lower()}{g_test}();\n"
            "};\n\n"
        )

        sop_test_hpp.write(generate_footer())


def write_sop_test_cpp() -> None:
    """Write the AttributeTest.cpp file."""
    with open(
            os.path.join(g_test_dir, g_sop_class_name + g_test + g_source_extension),
            mode='w',
            encoding='utf-8'
    ) as sop_test_cpp:
        sop_test_cpp.write(
            generate_preamble(
                includes=(
                    f"#include \"{g_sop_class_name}{g_test}{g_header_extension}\"\n\n"
                    f"#include <data/dicom/{g_sop_class_name}{g_header_extension}>\n\n"
                    f"CPPUNIT_TEST_SUITE_REGISTRATION(sight::data::dicom::ut::{g_sop_class_name}{g_test});"
                ),
                header=False
            )
        )

        sop_test_cpp.write(
            f"void {g_sop_class_name}{g_test}::setUp()\n"
            "{\n"
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
            f"void {g_sop_class_name}{g_test}::tearDown()\n"
            "{\n"
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
        )

        sop_test_cpp.write(
            f"void {g_sop_class_name}{g_test}::{g_sop_class_name.lower()}{g_test}()\n"
            "{\n"
        )

        for uid in sorted(dicom_parser.g_filtered_sops):
            sop_test_cpp.write(
                f"    CPPUNIT_ASSERT_EQUAL({g_sop_class_name.lower()}::{g_sop_class_name}<{g_sop_class_name.lower()}::Keyword::{uid.keyword}>::s_keyword, {g_sop_class_name.lower()}::get({g_sop_class_name.lower()}::Keyword::{uid.keyword}).m_keyword);\n"
                f"    CPPUNIT_ASSERT_EQUAL({g_sop_class_name.lower()}::{g_sop_class_name}<{g_sop_class_name.lower()}::Keyword::{uid.keyword}>::s_keyword, {g_sop_class_name.lower()}::get(\"{uid.value}\").m_keyword);\n"
            )

        sop_test_cpp.write(
            "}\n\n"
            "//------------------------------------------------------------------------------\n\n"
        )

        sop_test_cpp.write(generate_footer())


def main():
    """Main function"""

    result = dicom_parser.main()

    # Attribute
    write_attribute_hpp()
    write_attribute_cpp()

    # Attribute tests
    write_attribute_test_hpp()
    write_attribute_test_cpp()

    # Sop
    write_sop_hpp()
    write_sop_cpp()

    # Sop tests
    write_sop_test_hpp()
    write_sop_test_cpp()

    return result


if __name__ == "__main__":
    sys.exit(main())