PyFunceble/execution_time.py
def _save(self, last=False):  # pragma: no cover
        """
        Save the current time to the file.

        :param last:
            Tell us if we are at the very end of the file testing.
        :type last: bool
        """

        if (
            self._authorization()
            and PyFunceble.CONFIGURATION["logs"]
            and "file_to_test" in PyFunceble.INTERN
            and PyFunceble.INTERN["file_to_test"]
        ):
            # * We are authorized to work.
            # and
            # * The generation of logs is activated.
            # and
            # * We are not testing as an imported module.

            # We set the location of the file we are working with.
            self.file = (
                PyFunceble.OUTPUT_DIRECTORY
                + PyFunceble.OUTPUTS["parent_directory"]
                + PyFunceble.OUTPUTS["logs"]["directories"]["parent"]
                + PyFunceble.OUTPUTS["logs"]["filenames"]["execution_time"]
            )

            if PyFunceble.path.isfile(self.file):
                # The file we are working with exist.

                # We get its content so we can directly work with it.
                content = Dict().from_json(File(self.file).read())
            else:
                # The file we are working with does not exist.

                # We generate a dummy content.
                content = {}

            if self.action == "start":
                # The action is equal to `start`.

                if "final_total" in content and content["final_total"]:
                    # The final total index exist.

                    # We delete it.
                    del content["final_total"]

                if "data" in content:
                    # The data index exist.

                    # We append the current start time inside it at
                    # a new sublist.
                    content["data"].append([PyFunceble.INTERN["start"]])
                else:
                    # The data index does not exist.

                    # We create the index along with the current start time.
                    content["data"] = [[PyFunceble.INTERN["start"]]]
            elif self.action == "stop":
                # The action is equal to `stop`.

                try:
                    # We try to work with the data index.

                    # We append the end time at the end of the last element
                    # of data.
                    #
                    # Note: It is at the end because we should have as first
                    # the star time.
                    content["data"][-1].append(PyFunceble.INTERN["end"])

                    # We get the start time.
                    start = content["data"][0][0]
                    # We get the end time.
                    end = content["data"][-1][-1]

                    # We calculate the execution time of the test.
                    content["current_total"] = self.format_execution_time(start, end)

                    if last:
                        # We are at the very end of the file testing.

                        # We initiate the global execution time.
                        content["final_total"] = content["current_total"]

                        # We inform the user about the global execution time.
                        print(
                            PyFunceble.Fore.MAGENTA
                            + PyFunceble.Style.BRIGHT
                            + "Global execution time: "
                            + content["final_total"]
                        )
                except KeyError:
                    # It is not possible to work with the data index because
                    # it does not exist.

                    # We ignore the problem.
                    pass

            try:
                # We try to save the whole data at its final location.
                Dict(content).to_json(self.file)
            except FileNotFoundError:
                # The directory was not found.

                # We construct the output directory
                DirectoryStructure()

                # And we retry to save the whole data at its final location.
                Dict(content).to_json(self.file)
def _save(self, last=False):  # pragma: no cover
        if (
            self._authorization()
            and PyFunceble.CONFIGURATION["logs"]
            and "file_to_test" in PyFunceble.INTERN
            and PyFunceble.INTERN["file_to_test"]
        ):
            # * We are authorized to work.
            # and
            # * The generation of logs is activated.
            # and
            # * We are not testing as an imported module.

            # We set the location of the file we are working with.
            self.file = (
                PyFunceble.OUTPUT_DIRECTORY
                + PyFunceble.OUTPUTS["parent_directory"]
                + PyFunceble.OUTPUTS["logs"]["directories"]["parent"]
                + PyFunceble.OUTPUTS["logs"]["filenames"]["execution_time"]
            )

            if PyFunceble.path.isfile(self.file):
                # The file we are working with exist.

                # We get its content so we can directly work with it.
                content = Dict().from_json(File(self.file).read())
            else:
                # The file we are working with does not exist.

                # We generate a dummy content.
                content = {}

            if self.action == "start":
                # The action is equal to `start`.

                if "final_total" in content and content["final_total"]:
                    # The final total index exist.

                    # We delete it.
                    del content["final_total"]

                if "data" in content:
                    # The data index exist.

                    # We append the current start time inside it at
                    # a new sublist.
                    content["data"].append([PyFunceble.INTERN["start"]])
                else:
                    # The data index does not exist.

                    # We create the index along with the current start time.
                    content["data"] = [[PyFunceble.INTERN["start"]]]
            elif self.action == "stop":
                # The action is equal to `stop`.

                try:
                    # We try to work with the data index.

                    # We append the end time at the end of the last element
                    # of data.
                    #
                    # Note: It is at the end because we should have as first
                    # the star time.
                    content["data"][-1].append(PyFunceble.INTERN["end"])

                    # We get the start time.
                    start = content["data"][0][0]
                    # We get the end time.
                    end = content["data"][-1][-1]

                    # We calculate the execution time of the test.
                    content["current_total"] = self.format_execution_time(start, end)

                    if last:
                        # We are at the very end of the file testing.

                        # We initiate the global execution time.
                        content["final_total"] = content["current_total"]

                        # We inform the user about the global execution time.
                        print(
                            PyFunceble.Fore.MAGENTA
                            + PyFunceble.Style.BRIGHT
                            + "Global execution time: "
                            + content["final_total"]
                        )
                except KeyError:
                    # It is not possible to work with the data index because
                    # it does not exist.

                    # We ignore the problem.
                    pass

            try:
                # We try to save the whole data at its final location.
                Dict(content).to_json(self.file)
            except FileNotFoundError:
                # The directory was not found.

                # We construct the output directory
                DirectoryStructure()

                # And we retry to save the whole data at its final location.
                Dict(content).to_json(self.file)
def _save ( self , last = False ) : # pragma: no cover if ( self . _authorization ( ) and PyFunceble . CONFIGURATION [ "logs" ] and "file_to_test" in PyFunceble . INTERN and PyFunceble . INTERN [ "file_to_test" ] ) : # * We are authorized to work. # and # * The generation of logs is activated. # and # * We are not testing as an imported module. # We set the location of the file we are working with. self . file = ( PyFunceble . OUTPUT_DIRECTORY + PyFunceble . OUTPUTS [ "parent_directory" ] + PyFunceble . OUTPUTS [ "logs" ] [ "directories" ] [ "parent" ] + PyFunceble . OUTPUTS [ "logs" ] [ "filenames" ] [ "execution_time" ] ) if PyFunceble . path . isfile ( self . file ) : # The file we are working with exist. # We get its content so we can directly work with it. content = Dict ( ) . from_json ( File ( self . file ) . read ( ) ) else : # The file we are working with does not exist. # We generate a dummy content. content = { } if self . action == "start" : # The action is equal to `start`. if "final_total" in content and content [ "final_total" ] : # The final total index exist. # We delete it. del content [ "final_total" ] if "data" in content : # The data index exist. # We append the current start time inside it at # a new sublist. content [ "data" ] . append ( [ PyFunceble . INTERN [ "start" ] ] ) else : # The data index does not exist. # We create the index along with the current start time. content [ "data" ] = [ [ PyFunceble . INTERN [ "start" ] ] ] elif self . action == "stop" : # The action is equal to `stop`. try : # We try to work with the data index. # We append the end time at the end of the last element # of data. # # Note: It is at the end because we should have as first # the star time. content [ "data" ] [ - 1 ] . append ( PyFunceble . INTERN [ "end" ] ) # We get the start time. start = content [ "data" ] [ 0 ] [ 0 ] # We get the end time. end = content [ "data" ] [ - 1 ] [ - 1 ] # We calculate the execution time of the test. content [ "current_total" ] = self . format_execution_time ( start , end ) if last : # We are at the very end of the file testing. # We initiate the global execution time. content [ "final_total" ] = content [ "current_total" ] # We inform the user about the global execution time. print ( PyFunceble . Fore . MAGENTA + PyFunceble . Style . BRIGHT + "Global execution time: " + content [ "final_total" ] ) except KeyError : # It is not possible to work with the data index because # it does not exist. # We ignore the problem. pass try : # We try to save the whole data at its final location. Dict ( content ) . to_json ( self . file ) except FileNotFoundError : # The directory was not found. # We construct the output directory DirectoryStructure ( ) # And we retry to save the whole data at its final location. Dict ( content ) . to_json ( self . file )
Save the current time to the file.

        :param last:
            Tell us if we are at the very end of the file testing.
        :type last: bool
Save the current time to the file .