cengal.unittest.patcher.versions.v_0.patcher

 1#!/usr/bin/env python
 2# coding=utf-8
 3
 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
 5# 
 6# Licensed under the Apache License, Version 2.0 (the "License");
 7# you may not use this file except in compliance with the License.
 8# You may obtain a copy of the License at
 9# 
10#     http://www.apache.org/licenses/LICENSE-2.0
11# 
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18from contextlib import contextmanager
19from typing import List, Tuple, Optional, Any, Dict, cast
20
21"""
22Module Docstring
23Docstrings: http://www.python.org/dev/peps/pep-0257/
24"""
25
26__author__ = "ButenkoMS <gtalk@butenkoms.space>"
27__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
28__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
29__license__ = "Apache License, Version 2.0"
30__version__ = "4.4.1"
31__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
32__email__ = "gtalk@butenkoms.space"
33# __status__ = "Prototype"
34__status__ = "Development"
35# __status__ = "Production"
36
37EntityExactHolder = Any
38EntityHolder = Optional[Any]
39EntityName = str
40OriginalExactEntity = Tuple[EntityExactHolder, EntityName]
41OriginalEntity = Tuple[EntityHolder, EntityName]
42EntityMock = Any
43
44@contextmanager
45def patch_entity(original: OriginalExactEntity, mock: EntityMock):
46    holder, name = original
47    buff_original_value = getattr(holder, name)
48    setattr(holder, name, mock)
49    try:
50        yield buff_original_value
51    finally:
52        setattr(holder, name, buff_original_value)
53
54@contextmanager
55def patch_builtins(name: EntityName, mock: EntityMock):
56    buff_original_value = __builtins__[name]
57    __builtins__[name] = mock
58    try:
59        yield buff_original_value
60    finally:
61        __builtins__[name] = buff_original_value
62
63@contextmanager
64def patch(original: OriginalEntity, mock: EntityMock):
65    holder, name = original
66    if holder is None:
67        return patch_builtins(name, mock)
68    else:
69        return patch_entity(cast(OriginalExactEntity, original), mock)
70        
71@contextmanager
72def patch_set(patch_set: Dict[OriginalEntity, EntityMock]):
73    buff = dict()
74    for original, mock in patch_set.items():
75        original: OriginalEntity = original
76        mock: EntityMock = mock
77        holder, name = original
78        holder: EntityHolder = holder
79        name: EntityName = name
80        if holder is None:
81            buff[original] = __builtins__[name]
82            __builtins__[name] = mock
83        else:
84            buff[original] = getattr(holder, name)
85            setattr(holder, name, mock)
86    try:
87        yield buff
88    finally:
89        for original, mock in patch_set.items():
90            holder, name = original
91            if holder is None:
92                __builtins__[name] = buff[original]
93            else:
94                setattr(holder, name, buff[original])
95        
EntityExactHolder = typing.Any
EntityHolder = typing.Union[typing.Any, NoneType]
EntityName = <class 'str'>
OriginalExactEntity = typing.Tuple[typing.Any, str]
OriginalEntity = typing.Tuple[typing.Union[typing.Any, NoneType], str]
EntityMock = typing.Any
@contextmanager
def patch_entity(original: Tuple[Any, str], mock: Any):
45@contextmanager
46def patch_entity(original: OriginalExactEntity, mock: EntityMock):
47    holder, name = original
48    buff_original_value = getattr(holder, name)
49    setattr(holder, name, mock)
50    try:
51        yield buff_original_value
52    finally:
53        setattr(holder, name, buff_original_value)
@contextmanager
def patch_builtins(name: str, mock: Any):
55@contextmanager
56def patch_builtins(name: EntityName, mock: EntityMock):
57    buff_original_value = __builtins__[name]
58    __builtins__[name] = mock
59    try:
60        yield buff_original_value
61    finally:
62        __builtins__[name] = buff_original_value
@contextmanager
def patch(original: Tuple[Union[Any, NoneType], str], mock: Any):
64@contextmanager
65def patch(original: OriginalEntity, mock: EntityMock):
66    holder, name = original
67    if holder is None:
68        return patch_builtins(name, mock)
69    else:
70        return patch_entity(cast(OriginalExactEntity, original), mock)
@contextmanager
def patch_set(patch_set: Dict[Tuple[Union[Any, NoneType], str], Any]):
72@contextmanager
73def patch_set(patch_set: Dict[OriginalEntity, EntityMock]):
74    buff = dict()
75    for original, mock in patch_set.items():
76        original: OriginalEntity = original
77        mock: EntityMock = mock
78        holder, name = original
79        holder: EntityHolder = holder
80        name: EntityName = name
81        if holder is None:
82            buff[original] = __builtins__[name]
83            __builtins__[name] = mock
84        else:
85            buff[original] = getattr(holder, name)
86            setattr(holder, name, mock)
87    try:
88        yield buff
89    finally:
90        for original, mock in patch_set.items():
91            holder, name = original
92            if holder is None:
93                __builtins__[name] = buff[original]
94            else:
95                setattr(holder, name, buff[original])