Debian Codegraph @

lib/ansible/module_utils/_internal/_no_six.py

raw
1 from __future__ import annotations
2
3 import sys
4 import types
5
6 from ansible.module_utils.common import warnings
7
8
9 # INLINED FROM THE SIX LIBRARY, see lib/ansible/module_utils/six/__init__.py
10 # Copyright (c) 2010-2024 Benjamin Peterson
11 def with_metaclass(meta, *bases):
12 """Create a base class with a metaclass."""
13
14 # This requires a bit of explanation: the basic idea is to make a dummy
15 # metaclass for one level of class instantiation that replaces itself with
16 # the actual metaclass.
17 class metaclass(type):
18
19 def __new__(cls, name, this_bases, d):
20 if sys.version_info[:2] >= (3, 7):
21 # This version introduced PEP 560 that requires a bit
22 # of extra care (we mimic what is done by __build_class__).
23 resolved_bases = types.resolve_bases(bases)
24 if resolved_bases is not bases:
25 d['__orig_bases__'] = bases
26 else:
27 resolved_bases = bases
28 return meta(name, resolved_bases, d)
29
30 @classmethod
31 def __prepare__(cls, name, this_bases):
32 return meta.__prepare__(name, bases)
33
34 return type.__new__(metaclass, 'temporary_class', (), {})
35
36
37 def add_metaclass(metaclass):
38 """Class decorator for creating a class with a metaclass."""
39
40 def wrapper(cls):
41 orig_vars = cls.__dict__.copy()
42 slots = orig_vars.get('__slots__')
43 if slots is not None:
44 if isinstance(slots, str):
45 slots = [slots]
46 for slots_var in slots:
47 orig_vars.pop(slots_var)
48 orig_vars.pop('__dict__', None)
49 orig_vars.pop('__weakref__', None)
50 if hasattr(cls, '__qualname__'):
51 orig_vars['__qualname__'] = cls.__qualname__
52 return metaclass(cls.__name__, cls.__bases__, orig_vars)
53
54 return wrapper
55
56
57 def iteritems(d, **kw):
58 return iter(d.items(**kw))
59
60
61 _mini_six = {
62 "PY2": False,
63 "PY3": True,
64 "text_type": str,
65 "binary_type": bytes,
66 "string_types": (str,),
67 "integer_types": (int,),
68 "iteritems": iteritems,
69 "add_metaclass": add_metaclass,
70 "with_metaclass": with_metaclass,
71 }
72 # INLINED SIX END
73
74
75 def deprecate(importable_name: str, module_name: str, *deprecated_args) -> object:
76 """Inject import-time deprecation warnings."""
77 if not (importable_name in deprecated_args and (importable := _mini_six.get(importable_name, ...) is not ...)):
78 raise AttributeError(f"module {module_name!r} has no attribute {importable_name!r}")
79
80 # TODO Inspect and remove all calls to this function in 2.24
81 warnings.deprecate(
82 msg=f"Importing {importable_name!r} from {module_name!r} is deprecated.",
83 version="2.24",
84 )
85
86 return importable