|
| 1 | +""" |
| 2 | +The tool to check the availability or syntax of domain, IP or URL. |
| 3 | +
|
| 4 | +:: |
| 5 | +
|
| 6 | +
|
| 7 | + ██████╗ ██╗ ██╗███████╗██╗ ██╗███╗ ██╗ ██████╗███████╗██████╗ ██╗ ███████╗ |
| 8 | + ██╔══██╗╚██╗ ██╔╝██╔════╝██║ ██║████╗ ██║██╔════╝██╔════╝██╔══██╗██║ ██╔════╝ |
| 9 | + ██████╔╝ ╚████╔╝ █████╗ ██║ ██║██╔██╗ ██║██║ █████╗ ██████╔╝██║ █████╗ |
| 10 | + ██╔═══╝ ╚██╔╝ ██╔══╝ ██║ ██║██║╚██╗██║██║ ██╔══╝ ██╔══██╗██║ ██╔══╝ |
| 11 | + ██║ ██║ ██║ ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗ |
| 12 | + ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝ |
| 13 | +
|
| 14 | +Provides the base of all extra handlers. |
| 15 | +
|
| 16 | +Author: |
| 17 | + Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom |
| 18 | +
|
| 19 | +Special thanks: |
| 20 | + https://pyfunceble.github.io/#/special-thanks |
| 21 | +
|
| 22 | +Contributors: |
| 23 | + https://pyfunceble.github.io/#/contributors |
| 24 | +
|
| 25 | +Project link: |
| 26 | + https://github.com/funilrys/PyFunceble |
| 27 | +
|
| 28 | +Project documentation: |
| 29 | + https://pyfunceble.readthedocs.io/en/dev/ |
| 30 | +
|
| 31 | +Project homepage: |
| 32 | + https://pyfunceble.github.io/ |
| 33 | +
|
| 34 | +License: |
| 35 | +:: |
| 36 | +
|
| 37 | +
|
| 38 | + Copyright 2017, 2018, 2019, 2020, 2022 Nissar Chababy |
| 39 | +
|
| 40 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 41 | + you may not use this file except in compliance with the License. |
| 42 | + You may obtain a copy of the License at |
| 43 | +
|
| 44 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 45 | +
|
| 46 | + Unless required by applicable law or agreed to in writing, software |
| 47 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 48 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 49 | + See the License for the specific language governing permissions and |
| 50 | + limitations under the License. |
| 51 | +""" |
| 52 | + |
| 53 | +import functools |
| 54 | +from typing import Callable, Optional |
| 55 | + |
| 56 | +import PyFunceble.factory |
| 57 | +from PyFunceble.checker.availability.status import AvailabilityCheckerStatus |
| 58 | + |
| 59 | + |
| 60 | +class ExtraRuleHandlerBase: |
| 61 | + """ |
| 62 | + Provides the base of all extra rules handler. |
| 63 | +
|
| 64 | + :param statatus: |
| 65 | + The previously gathered status. |
| 66 | + :type status: |
| 67 | + :class:`~PyFunceble.checker.availability.status.AvailabilityCheckerStatus` |
| 68 | + """ |
| 69 | + |
| 70 | + _status: Optional[AvailabilityCheckerStatus] = None |
| 71 | + |
| 72 | + def __init__(self, status: Optional[AvailabilityCheckerStatus] = None) -> None: |
| 73 | + if status is not None: |
| 74 | + self.status = status |
| 75 | + |
| 76 | + # Be sure that all settings are loaded proprely!! |
| 77 | + PyFunceble.factory.Requester.guess_all_settings() |
| 78 | + |
| 79 | + def ensure_status_is_given( |
| 80 | + func: Callable[..., "ExtraRuleHandlerBase"] |
| 81 | + ): # pylint: disable=no-self-argument |
| 82 | + """ |
| 83 | + Ensures that the status is given before running the decorated method. |
| 84 | +
|
| 85 | + :raise TypeError: |
| 86 | + If the subject is not a string. |
| 87 | + """ |
| 88 | + |
| 89 | + @functools.wraps(func) |
| 90 | + def wrapper(self, *args, **kwargs): # pragma: no cover ## Safety! |
| 91 | + if not self.status: |
| 92 | + raise TypeError( |
| 93 | + f"<self.status> should be {AvailabilityCheckerStatus}, " |
| 94 | + f"{type(self.status)} given." |
| 95 | + ) |
| 96 | + |
| 97 | + return func(self, *args, **kwargs) # pylint: disable=not-callable |
| 98 | + |
| 99 | + return wrapper |
| 100 | + |
| 101 | + def setup_status_before( |
| 102 | + func: Callable[..., "ExtraRuleHandlerBase"] |
| 103 | + ): # pylint: disable=no-self-argument |
| 104 | + """ |
| 105 | + Ensures that the status is given before running the decorated method. |
| 106 | +
|
| 107 | + :raise TypeError: |
| 108 | + If the subject is not a string. |
| 109 | + """ |
| 110 | + |
| 111 | + @functools.wraps(func) |
| 112 | + def wrapper(self, *args, **kwargs): # pragma: no cover ## Safety! |
| 113 | + self.status.status_before_extra_rules = self.status.status |
| 114 | + self.status.status_source_before_extra_rules = self.status.status_source |
| 115 | + |
| 116 | + return func(self, *args, **kwargs) # pylint: disable=not-callable |
| 117 | + |
| 118 | + return wrapper |
| 119 | + |
| 120 | + def setup_status_after( |
| 121 | + func: Callable[..., "ExtraRuleHandlerBase"] |
| 122 | + ): # pylint: disable=no-self-argument |
| 123 | + """ |
| 124 | + Ensures that the status is given before running the decorated method. |
| 125 | +
|
| 126 | + :raise TypeError: |
| 127 | + If the subject is not a string. |
| 128 | + """ |
| 129 | + |
| 130 | + @functools.wraps(func) |
| 131 | + def wrapper(self, *args, **kwargs): # pragma: no cover ## Safety! |
| 132 | + result = func(self, *args, **kwargs) # pylint: disable=not-callable |
| 133 | + |
| 134 | + if self.status.status_after_extra_rules: |
| 135 | + self.status.status = self.status.status_after_extra_rules |
| 136 | + self.status.status_source = self.status.status_source_after_extra_rules |
| 137 | + |
| 138 | + PyFunceble.facility.Logger.info( |
| 139 | + "Could define the status of %r from our own set of rules.", |
| 140 | + self.status.idna_subject, |
| 141 | + ) |
| 142 | + else: |
| 143 | + self.status.status_before_extra_rules = None |
| 144 | + self.status.status_source_before_extra_rules = None |
| 145 | + self.status.status_after_extra_rules = None |
| 146 | + self.status.status_source_after_extra_rules = None |
| 147 | + |
| 148 | + return result |
| 149 | + |
| 150 | + return wrapper |
| 151 | + |
| 152 | + @property |
| 153 | + def status(self) -> Optional[AvailabilityCheckerStatus]: |
| 154 | + """ |
| 155 | + Provides the current state of the :code:`_status` attribute. |
| 156 | + """ |
| 157 | + |
| 158 | + return self._status |
| 159 | + |
| 160 | + @status.setter |
| 161 | + def status(self, value: AvailabilityCheckerStatus) -> None: |
| 162 | + """ |
| 163 | + Sets the status to work with. |
| 164 | +
|
| 165 | + :param value: |
| 166 | + The status to work with. |
| 167 | +
|
| 168 | + :raise TypeError: |
| 169 | + When the given :code:`value` is not a |
| 170 | + :class:`~PyFunceble.checker.availability.status.AvailabilityCheckerStatus`. |
| 171 | + """ |
| 172 | + |
| 173 | + if not isinstance(value, AvailabilityCheckerStatus): |
| 174 | + raise TypeError( |
| 175 | + f"<value> should be {AvailabilityCheckerStatus}, {type(value)} given." |
| 176 | + ) |
| 177 | + |
| 178 | + self._status = value |
| 179 | + |
| 180 | + def set_status(self, value: AvailabilityCheckerStatus) -> "ExtraRuleHandlerBase": |
| 181 | + """ |
| 182 | + Sets the status to work with. |
| 183 | +
|
| 184 | + :param value: |
| 185 | + The status to work with. |
| 186 | + """ |
| 187 | + |
| 188 | + self.status = value |
| 189 | + |
| 190 | + return self |
| 191 | + |
| 192 | + def start(self) -> "ExtraRuleHandlerBase": |
| 193 | + """ |
| 194 | + Starts the gathering process. |
| 195 | + """ |
| 196 | + |
| 197 | + raise NotImplementedError() |
| 198 | + |
| 199 | + def switch_to_down(self) -> "ExtraRuleHandlerBase": |
| 200 | + """ |
| 201 | + Switches the status to inactive. |
| 202 | + """ |
| 203 | + |
| 204 | + self.status.status_after_extra_rules = PyFunceble.storage.STATUS.down |
| 205 | + self.status.status_source_after_extra_rules = "SPECIAL" |
| 206 | + |
| 207 | + return self |
| 208 | + |
| 209 | + def switch_to_up(self) -> "ExtraRuleHandlerBase": |
| 210 | + """ |
| 211 | + Switches the status to active. |
| 212 | + """ |
| 213 | + |
| 214 | + self.status.status_after_extra_rules = PyFunceble.storage.STATUS.up |
| 215 | + self.status.status_source_after_extra_rules = "SPECIAL" |
0 commit comments