ospd.command.initsubclass   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A InitSubclassMeta.__new__() 0 6 2
A InitSubclassMeta.__init__() 0 5 2
1
# Copyright (C) 2014-2021 Greenbone Networks GmbH
2
#
3
# SPDX-License-Identifier: AGPL-3.0-or-later
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU Affero General Public License as
7
# published by the Free Software Foundation, either version 3 of the
8
# 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 Affero General Public License for more details.
14
#
15
# You should have received a copy of the GNU Affero General Public License
16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18
# pylint: disable=bad-mcs-classmethod-argument, no-member
19
20
_has_init_subclass = hasattr(  # pylint: disable=invalid-name
21
    type, "__init_subclass__"
22
)
23
24
if not _has_init_subclass:
25
26
    class InitSubclassMeta(type):
27
        """Metaclass that implements PEP 487 protocol"""
28
29
        def __new__(cls, name, bases, ns, **kwargs):
30
            __init_subclass__ = ns.pop("__init_subclass__", None)
31
            if __init_subclass__:
32
                __init_subclass__ = classmethod(__init_subclass__)
33
                ns["__init_subclass__"] = __init_subclass__
34
            return super().__new__(cls, name, bases, ns, **kwargs)
35
36
        def __init__(cls, name, bases, ns, **kwargs):
37
            super().__init__(name, bases, ns)
38
            super_class = super(cls, cls)
39
            if hasattr(super_class, "__init_subclass__"):
40
                super_class.__init_subclass__.__func__(cls, **kwargs)
41
42
43
else:
44
    InitSubclassMeta = type  # type: ignore
45