Пример #1
0
    def test_header_constructor_without_separator(self):  # pylint: disable=invalid-name
        """
        Test Prints()._header_constructor() for the case that we
        want to print the result of the test.
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        expected = ["hello world  here    is       PyFunceble"]
        actual = Prints(
            None, None, output_file=None, only_on_file=False
        )._header_constructor(self.to_print["basic"], None)

        self.assertEqual(expected, actual)

        # Test of the case that we want to print the hosts file format.
        expected = [" ".join(self.to_print["hosts"].keys())]

        actual = Prints(
            None, None, output_file=None, only_on_file=False
        )._header_constructor(self.to_print["hosts"], None)

        self.assertEqual(expected, actual)
Пример #2
0
    def print_header(cls):  # pragma: no cover
        """
        Print the header if needed.
        """

        if (not PyFunceble.CONFIGURATION["quiet"]
                and not PyFunceble.CONFIGURATION["header_printed"]):
            # * The quiet mode is not activated.
            # and
            # * The header has not been already printed.

            # We print a new line.
            print("\n")

            if PyFunceble.CONFIGURATION["less"]:
                # We have to show less informations on screen.

                # We print the `Less` header.
                Prints(None, "Less").header()
            else:
                # We have to show every informations on screen.

                # We print the `Generic` header.
                Prints(None, "Generic").header()

            # The header was printed.

            # We initiate the variable which say that the header has been printed to True.
            PyFunceble.CONFIGURATION["header_printed"] = True
Пример #3
0
    def unified_file(self):
        """
        Generate unified file. Understand by that that we use an unified table
        instead of a separate table for each status which could result into a
        misunderstanding.
        """

        if PyFunceble.CONFIGURATION["unified"]:
            output = self.output_parent_dir + PyFunceble.OUTPUTS["default_files"][
                "results"
            ]
            if PyFunceble.CONFIGURATION["less"]:
                if PyFunceble.HTTP_CODE["active"]:
                    to_print = [
                        self.tested,
                        self.domain_status,
                        PyFunceble.CONFIGURATION["http_code"],
                    ]
                else:
                    to_print = [self.tested, self.domain_status, self.source]

                Prints(to_print, "Less", output, True).data()
            else:
                to_print = [
                    self.tested,
                    self.domain_status,
                    self.expiration_date,
                    self.source,
                    PyFunceble.CONFIGURATION["http_code"],
                    PyFunceble.CURRENT_TIME,
                ]

                Prints(to_print, "Generic_File", output, True).data()
Пример #4
0
    def test_colorify(self):
        """
        Test Prints().colorify(). In other word, it test the coloration
        of the line we have to print depending of the status.
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        # Test with a template that is not designed for colorify
        expected = self.to_print["basic_string"]
        actual = Prints(None, "Hehehe", output_file=None, only_on_file=False)._colorify(
            self.to_print["basic_string"]
        )

        self.assertEqual(expected, actual)

        # Test with a template that is designed for colorify + Status is UP
        expected = (
            PyFunceble.Fore.BLACK
            + PyFunceble.Back.GREEN
            + self.to_print["basic_string"]
        )
        actual = Prints(
            ["This is a test", PyFunceble.STATUS["official"]["up"]],
            "Generic",
            output_file=None,
            only_on_file=False,
        )._colorify(self.to_print["basic_string"])

        self.assertEqual(expected, actual)

        # Test with a template that is designed for colorify + Status is DOWN
        expected = (
            PyFunceble.Fore.BLACK + PyFunceble.Back.RED + self.to_print["basic_string"]
        )
        actual = Prints(
            ["This is a test", PyFunceble.STATUS["official"]["down"]],
            "Generic",
            output_file=None,
            only_on_file=False,
        )._colorify(self.to_print["basic_string"])

        self.assertEqual(expected, actual)

        # Test with a template that is designed for colorify + Status is
        # UNKNOWN or INVALID
        expected = (
            PyFunceble.Fore.BLACK + PyFunceble.Back.CYAN + self.to_print["basic_string"]
        )
        actual = Prints(
            ["This is a test", PyFunceble.STATUS["official"]["invalid"]],
            "Generic",
            output_file=None,
            only_on_file=False,
        )._colorify(self.to_print["basic_string"])

        self.assertEqual(expected, actual)
Пример #5
0
    def status_file(self):
        """
        Generate a file according to the domain status.
        """

        try:
            PyFunceble.CONFIGURATION["http_code"]
        except KeyError:
            PyFunceble.CONFIGURATION["http_code"] = "*" * 3

        if not PyFunceble.CONFIGURATION["http_code"]:
            PyFunceble.CONFIGURATION["http_code"] = "*" * 3

        if self.domain_status.lower() in PyFunceble.STATUS["list"]["up"]:
            self.up_status_file()
        elif self.domain_status.lower() in PyFunceble.STATUS["list"]["down"]:
            self.down_status_file()
        elif self.domain_status.lower() in PyFunceble.STATUS["list"]["invalid"]:
            self.invalid_status_file()

        Generate(self.domain_status, self.source, self.expiration_date).hosts_file()
        Percentage(self.domain_status).count()

        if not PyFunceble.CONFIGURATION["quiet"]:
            if PyFunceble.CONFIGURATION["less"]:
                Prints(
                    [
                        self.tested,
                        self.domain_status,
                        PyFunceble.CONFIGURATION["http_code"],
                    ],
                    "Less",
                ).data()
            else:
                if PyFunceble.HTTP_CODE["active"]:
                    data_to_print = [
                        self.tested,
                        self.domain_status,
                        self.expiration_date,
                        self.source,
                        PyFunceble.CONFIGURATION["http_code"],
                    ]
                else:
                    data_to_print = [
                        self.tested,
                        self.domain_status,
                        self.expiration_date,
                        self.source,
                        PyFunceble.CURRENT_TIME,
                    ]

                Prints(data_to_print, "Generic").data()

        if not PyFunceble.CONFIGURATION["no_files"] and PyFunceble.CONFIGURATION[
            "split"
        ]:
            self._prints_status_file()
        else:
            self.unified_file()
Пример #6
0
    def _prints_status_screen(self):
        """
        Logic behind the printing (on screen) when generating status file.
        """

        if not PyFunceble.CONFIGURATION["quiet"]:
            # The quiet mode is not activated.

            if PyFunceble.CONFIGURATION["less"]:
                # We have to print less information.

                # We initiate the data to print.
                to_print = [
                    self.tested,
                    self.domain_status,
                    PyFunceble.INTERN["http_code"],
                ]

                if not PyFunceble.HTTP_CODE["active"]:
                    # The http status code is not activated.

                    # We replace the last element to print with
                    # the source.
                    to_print[-1] = self.source

                # We print the informations on screen.
                Prints(to_print, "Less").data()
            else:
                # We have to print all informations on screen.

                if PyFunceble.HTTP_CODE["active"]:
                    # The http status code extraction is activated.

                    # We initiate the data to print.
                    data_to_print = [
                        self.tested,
                        self.domain_status,
                        self.expiration_date,
                        self.source,
                        PyFunceble.INTERN["http_code"],
                    ]
                else:
                    # The http status code extraction is not activated.

                    # We initiate the data to print.
                    data_to_print = [
                        self.tested,
                        self.domain_status,
                        self.expiration_date,
                        self.source,
                        PyFunceble.CURRENT_TIME,
                    ]

                # We print the information on screen.
                Prints(data_to_print, "Generic").data()
Пример #7
0
    def test_data_constructor(self):
        """
        This method test Prints()._data_constructor().
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        expected = PyFunceble.OrderedDict()
        to_print = []

        chars = ["H", "E", "L", "L", "O", "!"]

        for i, size in enumerate(self.to_print["size_constructor"]):
            index = chars[i] * size
            expected[index] = size
            to_print.append(index)

        actual = Prints(
            to_print, None, output_file=None, only_on_file=False
        )._data_constructor(
            self.to_print["size_constructor"]
        )

        self.assertEqual(expected, actual)

        # Test the case that there is an issue.
        expected = PyFunceble.OrderedDict()
        to_print = []

        chars = ["H", "E", "L", "L", "O", "!"]

        for i, size in enumerate(self.to_print["size_constructor"]):
            index = chars[i] * size
            expected[index] = size
            to_print.append(index)

        del to_print[-1]

        self.assertRaisesRegex(
            Exception,
            "Inputed: %d; Size: %d"
            % (
                len(self.to_print["size_constructor"]) - 1,
                len(self.to_print["size_constructor"]),
            ),
            lambda: Prints(
                to_print, None, output_file=None, only_on_file=False
            )._data_constructor(
                self.to_print["size_constructor"]
            ),
        )
Пример #8
0
    def unified_file(self):
        """
        Generate unified file. Understand by that that we use an unified table
        instead of a separate table for each status which could result into a
        misunderstanding.
        """

        if ("file_to_test" in PyFunceble.INTERN
                and PyFunceble.INTERN["file_to_test"]
                and PyFunceble.CONFIGURATION["unified"]):
            # * We are testing a file.
            # and
            # * The unified file generation is activated.

            # We construct the path of the unified file.
            output = (self.output_parent_dir +
                      PyFunceble.OUTPUTS["default_files"]["results"])

            if PyFunceble.CONFIGURATION["less"]:
                # We have to print less information.

                if PyFunceble.HTTP_CODE["active"]:
                    # The http status code request is activated.

                    # We construct what we have to print.
                    to_print = [
                        self.tested,
                        self.domain_status,
                        PyFunceble.INTERN["http_code"],
                    ]
                else:
                    # The http status code request is not activated.

                    # We construct what we have to print.
                    to_print = [self.tested, self.domain_status, self.source]

                # And we print the informations on file.
                Prints(to_print, "Less", output, True).data()
            else:
                # The unified file generation is not activated.

                # We construct what we have to print.
                to_print = [
                    self.tested,
                    self.domain_status,
                    self.expiration_date,
                    self.source,
                    PyFunceble.INTERN["http_code"],
                    PyFunceble.CURRENT_TIME,
                ]

                # And we print the information on file.
                Prints(to_print, "Generic_File", output, True).data()
Пример #9
0
    def test_before_header(self, header_constructor_patch):
        """
        Test the functionability of Prints().before_header()
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        self.assertEqual(expected, actual)

        expected = """# File generated by {0} (v{1}) / {2}
# Date of generation: {3}

""".format(
            PyFunceble.NAME,
            PyFunceble.VERSION.split()[0],
            PyFunceble.LINKS["repo"],
            PyFunceble.CURRENT_TIME + " ",
        )

        Prints(None, None, output_file=self.file, only_on_file=False)._before_header()

        self.assertEqual(expected, File(self.file).read())

        # Test of the case that we have a Generic_File template

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        self.assertEqual(expected, actual)

        expected = """# File generated by {0} (v{1}) / {2}
# Date of generation: {3}

Hello World!
""".format(
            PyFunceble.NAME,
            PyFunceble.VERSION.split()[0],
            PyFunceble.LINKS["repo"],
            PyFunceble.CURRENT_TIME + " ",
        )

        header_constructor_patch.return_value = ["Hello World!"]
        Prints(
            None, "Generic_File", output_file=self.file, only_on_file=False
        )._before_header()

        self.assertEqual(expected, File(self.file).read())
Пример #10
0
    def print_header(cls):
        """
        Decide if we print or not the header.
        """

        if not PyFunceble.CONFIGURATION[
                "quiet"] and not PyFunceble.CONFIGURATION["header_printed"]:
            print("\n")
            if PyFunceble.CONFIGURATION["less"]:
                Prints(None, "Less").header()
            else:
                Prints(None, "Generic").header()

            PyFunceble.CONFIGURATION["header_printed"] = True
Пример #11
0
    def test_before_header(self, header_constructor_patch):
        """
        This method test the functionability of Prints().before_header()
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        self.assertEqual(expected, actual)

        expected = """# File generated with %s
# Date of generation: %s

""" % (
            PyFunceble.LINKS["repo"], PyFunceble.CURRENT_TIME + " "
        )

        Prints(None, None, output_file=self.file, only_on_file=False).before_header()

        self.assertEqual(expected, File(self.file).read())

        # Test of the case that we have a Generic_File template

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        self.assertEqual(expected, actual)

        expected = """# File generated with %s
# Date of generation: %s

Hello World!
""" % (
            PyFunceble.LINKS["repo"], PyFunceble.CURRENT_TIME + " "
        )

        header_constructor_patch.return_value = ["Hello World!"]
        Prints(
            None, "Generic_File", output_file=self.file, only_on_file=False
        ).before_header()

        self.assertEqual(expected, File(self.file).read())
Пример #12
0
    def log(self):
        """
        Print on screen and on file the percentages for each status.
        """

        if PyFunceble.CONFIGURATION[
                "show_percentage"] and PyFunceble.CONFIGURATION["counter"][
                    "number"]["tested"] > 0:
            output = PyFunceble.CURRENT_DIRECTORY + PyFunceble.OUTPUTS[
                "parent_directory"] + PyFunceble.OUTPUTS["logs"][
                    "directories"]["parent"] + PyFunceble.OUTPUTS["logs"][
                        "directories"]["percentage"] + PyFunceble.OUTPUTS[
                            "logs"]["filenames"]["percentage"]
            File(output).delete()

            self._calculate()

            if not PyFunceble.CONFIGURATION["quiet"]:
                print("\n")
                Prints(None, "Percentage", output).header()

                for to_print in [
                    [
                        PyFunceble.STATUS["official"]["up"],
                        str(PyFunceble.CONFIGURATION["counter"]["percentage"]
                            ["up"]) + "%",
                        PyFunceble.CONFIGURATION["counter"]["number"]["up"],
                    ],
                    [
                        PyFunceble.STATUS["official"]["down"],
                        str(PyFunceble.CONFIGURATION["counter"]["percentage"]
                            ["down"]) + "%",
                        PyFunceble.CONFIGURATION["counter"]["number"]["down"],
                    ],
                    [
                        PyFunceble.STATUS["official"]["invalid"],
                        str(PyFunceble.CONFIGURATION["counter"]["percentage"]
                            ["invalid"]) + "%",
                        PyFunceble.CONFIGURATION["counter"]["number"]
                        ["invalid"],
                    ],
                ]:
                    Prints(to_print, "Percentage", output).data()
        elif PyFunceble.CONFIGURATION["counter"]["number"]["tested"] > 0:
            self._calculate()
Пример #13
0
    def _analytic_file(self, new_status, old_status):
        """
        Generate HTTP_Analytic/* files.

        Arguments:
            - new_status: str
                The new status of the domain.
            - old_status: str
                The old status of the domain.
        """

        output = self.output_parent_dir + PyFunceble.OUTPUTS["http_analytic"][
            "directories"
        ][
            "parent"
        ] + "%s%s"
        if new_status.lower() in PyFunceble.STATUS["list"]["up"]:
            output = output % (
                PyFunceble.OUTPUTS["http_analytic"]["directories"]["up"],
                PyFunceble.OUTPUTS["http_analytic"]["filenames"]["up"],
            )
            Generate("HTTP_Active").hosts_file()
        elif new_status.lower() in PyFunceble.STATUS["list"]["potentially_up"]:
            output = output % (
                PyFunceble.OUTPUTS["http_analytic"]["directories"]["potentially_up"],
                PyFunceble.OUTPUTS["http_analytic"]["filenames"]["potentially_up"],
            )
            Generate("potentially_up").hosts_file()
        else:
            output = output % (
                PyFunceble.OUTPUTS["http_analytic"]["directories"]["potentially_down"],
                PyFunceble.OUTPUTS["http_analytic"]["filenames"]["potentially_down"],
            )
            Generate("potentially_down").hosts_file()

        Prints(
            [
                self.tested,
                old_status,
                PyFunceble.CONFIGURATION["http_code"],
                PyFunceble.CURRENT_TIME,
            ],
            "HTTP",
            output,
            True,
        ).data()
Пример #14
0
    def test_size_from_header(self):
        """
        Test Prints()._size_from_header() which is used to extract
        the static sizes.
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        expected = [element for _, element in self.to_print["basic"].items()]

        actual = Prints(
            None, None, output_file=None, only_on_file=False
        )._size_from_header(self.to_print["basic"])

        self.assertEqual(expected, actual)
Пример #15
0
    def test_header_constructor_with_separator(self):  # pylint: disable=invalid-name
        """
        Test Prints()._header_constructor() for the case that we
        want to print the header.
        """

        File(self.file).delete()

        expected = False
        actual = PyFunceble.path.isfile(self.file)

        expected = [
            "hello world  here    is       PyFunceble",
            "----- ------ ------- -------- ----------",
        ]
        actual = Prints(
            None, None, output_file=None, only_on_file=False
        )._header_constructor(self.to_print["basic"])

        self.assertEqual(expected, actual)
Пример #16
0
    def analytic_file(self, new_status, old_status):
        """
        Generate :code:`Analytic/*` files based on the given old and
        new statuses.

        :param new_status: The new status of the domain.
        :type new_status: str

        :param old_status: The old status of the domain.
        :type old_status: str
        """

        if "file_to_test" in PyFunceble.INTERN and PyFunceble.INTERN[
                "file_to_test"]:
            # We are testing a file.

            # We partially construct the path to the file to write/print.
            output = (self.output_parent_dir +
                      PyFunceble.OUTPUTS["analytic"]["directories"]["parent"] +
                      "%s%s")

            if new_status.lower() in PyFunceble.STATUS["list"]["up"]:
                # The new status is in the list of up status.

                # We complete the output directory.
                output = output % (
                    PyFunceble.OUTPUTS["analytic"]["directories"]["up"],
                    PyFunceble.OUTPUTS["analytic"]["filenames"]["up"],
                )

                # We generate the hosts file.
                Generate("HTTP_Active").info_files()
            elif new_status.lower(
            ) in PyFunceble.STATUS["list"]["potentially_up"]:
                # The new status is in the list of down status.

                # We complete the output directory.
                output = output % (
                    PyFunceble.OUTPUTS["analytic"]["directories"]
                    ["potentially_up"],
                    PyFunceble.OUTPUTS["analytic"]["filenames"]
                    ["potentially_up"],
                )

                # We generate the hosts file.
                Generate("potentially_up").info_files()
            elif new_status.lower() in PyFunceble.STATUS["list"]["suspicious"]:
                # The new status is in the list of suspicious status.

                # We complete the output directory.
                output = output % (
                    PyFunceble.OUTPUTS["analytic"]["directories"]
                    ["suspicious"],
                    PyFunceble.OUTPUTS["analytic"]["filenames"]["suspicious"],
                )

                # We generate the hosts file.
                Generate("suspicious").info_files()
            else:
                # The new status is in the list of up and down status.

                # We complete the output directory.
                output = output % (
                    PyFunceble.OUTPUTS["analytic"]["directories"]
                    ["potentially_down"],
                    PyFunceble.OUTPUTS["analytic"]["filenames"]
                    ["potentially_down"],
                )

                # We generate the hosts files.
                Generate("potentially_down").info_files()

            # We print the information on file.
            Prints(
                [
                    self.tested,
                    old_status,
                    PyFunceble.INTERN["http_code"],
                    PyFunceble.CURRENT_TIME,
                ],
                "HTTP",
                output,
                True,
            ).data()
Пример #17
0
    def _prints_status_file(self):
        """
        Logic behind the printing when generating status file.
        """

        if PyFunceble.CONFIGURATION["less"]:
            Prints(
                [self.tested, self.domain_status, self.source],
                "Less",
                self.output,
                True,
            ).data()
        elif PyFunceble.CONFIGURATION["split"]:
            if self.domain_status.lower() in PyFunceble.STATUS["list"]["up"]:
                if PyFunceble.HTTP_CODE["active"]:
                    data_to_print = [
                        self.tested,
                        self.expiration_date,
                        self.source,
                        PyFunceble.CONFIGURATION["http_code"],
                        PyFunceble.CURRENT_TIME,
                    ]
                else:
                    data_to_print = [
                        self.tested,
                        self.expiration_date,
                        self.source,
                        PyFunceble.CURRENT_TIME,
                    ]

                Prints(
                    data_to_print,
                    PyFunceble.STATUS["official"]["up"],
                    self.output,
                    True,
                ).data()
            elif self.domain_status.lower() in PyFunceble.STATUS["list"]["down"]:
                if PyFunceble.HTTP_CODE["active"]:
                    data_to_print = [
                        self.tested,
                        PyFunceble.CONFIGURATION["referer"],
                        self.domain_status,
                        self.source,
                        PyFunceble.CONFIGURATION["http_code"],
                        PyFunceble.CURRENT_TIME,
                    ]
                else:
                    data_to_print = [
                        self.tested,
                        PyFunceble.CONFIGURATION["referer"],
                        self.domain_status,
                        self.source,
                        PyFunceble.CURRENT_TIME,
                    ]

                Prints(
                    data_to_print,
                    PyFunceble.STATUS["official"]["down"],
                    self.output,
                    True,
                ).data()
            elif self.domain_status.lower() in PyFunceble.STATUS["list"]["invalid"]:
                if PyFunceble.HTTP_CODE["active"]:
                    data_to_print = [
                        self.tested,
                        self.source,
                        PyFunceble.CONFIGURATION["http_code"],
                        PyFunceble.CURRENT_TIME,
                    ]
                else:
                    data_to_print = [self.tested, self.source, PyFunceble.CURRENT_TIME]

                Prints(
                    data_to_print,
                    PyFunceble.STATUS["official"]["invalid"],
                    self.output,
                    True,
                ).data()
Пример #18
0
    def hosts_file(self):
        """
        Generate a hosts file.
        """

        if PyFunceble.CONFIGURATION["generate_hosts"] or PyFunceble.CONFIGURATION[
            "plain_list_domain"
        ]:
            splited_destination = ""
            http_list = []
            http_list.extend(PyFunceble.STATUS["list"]["potentially_up"])
            http_list.extend(PyFunceble.STATUS["list"]["potentially_down"])
            http_list.extend(PyFunceble.STATUS["list"]["http_active"])

            output_hosts = self.output_parent_dir + PyFunceble.OUTPUTS["hosts"][
                "directory"
            ] + "%s" + directory_separator + PyFunceble.OUTPUTS[
                "hosts"
            ][
                "filename"
            ]

            output_domains = self.output_parent_dir + PyFunceble.OUTPUTS["domains"][
                "directory"
            ] + "%s" + directory_separator + PyFunceble.OUTPUTS[
                "domains"
            ][
                "filename"
            ]

            if self.domain_status.lower() in PyFunceble.STATUS["list"]["up"]:
                hosts_destination = output_hosts % PyFunceble.STATUS["official"]["up"]
                plain_destination = output_domains % PyFunceble.STATUS["official"]["up"]
            elif self.domain_status.lower() in PyFunceble.STATUS["list"]["down"]:
                hosts_destination = output_hosts % PyFunceble.STATUS["official"]["down"]
                plain_destination = output_domains % PyFunceble.STATUS["official"][
                    "down"
                ]
            elif self.domain_status.lower() in PyFunceble.STATUS["list"]["invalid"]:
                hosts_destination = output_hosts % PyFunceble.STATUS["official"][
                    "invalid"
                ]
                plain_destination = output_domains % PyFunceble.STATUS["official"][
                    "invalid"
                ]
            elif self.domain_status.lower() in http_list:

                output_dir = self.output_parent_dir + PyFunceble.OUTPUTS[
                    "http_analytic"
                ][
                    "directories"
                ][
                    "parent"
                ]
                if self.domain_status.lower() in PyFunceble.STATUS["list"][
                    "potentially_up"
                ]:
                    output_dir += PyFunceble.OUTPUTS["http_analytic"]["directories"][
                        "potentially_up"
                    ]
                elif self.domain_status.lower() in PyFunceble.STATUS["list"][
                    "potentially_down"
                ]:
                    output_dir += PyFunceble.OUTPUTS["http_analytic"]["directories"][
                        "potentially_down"
                    ]
                else:
                    output_dir += PyFunceble.OUTPUTS["http_analytic"]["directories"][
                        "up"
                    ]

                if not output_dir.endswith(directory_separator):
                    output_dir += directory_separator

                hosts_destination = output_dir + PyFunceble.OUTPUTS["hosts"]["filename"]
                plain_destination = output_dir + PyFunceble.OUTPUTS["domains"][
                    "filename"
                ]
                splited_destination = output_dir + str(
                    PyFunceble.CONFIGURATION["http_code"]
                )

            if PyFunceble.CONFIGURATION["generate_hosts"]:
                Prints(
                    [PyFunceble.CONFIGURATION["custom_ip"], self.tested],
                    "FullHosts",
                    hosts_destination,
                ).data()

            if PyFunceble.CONFIGURATION["plain_list_domain"]:
                Prints([self.tested], "PlainDomain", plain_destination).data()

            if PyFunceble.CONFIGURATION["split"] and splited_destination:
                Prints([self.tested], "PlainDomain", splited_destination).data()
Пример #19
0
    def log(self):
        """
        Print on screen and on file the percentages for each status.
        """

        if (PyFunceble.CONFIGURATION["show_percentage"]
                and PyFunceble.INTERN["counter"]["number"]["tested"] > 0):
            # * We are allowed to show the percentage on screen.
            # and
            # * The number of tested is greater than 0.

            # We initiate the output file.
            output = (PyFunceble.OUTPUT_DIRECTORY +
                      PyFunceble.OUTPUTS["parent_directory"] +
                      PyFunceble.OUTPUTS["logs"]["directories"]["parent"] +
                      PyFunceble.OUTPUTS["logs"]["directories"]["percentage"] +
                      PyFunceble.OUTPUTS["logs"]["filenames"]["percentage"])

            # We delete the output file if it does exist.
            File(output).delete()

            # We calculate the percentage of each statuses.
            self._calculate()

            if not PyFunceble.CONFIGURATION["quiet"]:
                # The quiet mode is activated.

                # We print a new line.
                print("\n")

                # We print the percentage header on file and screen.
                Prints(None, "Percentage", output).header()

                # We construct the different lines/data to print on screen and file.
                lines_to_print = [
                    [
                        PyFunceble.STATUS["official"]["up"],
                        str(PyFunceble.INTERN["counter"]["percentage"]["up"]) +
                        "%",
                        PyFunceble.INTERN["counter"]["number"]["up"],
                    ],
                    [
                        PyFunceble.STATUS["official"]["down"],
                        str(PyFunceble.INTERN["counter"]["percentage"]["down"])
                        + "%",
                        PyFunceble.INTERN["counter"]["number"]["down"],
                    ],
                    [
                        PyFunceble.STATUS["official"]["invalid"],
                        str(PyFunceble.INTERN["counter"]["percentage"]
                            ["invalid"]) + "%",
                        PyFunceble.INTERN["counter"]["number"]["invalid"],
                    ],
                ]

                if PyFunceble.CONFIGURATION["syntax"]:
                    # We are checking for syntax.

                    # We update the denomination of the UP.
                    lines_to_print[0][0] = PyFunceble.STATUS["official"][
                        "valid"]

                    # And we unset the INACTIVE line.
                    del lines_to_print[1]

                for to_print in lines_to_print:
                    # We loop throught the different line to print.
                    # (one line for each status.)

                    # And we print the current status line on file and screen.
                    Prints(to_print, "Percentage", output).data()

        elif PyFunceble.INTERN["counter"]["number"]["tested"] > 0:
            # * We are not allowed to show the percentage on screen.
            # but
            # * The number of tested is greater than 0.

            # We run the calculation.
            # Note: The following is needed, because all counter calculation are
            # done by this class.
            self._calculate()
Пример #20
0
    def _prints_status_file(self):  # pylint: disable=too-many-branches
        """
        Logic behind the printing (in file) when generating status file.
        """

        if self.subject_type.startswith("file_"):
            # We are testing a file.

            output = (self.output_parent_dir +
                      PyFunceble.OUTPUTS["splited"]["directory"] + self.status)

            if PyFunceble.CONFIGURATION["less"]:
                # We have to print less information.

                # We print the information on file.
                Prints([self.subject, self.status, self.source], "Less",
                       output, True).data()
            elif PyFunceble.CONFIGURATION["split"]:
                # We have to split the information we print on file.

                if self.status.lower() in PyFunceble.STATUS["list"]["up"]:
                    # The status is in the list of up status.

                    if PyFunceble.HTTP_CODE["active"]:
                        # The http code extraction is activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.subject,
                            self.expiration_date,
                            self.source,
                            self.status_code,
                            PyFunceble.CURRENT_TIME,
                        ]
                    else:
                        # The http code extraction is not activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.subject,
                            self.expiration_date,
                            self.source,
                            PyFunceble.CURRENT_TIME,
                        ]

                    # We print the informations to print on file.
                    Prints(data_to_print, PyFunceble.STATUS["official"]["up"],
                           output, True).data()
                elif self.status.lower() in PyFunceble.STATUS["list"]["valid"]:
                    # The status is in the list of valid status.

                    # We initiate the data to print.
                    data_to_print = [
                        self.subject, self.source, PyFunceble.CURRENT_TIME
                    ]

                    # We print the informations to print on file.
                    Prints(
                        data_to_print,
                        PyFunceble.STATUS["official"]["valid"],
                        output,
                        True,
                    ).data()
                elif self.status.lower() in PyFunceble.STATUS["list"]["down"]:
                    # The status is in the list of down status.

                    if PyFunceble.HTTP_CODE["active"]:
                        # The http statuc code extraction is activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.subject,
                            self.whois_server,
                            self.status,
                            self.source,
                            self.status_code,
                            PyFunceble.CURRENT_TIME,
                        ]
                    else:
                        # The http status code extraction is not activated.

                        # We initate the data to print.
                        data_to_print = [
                            self.subject,
                            self.whois_server,
                            self.status,
                            self.source,
                            PyFunceble.CURRENT_TIME,
                        ]

                    # We print the information on file.
                    Prints(
                        data_to_print,
                        PyFunceble.STATUS["official"]["down"],
                        output,
                        True,
                    ).data()
                elif self.status.lower(
                ) in PyFunceble.STATUS["list"]["invalid"]:
                    # The status is in the list of invalid status.

                    if PyFunceble.HTTP_CODE["active"]:
                        # The http status code extraction is activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.subject,
                            self.source,
                            self.status_code,
                            PyFunceble.CURRENT_TIME,
                        ]
                    else:
                        # The http status code extraction is not activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.subject,
                            self.source,
                            PyFunceble.CURRENT_TIME,
                        ]

                    # We print the information to print on file.
                    Prints(
                        data_to_print,
                        PyFunceble.STATUS["official"]["invalid"],
                        output,
                        True,
                    ).data()
Пример #21
0
    def analytic_file(self, new_status, old_status=None):
        """
        Generate :code:`Analytic/*` files based on the given old and
        new statuses.

        :param str new_status: The new status of the domain.

        :param str old_status: The old status of the domain.
        """

        if not old_status:
            # The old status is not given.

            # We set the old status as the one given globally.
            old_status = self.status

        if self.subject_type.startswith("file_"):
            # We are testing files.

            # We map the way we are going to work with the status.
            status_map = {
                "up": "HTTP_Active",
                "potentially_up": "potentially_up",
                "suspicious": "suspicious",
            }

            # We keep a track of the map usage.
            map_used = False

            # We partially construct the path to the file to write/print.
            output = (self.output_parent_dir +
                      PyFunceble.OUTPUTS["analytic"]["directories"]["parent"] +
                      "%s%s")

            for status, generate_status in status_map.items():
                # We loop through our map.

                if new_status.lower() in PyFunceble.STATUS["list"][status]:
                    # The status is found into our map.

                    # We conmplete the output directory.
                    output = output % (
                        PyFunceble.OUTPUTS["analytic"]["directories"][status],
                        PyFunceble.OUTPUTS["analytic"]["filenames"][status],
                    )

                    # We generate the different file(s).
                    Generate(
                        self.subject,
                        self.subject_type,
                        generate_status,
                        source=self.source,
                        expiration_date=self.expiration_date,
                        http_status_code=self.status_code,
                        whois_server=self.whois_server,
                        filename=self.filename,
                        ip_validation=self.ip_validation,
                    ).info_files()

                    # We update the map usage.
                    map_used = True

                    # And we break the loop.
                    break

            if not map_used:
                # The map was not user, that means that
                # we are working with potentially inactive domains.

                # We complete the output directory.
                output = output % (
                    PyFunceble.OUTPUTS["analytic"]["directories"]
                    ["potentially_down"],
                    PyFunceble.OUTPUTS["analytic"]["filenames"]
                    ["potentially_down"],
                )

                # We generate the hosts files.
                Generate(
                    self.subject,
                    self.subject_type,
                    "potentially_down",
                    source=self.source,
                    expiration_date=self.expiration_date,
                    http_status_code=self.status_code,
                    whois_server=self.whois_server,
                    filename=self.filename,
                    ip_validation=self.ip_validation,
                ).info_files()

            # We print the information on file.
            Prints(
                [
                    self.subject, old_status, self.status_code,
                    PyFunceble.CURRENT_TIME
                ],
                "HTTP",
                output,
                True,
            ).data()
Пример #22
0
    def info_files(  # pylint: disable=inconsistent-return-statements,too-many-branches
        self):
        """
        Generate the hosts file, the plain list, the JSON file and the splitted files.
        """

        if self.___info_files_authorization():
            # We initiate a variable which whill save the splited testination.
            splited_destination = ""

            # We partially initiate the path to the hosts file.
            output_hosts = (self.output_parent_dir +
                            PyFunceble.OUTPUTS["hosts"]["directory"] + "%s" +
                            directory_separator +
                            PyFunceble.OUTPUTS["hosts"]["filename"])

            # We partially initiate the path to the plain list file.
            output_domains = (self.output_parent_dir +
                              PyFunceble.OUTPUTS["domains"]["directory"] +
                              "%s" + directory_separator +
                              PyFunceble.OUTPUTS["domains"]["filename"])

            # We partially intiate the path to the json list file.
            output_json = (self.output_parent_dir +
                           PyFunceble.OUTPUTS["json"]["directory"] + "%s" +
                           directory_separator +
                           PyFunceble.OUTPUTS["json"]["filename"])

            if self.ip_validation:
                # The element is an IP.

                # We construct the output file.
                output_hosts = (self.output_parent_dir +
                                PyFunceble.OUTPUTS["hosts"]["directory"] +
                                "%s" + directory_separator +
                                PyFunceble.OUTPUTS["hosts"]["ip_filename"])

            # We get the destination of the different files.
            hosts_destination, plain_destination, json_destination, splited_destination = self.___get_info_files_destinations(  # pylint: disable=line-too-long
                output_hosts, output_domains, output_json)

            if PyFunceble.CONFIGURATION["generate_hosts"]:
                # The hosts file generation is activated.

                # We generate/append the currently tested element in its
                # final location. (hosts file format)
                # We print on screen and on file.
                Prints(
                    [PyFunceble.CONFIGURATION["custom_ip"], self.subject],
                    "FullHosts",
                    hosts_destination,
                ).data()

            if PyFunceble.CONFIGURATION["plain_list_domain"]:
                # The plain list generation is activated.

                # We generate/append the currently tested element in its
                # final location. (the plain list format)
                # We print on file.
                Prints([self.subject], "PlainDomain", plain_destination).data()

            if PyFunceble.CONFIGURATION["split"] and splited_destination:
                # The splited list generation is activated.

                # We generate/append the currently tested element in its
                # final location. (the split list format)
                # We print on file.
                Prints([self.subject], "PlainDomain",
                       splited_destination).data()

            if (PyFunceble.CONFIGURATION["generate_json"]
                    and not PyFunceble.CONFIGURATION["multiprocess"]):
                # The json list generation is activated.

                # We generate/append the currently tested element in its
                # final location. (the json format)
                # We print on file.
                Prints([self.subject], "JSON", json_destination).data()
Пример #23
0
    def _prints_status_file(self):  # pylint: disable=too-many-branches
        """
        Logic behind the printing (in file) when generating status file.
        """

        if PyFunceble.INTERN["file_to_test"]:
            # We are testing a file.

            if PyFunceble.CONFIGURATION["less"]:
                # We have to print less information.

                # We print the information on file.
                Prints(
                    [self.tested, self.domain_status, self.source],
                    "Less",
                    self.output,
                    True,
                ).data()
            elif PyFunceble.CONFIGURATION["split"]:
                # We have to split the information we print on file.

                if self.domain_status.lower(
                ) in PyFunceble.STATUS["list"]["up"]:
                    # The status is in the list of up status.

                    if PyFunceble.HTTP_CODE["active"]:
                        # The http code extraction is activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.tested,
                            self.expiration_date,
                            self.source,
                            PyFunceble.INTERN["http_code"],
                            PyFunceble.CURRENT_TIME,
                        ]
                    else:
                        # The http code extraction is not activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.tested,
                            self.expiration_date,
                            self.source,
                            PyFunceble.CURRENT_TIME,
                        ]

                    # We print the informations to print on file.
                    Prints(
                        data_to_print,
                        PyFunceble.STATUS["official"]["up"],
                        self.output,
                        True,
                    ).data()
                elif self.domain_status.lower(
                ) in PyFunceble.STATUS["list"]["valid"]:
                    # The status is in the list of valid status.

                    # We initiate the data to print.
                    data_to_print = [
                        self.tested, self.source, PyFunceble.CURRENT_TIME
                    ]

                    # We print the informations to print on file.
                    Prints(
                        data_to_print,
                        PyFunceble.STATUS["official"]["valid"],
                        self.output,
                        True,
                    ).data()
                elif self.domain_status.lower(
                ) in PyFunceble.STATUS["list"]["down"]:
                    # The status is in the list of down status.

                    if PyFunceble.HTTP_CODE["active"]:
                        # The http statuc code extraction is activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.tested,
                            PyFunceble.INTERN["referer"],
                            self.domain_status,
                            self.source,
                            PyFunceble.INTERN["http_code"],
                            PyFunceble.CURRENT_TIME,
                        ]
                    else:
                        # The http status code extraction is not activated.

                        # We initate the data to print.
                        data_to_print = [
                            self.tested,
                            PyFunceble.INTERN["referer"],
                            self.domain_status,
                            self.source,
                            PyFunceble.CURRENT_TIME,
                        ]

                    # We print the information on file.
                    Prints(
                        data_to_print,
                        PyFunceble.STATUS["official"]["down"],
                        self.output,
                        True,
                    ).data()
                elif self.domain_status.lower(
                ) in PyFunceble.STATUS["list"]["invalid"]:
                    # The status is in the list of invalid status.

                    if PyFunceble.HTTP_CODE["active"]:
                        # The http status code extraction is activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.tested,
                            self.source,
                            PyFunceble.INTERN["http_code"],
                            PyFunceble.CURRENT_TIME,
                        ]
                    else:
                        # The http status code extraction is not activated.

                        # We initiate the data to print.
                        data_to_print = [
                            self.tested,
                            self.source,
                            PyFunceble.CURRENT_TIME,
                        ]

                    # We print the information to print on file.
                    Prints(
                        data_to_print,
                        PyFunceble.STATUS["official"]["invalid"],
                        self.output,
                        True,
                    ).data()
Пример #24
0
    def info_files(self):  # pylint: disable=inconsistent-return-statements
        """
        Generate the hosts file, the plain list and the splitted lists.
        """

        if self._do_not_produce_file():
            # We do not have to produce file.

            # We return false.
            return False

        if ("file_to_test" in PyFunceble.INTERN
                and PyFunceble.INTERN["file_to_test"]
                and (PyFunceble.CONFIGURATION["generate_hosts"]
                     or PyFunceble.CONFIGURATION["plain_list_domain"]
                     or PyFunceble.CONFIGURATION["generate_json"])):
            # * We are testing a file.
            # and
            # * The hosts file generation is activated.
            # or
            # * The plain list generation is activated.

            # We initiate a variable which whill save the splited testination.
            splited_destination = ""

            # We initiate the list of all analytic related statuses.
            http_list = []
            http_list.extend(PyFunceble.STATUS["list"]["potentially_up"])
            http_list.extend(PyFunceble.STATUS["list"]["potentially_down"])
            http_list.extend(PyFunceble.STATUS["list"]["http_active"])
            http_list.extend(PyFunceble.STATUS["list"]["suspicious"])

            # We partially initiate the path to the hosts file.
            output_hosts = (self.output_parent_dir +
                            PyFunceble.OUTPUTS["hosts"]["directory"] + "%s" +
                            directory_separator +
                            PyFunceble.OUTPUTS["hosts"]["filename"])

            # We partially initiate the path to the plain list file.
            output_domains = (self.output_parent_dir +
                              PyFunceble.OUTPUTS["domains"]["directory"] +
                              "%s" + directory_separator +
                              PyFunceble.OUTPUTS["domains"]["filename"])

            # We partially intiate the path to the json list file.
            output_json = (self.output_parent_dir +
                           PyFunceble.OUTPUTS["json"]["directory"] + "%s" +
                           directory_separator +
                           PyFunceble.OUTPUTS["json"]["filename"])

            if self.domain_status.lower() in PyFunceble.STATUS["list"]["up"]:
                # The status is in the list of up list.

                # We complete the path to the hosts file.
                hosts_destination = output_hosts % PyFunceble.STATUS[
                    "official"]["up"]

                # We complete the path to the plain list file.
                plain_destination = output_domains % PyFunceble.STATUS[
                    "official"]["up"]

                # We complete the path to the json list file.
                json_destination = output_json % PyFunceble.STATUS["official"][
                    "up"]
            elif self.domain_status.lower(
            ) in PyFunceble.STATUS["list"]["valid"]:
                # The status is in the list of valid list.

                # We complete the path to the hosts file.
                hosts_destination = (output_hosts %
                                     PyFunceble.STATUS["official"]["valid"])

                # We complete the path to the plain list file.
                plain_destination = (output_domains %
                                     PyFunceble.STATUS["official"]["valid"])

                # We complete the path to the json list file.
                json_destination = output_json % PyFunceble.STATUS["official"][
                    "valid"]
            elif self.domain_status.lower(
            ) in PyFunceble.STATUS["list"]["down"]:
                # The status is in the list of down list.

                # We complete the path to the hosts file.
                hosts_destination = output_hosts % PyFunceble.STATUS[
                    "official"]["down"]

                # We complete the path to the plain list file.
                plain_destination = (output_domains %
                                     PyFunceble.STATUS["official"]["down"])

                # We complete the path to the json list file.
                json_destination = output_json % PyFunceble.STATUS["official"][
                    "down"]
            elif self.domain_status.lower(
            ) in PyFunceble.STATUS["list"]["invalid"]:
                # The status is in the list of invalid list.

                # We complete the path to the hosts file.
                hosts_destination = (output_hosts %
                                     PyFunceble.STATUS["official"]["invalid"])

                # We complete the path to the plain list file.
                plain_destination = (output_domains %
                                     PyFunceble.STATUS["official"]["invalid"])

                # We complete the path to the json list file.
                json_destination = (output_json %
                                    PyFunceble.STATUS["official"]["invalid"])
            elif self.domain_status.lower() in http_list:
                # The status is in the list of analytic status.

                # We construct the path to the analytic directory.
                output_dir = self._analytic_host_file_directory()

                if not output_dir.endswith(directory_separator):
                    # The output directory does not ends with the directory separator.

                    # We append the directory separator at the end of the output directory.
                    output_dir += directory_separator

                # We initiate the hosts file path.
                hosts_destination = output_dir + PyFunceble.OUTPUTS["hosts"][
                    "filename"]

                # We initiate the plain list file path.
                plain_destination = (output_dir +
                                     PyFunceble.OUTPUTS["domains"]["filename"])

                # We complete the path to the json list file.
                json_destination = output_dir + PyFunceble.OUTPUTS["json"][
                    "filename"]

                # We initiate the path to the http code file.
                # Note: We generate the http code file so that
                # we can have each domain in a file which is the
                # extracted http code.
                splited_destination = output_dir + str(
                    PyFunceble.INTERN["http_code"])

            if PyFunceble.CONFIGURATION["generate_hosts"]:
                # The hosts file generation is activated.

                # We generate/append the currently tested element in its
                # final location. (hosts file format)
                # We print on screen and on file.
                Prints(
                    [PyFunceble.CONFIGURATION["custom_ip"], self.tested],
                    "FullHosts",
                    hosts_destination,
                ).data()

            if PyFunceble.CONFIGURATION["plain_list_domain"]:
                # The plain list generation is activated.

                # We generate/append the currently tested element in its
                # final location. (the plain list format)
                # We print on file.
                Prints([self.tested], "PlainDomain", plain_destination).data()

            if PyFunceble.CONFIGURATION["split"] and splited_destination:
                # The splited list generation is activated.

                # We generate/append the currently tested element in its
                # final location. (the split list format)
                # We print on file.
                Prints([self.tested], "PlainDomain",
                       splited_destination).data()

            if PyFunceble.CONFIGURATION["generate_json"]:
                # The jsaon list generation is activated.

                # We generate/append the currently tested element in its
                # final location. (the json format)
                # We print on file.
                Prints([self.tested], "JSON", json_destination).data()