1# <pep8-80 compliant>
2
3# ##### BEGIN GPL LICENSE BLOCK #####
4#
5#  This program is free software; you can redistribute it and/or
6#  modify it under the terms of the GNU General Public License
7#  as published by the Free Software Foundation; either version 2
8#  of the License, or (at your option) any later version.
9#
10#  This program is distributed in the hope that it will be useful,
11#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#  GNU General Public License for more details.
14#
15#  You should have received a copy of the GNU General Public License
16#  along with this program; if not, write to the Free Software Foundation,
17#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18#
19# ##### END GPL LICENSE BLOCK #####
20
21__author__ = "Nutti <nutti.metro@gmail.com>"
22__status__ = "production"
23__version__ = "6.3"
24__date__ = "10 Aug 2020"
25
26import bpy
27
28from .. import common
29
30
31class BlClassRegistry:
32    class_list = []
33
34    def __init__(self, *_, **kwargs):
35        self.legacy = kwargs.get('legacy', False)
36
37    def __call__(self, cls):
38        if hasattr(cls, "bl_idname"):
39            BlClassRegistry.add_class(cls.bl_idname, cls, self.legacy)
40        elif hasattr(cls, "bl_context"):
41            bl_idname = "{}{}{}{}".format(cls.bl_space_type,
42                                          cls.bl_region_type,
43                                          cls.bl_context, cls.bl_label)
44            BlClassRegistry.add_class(bl_idname, cls, self.legacy)
45        else:
46            bl_idname = "{}{}{}".format(cls.bl_space_type,
47                                        cls.bl_region_type,
48                                        cls.bl_label)
49            BlClassRegistry.add_class(bl_idname, cls, self.legacy)
50        return cls
51
52    @classmethod
53    def add_class(cls, bl_idname, op_class, legacy):
54        for class_ in cls.class_list:
55            if (class_["bl_idname"] == bl_idname) and \
56               (class_["legacy"] == legacy):
57                raise RuntimeError("{} is already registered"
58                                   .format(bl_idname))
59
60        new_op = {
61            "bl_idname": bl_idname,
62            "class": op_class,
63            "legacy": legacy,
64        }
65        cls.class_list.append(new_op)
66        common.debug_print("{} is registered.".format(bl_idname))
67
68    @classmethod
69    def register(cls):
70        for class_ in cls.class_list:
71            bpy.utils.register_class(class_["class"])
72            common.debug_print("{} is registered to Blender."
73                               .format(class_["bl_idname"]))
74
75    @classmethod
76    def unregister(cls):
77        for class_ in cls.class_list:
78            bpy.utils.unregister_class(class_["class"])
79            common.debug_print("{} is unregistered from Blender."
80                               .format(class_["bl_idname"]))
81
82    @classmethod
83    def cleanup(cls):
84        cls.class_list = []
85        common.debug_print("Cleanup registry.")
86