Source code for PyFunceble.preset

# pylint: disable=line-too-long
"""
The tool to check the availability or syntax of domains, IPv4 or URL.

::


    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provide a way to preset the configuration before launching a specific test type.

Author:
    Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks:
    https://pyfunceble.github.io/special-thanks.html

Contributors:
    https://pyfunceble.github.io/contributors.html

Project link:
    https://github.com/funilrys/PyFunceble

Project documentation:
    https://pyfunceble.readthedocs.io/en/master/

Project homepage:
    https://pyfunceble.github.io/

License:
::


    MIT License

    Copyright (c) 2017, 2018, 2019 Nissar Chababy

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
"""
# pylint: enable=line-too-long
import PyFunceble


[docs]class Preset: """ Check or update the global configuration based on some events. """ # List all index which can be superset. # In other words if an index which is listed here # is also listed into PyFunceble.INTERN["custom_config_loaded"], # We do not update it. do_not_overwrite_if_customized = ["no_files", "whois_database", "inactive_database"] def __init__(self): self.syntax_test()
[docs] @classmethod def switch( cls, variable, custom=False ): # pylint: disable=inconsistent-return-statements # pragma: no cover """ Switch PyFunceble.CONFIGURATION variables to their opposite. :param variable: The variable name to switch. The variable should be an index our configuration system. If we want to switch a bool variable, we should parse it here. :type variable: str|bool :param bool custom: Let us know if have to switch the parsed variable instead of our configuration index. :return: The opposite of the configuration index or the given variable. :rtype: bool :raises: :code:`Exception` When the configuration is not valid. In other words, if the PyFunceble.CONFIGURATION[variable_name] is not a bool. """ if not custom: # We are not working with custom variable which is not into # the configuration. # We get the current state. current_state = dict.get(PyFunceble.CONFIGURATION, variable) else: # We are working with a custom variable which is not into the # configuration current_state = variable if isinstance(current_state, bool): # The current state is a boolean. if current_state: # The current state is equal to True. # We return False. return False # The current state is equal to False. # We return True. return True # The current state is not a boolean. # We set the message to raise. to_print = "Impossible to switch %s. Please post an issue to %s" # We raise an exception inviting the user to report an issue. raise Exception( to_print % (repr(variable), PyFunceble.LINKS["repo"] + "/issues.") )
[docs] @classmethod def disable(cls, indexes): # pragma: no cover """ Set the given configuration index to :code:`False`. """ if ( indexes in cls.do_not_overwrite_if_customized and "custom_loaded" in PyFunceble.INTERN and PyFunceble.INTERN["custom_loaded"] and indexes in PyFunceble.INTERN["custom_config_loaded"] ): return None if isinstance(indexes, list): for index in indexes: cls.disable(index) else: if ( indexes not in PyFunceble.CONFIGURATION or PyFunceble.CONFIGURATION[indexes] ): PyFunceble.CONFIGURATION[indexes] = False return None
[docs] @classmethod def enable(cls, indexes): # pragma: no cover """ Set the given configuration index to :code:`True`. """ if ( indexes in cls.do_not_overwrite_if_customized and "custom_loaded" in PyFunceble.INTERN and PyFunceble.INTERN["custom_loaded"] and indexes in PyFunceble.INTERN["custom_config_loaded"] ): return None if isinstance(indexes, list): for index in indexes: cls.enable(index) else: if ( indexes not in PyFunceble.CONFIGURATION or not PyFunceble.CONFIGURATION[indexes] ): PyFunceble.CONFIGURATION[indexes] = True return None
[docs] @classmethod def reset_counters(cls): """ Reset the counters. """ for status in ["up", "down", "invalid", "tested"]: # We loop through to the index of the autoContinue subsystem. # And we set the counter of the currently read status to 0. PyFunceble.INTERN["counter"]["number"][status] = 0 PyFunceble.INTERN["counter"]["percentage"][status] = 0
[docs] @classmethod def syntax_test(cls): # pragma: no cover """ Disable the HTTP status code if we are testing for syntax """ if PyFunceble.CONFIGURATION["syntax"]: # We are checking for syntax. # We deactivate the http status code. PyFunceble.HTTP_CODE["active"] = False
[docs] @classmethod def maximal_processes(cls): # pragma: no cover """ Ensure that the number of maximal processes is alway >= 1. """ if PyFunceble.CONFIGURATION["maximal_processes"] < 1: PyFunceble.CONFIGURATION["maximal_processes"] = 1
[docs] def simple_domain(self): # pragma: no cover """ Prepare the global configuration for a domain test. """ should_be_disabled = ["show_percentage", "whois_database"] for index in should_be_disabled: self.disable(index)
[docs] def simple_url(self): # pragma: no cover """ Prepare the global configuration for an URL test. """ should_be_disabled = ["show_percentage", "whois_database"] self.disable(should_be_disabled)
[docs] def complements(self): # pragma: no cover """ Prepare the global configuration for a complements generation. """ should_be_enabled = ["auto_continue"] self.enable(should_be_enabled)
[docs] def file_url(self): # pragma: no cover """ Prepare the global configuration for a list of URL to test. """ should_be_disabled = ["generate_hosts"] should_be_enabled = ["no_whois", "plain_list_domain", "split"] self.disable(should_be_disabled) self.enable(should_be_enabled)
[docs] def api(self): # pragma: no cover """ Prepare the global configuration for a test from the API. """ should_be_disabled = [ "inactive_database", "auto_continue", "show_execution_time", ] should_be_enabled = ["simple", "quiet", "whois_database", "no_files"] self.disable(should_be_disabled) self.enable(should_be_enabled)
[docs] def multiprocess(self): # pragma: no cover """ Prepare the global configuration for a test with multiple processes. """ if PyFunceble.CONFIGURATION["multiprocess"]: should_be_enabled = ["auto_continue", "whois_database"] self.enable(should_be_enabled)