def _reformat_historical_formating_error(self):  # pragma: no cover
        """
        Format the old format so it can be merged into the newer format.
        """

        if PyFunceble.CONFIGURATION["inactive_database"]:
            # The database subsystem is activated.

            # We construct the possible path to an older version of the database.
            historical_formating_error = (
                PyFunceble.CURRENT_DIRECTORY + "inactive-db.json"
            )

            if PyFunceble.path.isfile(historical_formating_error):
                # The histortical file already exists.

                # We get its content.
                data = Dict().from_json(File(historical_formating_error).read())

                # We initiate a variable which will save the data that is going
                # to be merged.
                data_to_parse = {}

                # We get the database keybase.
                top_keys = data.keys()

                for top_key in top_keys:
                    # We loop through the list of upper keys.

                    # We get the lowest keys.
                    low_keys = data[top_key].keys()

                    # We initiate the data to parse.
                    data_to_parse[top_key] = {}

                    for low_key in low_keys:
                        # We loop through the list of lower keys.

                        if low_key.isdigit():
                            # The current low key is a digit.

                            # We parse its content (from the old) into the new format.
                            # In between, we remove 30 days from the low_key so that
                            # it become in the past. This way they will be retested
                            # automatically.
                            data_to_parse[top_key][
                                int(low_key) - (self.one_day_in_seconds * 30)
                            ] = data[top_key][low_key]
                        else:
                            # The current low key is not a digit.

                            # We parse its content (from the old) into the new format.
                            # In between, we remove 30 days from the current time so that
                            # it become in the past. This way they will be retested
                            # automatically.
                            data_to_parse[top_key][
                                int(PyFunceble.time()) - (self.one_day_in_seconds * 30)
                            ] = data[top_key][low_key]

                if "inactive_db" in PyFunceble.INTERN:
                    # The current (new) database is not empty.

                    # We update add the content of the old into the current database.
                    PyFunceble.INTERN["inactive_db"].update(data_to_parse)
                else:
                    # The current (new) database is empty.

                    # We replace the content with the data_to_parse as it is complient
                    # with the new format.
                    PyFunceble.INTERN["inactive_db"] = data_to_parse

                # We delete the old database file.
                File(historical_formating_error).delete()

def _reformat_historical_formating_error ( self ) : if PyFunceble . CONFIGURATION [ "inactive_database" ] : historical_formating_error = ( PyFunceble . CURRENT_DIRECTORY + "inactive-db.json" ) if PyFunceble . path . isfile ( historical_formating_error ) : data = Dict ( ) . from_json ( File ( historical_formating_error ) . read ( ) ) data_to_parse = { } top_keys = data . keys ( ) for top_key in top_keys : low_keys = data [ top_key ] . keys ( ) data_to_parse [ top_key ] = { } for low_key in low_keys : if low_key . isdigit ( ) : data_to_parse [ top_key ] [ int ( low_key ) - ( self . one_day_in_seconds * 30 ) ] = data [ top_key ] [ low_key ] else : data_to_parse [ top_key ] [ int ( PyFunceble . time ( ) ) - ( self . one_day_in_seconds * 30 ) ] = data [ top_key ] [ low_key ] if "inactive_db" in PyFunceble . INTERN : PyFunceble . INTERN [ "inactive_db" ] . update ( data_to_parse ) else : PyFunceble . INTERN [ "inactive_db" ] = data_to_parse File ( historical_formating_error ) . delete ( )

Format the old format so it can be merged into the newer format .


def _retrieve(self):
        """
        Return the current content of the inactive-db.json file.
        """

        if PyFunceble.CONFIGURATION["inactive_database"]:
            # The database subsystem is activated.

            # We get, format and initiate the historical database file.
            self._reformat_historical_formating_error()

            if PyFunceble.path.isfile(self.inactive_db_path):
                # The database file exist.

                # We merge our current database into already initiated one.
                self._merge()

def _retrieve ( self ) : if PyFunceble . CONFIGURATION [ "inactive_database" ] : self . _reformat_historical_formating_error ( ) if PyFunceble . path . isfile ( self . inactive_db_path ) : self . _merge ( )

Return the current content of the inactive - db . json file .


def _backup(self):
        """
        Save the current database into the inactive-db.json file.
        """

        if PyFunceble.CONFIGURATION["inactive_database"]:
            # The database subsystem is activated.

            # We save the current database state into the database file.
            Dict(PyFunceble.INTERN["inactive_db"]).to_json(self.inactive_db_path)

def _backup ( self ) : if PyFunceble . CONFIGURATION [ "inactive_database" ] : Dict ( PyFunceble . INTERN [ "inactive_db" ] ) . to_json ( self . inactive_db_path )

Save the current database into the inactive - db . json file .


def _timestamp(self):
        """
        Get the timestamp where we are going to save our current list.

        :return: The timestamp to append with the currently tested element.
        :rtype: int|str
        """

        if PyFunceble.CONFIGURATION["inactive_database"]:
            # The database subsystem is activated.

            if (
                "inactive_db" in PyFunceble.INTERN
                and PyFunceble.INTERN["file_to_test"]
                in PyFunceble.INTERN["inactive_db"]
                and PyFunceble.INTERN["inactive_db"][PyFunceble.INTERN["file_to_test"]]
            ):
                # The file we are testing is into the database and its content
                # is not empty.

                # We get the indexes of the current file (in the dabase).
                database_keys = [
                    x
                    for x in PyFunceble.INTERN["inactive_db"][
                        PyFunceble.INTERN["file_to_test"]
                    ].keys()
                    if x.isdigit()
                ]

                if database_keys:
                    # The list of keys is not empty.

                    # We get the most recent date.
                    recent_date = max(database_keys)
                else:  # pragma: no cover
                    # The list of keys is empty.

                    # We return the current time.
                    return int(PyFunceble.time())

                if int(PyFunceble.time()) > int(recent_date) + self.one_day_in_seconds:
                    # The most recent time was in more than one day.

                    # We return the current time.
                    return int(PyFunceble.time())

                # The most recent time was in less than one day.

                if int(PyFunceble.time()) < int(recent_date) + self.days_in_seconds:
                    # The most recent time was in less than the expected number of day for
                    # retesting.

                    # We return the most recent data.
                    return int(recent_date)

        # The database subsystem is not activated.

        # We return the current time.
        return int(PyFunceble.time())

def _timestamp ( self ) : if PyFunceble . CONFIGURATION [ "inactive_database" ] : if ( "inactive_db" in PyFunceble . INTERN and PyFunceble . INTERN [ "file_to_test" ] in PyFunceble . INTERN [ "inactive_db" ] and PyFunceble . INTERN [ "inactive_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] ) : database_keys = [ x for x in PyFunceble . INTERN [ "inactive_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] . keys ( ) if x . isdigit ( ) ] if database_keys : recent_date = max ( database_keys ) else : return int ( PyFunceble . time ( ) ) if int ( PyFunceble . time ( ) ) > int ( recent_date ) + self . one_day_in_seconds : return int ( PyFunceble . time ( ) ) if int ( PyFunceble . time ( ) ) < int ( recent_date ) + self . days_in_seconds : return int ( recent_date ) return int ( PyFunceble . time ( ) )

Get the timestamp where we are going to save our current list .


def content(cls):
        """
        Get the content of the database.

        :return: The content of the database.
        :rtype: list
        """

        # We initiate a variable which will save what we are going to return.
        result = []

        if (
            PyFunceble.CONFIGURATION["inactive_database"]
            and PyFunceble.INTERN["inactive_db"]
        ):
            # * The database subsystem is activated.
            # and
            # * The database is not empty.

            for key in PyFunceble.INTERN["inactive_db"][
                PyFunceble.INTERN["file_to_test"]
            ]:
                # We loop through the index of the current file database.

                if key == "to_test":
                    # The current key is `to_test`.

                    # We continue to the next element.
                    continue

                # We extend the result with the content of the currently read index.
                result.extend(
                    PyFunceble.INTERN["inactive_db"][PyFunceble.INTERN["file_to_test"]][
                        key
                    ]
                )

        # We return the content of the database.
        return result

def content ( cls ) : result = [ ] if ( PyFunceble . CONFIGURATION [ "inactive_database" ] and PyFunceble . INTERN [ "inactive_db" ] ) : for key in PyFunceble . INTERN [ "inactive_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] : if key == "to_test" : continue result . extend ( PyFunceble . INTERN [ "inactive_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ key ] ) return result


def is_present(cls):
        """
        Check if the currently tested element is into the database.
        """

        if PyFunceble.CONFIGURATION["inactive_database"]:
            # The database subsystem is activated.

            if PyFunceble.INTERN["to_test"] in PyFunceble.INTERN[
                "flatten_inactive_db"
            ] or (
                PyFunceble.INTERN["file_to_test"] in PyFunceble.INTERN["inactive_db"]
                and PyFunceble.INTERN["inactive_db"][PyFunceble.INTERN["file_to_test"]]
                and "to_test"
                in PyFunceble.INTERN["inactive_db"][PyFunceble.INTERN["file_to_test"]]
                and PyFunceble.INTERN["to_test"]
                in PyFunceble.INTERN["inactive_db"][PyFunceble.INTERN["file_to_test"]][
                    "to_test"
                ]
            ):
                return True

        return False

def is_present ( cls ) : if PyFunceble . CONFIGURATION [ "inactive_database" ] : if PyFunceble . INTERN [ "to_test" ] in PyFunceble . INTERN [ "flatten_inactive_db" ] or ( PyFunceble . INTERN [ "file_to_test" ] in PyFunceble . INTERN [ "inactive_db" ] and PyFunceble . INTERN [ "inactive_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] and "to_test" in PyFunceble . INTERN [ "inactive_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] and PyFunceble . INTERN [ "to_test" ] in PyFunceble . INTERN [ "inactive_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ "to_test" ] ) : return True return False

Check if the currently tested element is into the database .


def _retrieve(self):
        """
        Retrieve the data from the database.
        """

        if self._authorization() and "whois_db" not in PyFunceble.INTERN:
            # The usage of the whois database is activated.

            if PyFunceble.path.isfile(self.whois_db_path):
                # The database file exist.

                # We merge our current database into already initiated one.
                PyFunceble.INTERN["whois_db"] = Dict().from_json(
                    File(self.whois_db_path).read()
                )
            else:
                # The database file does not exist.

                # We initiate an empty database.
                PyFunceble.INTERN["whois_db"] = {}

def _retrieve ( self ) : if self . _authorization ( ) and "whois_db" not in PyFunceble . INTERN : if PyFunceble . path . isfile ( self . whois_db_path ) : PyFunceble . INTERN [ "whois_db" ] = Dict ( ) . from_json ( File ( self . whois_db_path ) . read ( ) ) else : PyFunceble . INTERN [ "whois_db" ] = { }


def _backup(self):
        """
        Backup the database into its file.
        """

        if self._authorization():
            # We are authorized to work.

            # We backup the current state of the datbase.
            Dict(PyFunceble.INTERN["whois_db"]).to_json(self.whois_db_path)

def _backup ( self ) : if self . _authorization ( ) : Dict ( PyFunceble . INTERN [ "whois_db" ] ) . to_json ( self . whois_db_path )


def is_in_database(self):
        """
        Check if the element is into the database.
        """

        if (
            self._authorization()
            and PyFunceble.INTERN["file_to_test"] in PyFunceble.INTERN["whois_db"]
            and PyFunceble.INTERN["to_test"]
            in PyFunceble.INTERN["whois_db"][PyFunceble.INTERN["file_to_test"]]
        ):
            # * We are authorized to work.
            # and
            # * The given file path exist in the database.
            # and
            # * The element we are testing is in the database related to the
            # given file path.

            # We return True, the element we are testing is into the database.
            return True

        # * We are not authorized to work.
        # or
        # * The given file path does not exist in the database.
        # or
        # * The element we are testing is not in the database related to the
        # given file path.

        # We return False,the element we are testing is not into the database.
        return False

def is_in_database ( self ) : if ( self . _authorization ( ) and PyFunceble . INTERN [ "file_to_test" ] in PyFunceble . INTERN [ "whois_db" ] and PyFunceble . INTERN [ "to_test" ] in PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] ) : return True return False

Check if the element is into the database .


def is_time_older(self):
        """
        Check if the current time is older than the one in the database.
        """

        if (
            self._authorization()
            and self.is_in_database()
            and int(
                PyFunceble.INTERN["whois_db"][PyFunceble.INTERN["file_to_test"]][
                    PyFunceble.INTERN["to_test"]
                ]["epoch"]
            )
            < int(PyFunceble.time())
        ):
            # * We are authorized to work.
            # and
            # * The element we are testing is in the database.
            # and
            # * The epoch of the expiration date is less than our current epoch.

            # The expiration date is in the past, we return True.
            return True

        # The expiration date is in the future, we return False.
        return False

def is_time_older ( self ) : if ( self . _authorization ( ) and self . is_in_database ( ) and int ( PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ PyFunceble . INTERN [ "to_test" ] ] [ "epoch" ] ) < int ( PyFunceble . time ( ) ) ) : return True return False

Check if the current time is older than the one in the database .


def get_expiration_date(self):
        """
        Get the expiration date from the database.

        :return: The expiration date from the database.
        :rtype: str|None
        """

        if self._authorization() and self.is_in_database() and not self.is_time_older():
            # * We are authorized to work.
            # and
            # * The element we are testing is in the database.
            # and
            # * The expiration date is in the future.

            # We get the expiration date from the database.
            result = PyFunceble.INTERN["whois_db"][PyFunceble.INTERN["file_to_test"]][
                PyFunceble.INTERN["to_test"]
            ]["expiration_date"]

            if result:
                # The expiration date from the database is not empty nor
                # equal to None.

                # We return it.
                return result

        # We return None, there is no data to work with.
        return None

def get_expiration_date ( self ) : if self . _authorization ( ) and self . is_in_database ( ) and not self . is_time_older ( ) : result = PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ PyFunceble . INTERN [ "to_test" ] ] [ "expiration_date" ] if result : return result return None

Get the expiration date from the database .


def add(self):
        """
        Add the currently tested element into the database.
        """

        if self._authorization():
            # We are authorized to work.

            if self.epoch < int(PyFunceble.time()):
                state = "past"
            else:
                state = "future"

            if self.is_in_database():
                # The element we are working with is in the database.

                if (
                    str(self.epoch)
                    != PyFunceble.INTERN["whois_db"][PyFunceble.INTERN["file_to_test"]][
                        PyFunceble.INTERN["to_test"]
                    ]["epoch"]
                ):
                    # The given epoch is diffent from the one saved.

                    # We update it.
                    PyFunceble.INTERN["whois_db"][PyFunceble.INTERN["file_to_test"]][
                        PyFunceble.INTERN["to_test"]
                    ].update(
                        {
                            "epoch": str(self.epoch),
                            "state": state,
                            "expiration_date": self.expiration_date,
                        }
                    )

                elif self.is_time_older():
                    # The expiration date from the database is in the past.

                    if (
                        PyFunceble.INTERN["whois_db"][
                            PyFunceble.INTERN["file_to_test"]
                        ][PyFunceble.INTERN["to_test"]]["state"]
                        != "past"
                    ):  # pragma: no cover
                        # The state of the element in the datbase is not
                        # equal to `past`.

                        # We update it to `past`.
                        PyFunceble.INTERN["whois_db"][
                            PyFunceble.INTERN["file_to_test"]
                        ][PyFunceble.INTERN["to_test"]].update({"state": "past"})
                elif (
                    PyFunceble.INTERN["whois_db"][PyFunceble.INTERN["file_to_test"]][
                        PyFunceble.INTERN["to_test"]
                    ]["state"]
                    != "future"
                ):
                    # * The expiration date from the database is in the future.
                    # and
                    # * The state of the element in the database is not
                    # equal to `future`.

                    # We update it to `future`.
                    PyFunceble.INTERN["whois_db"][PyFunceble.INTERN["file_to_test"]][
                        PyFunceble.INTERN["to_test"]
                    ].update({"state": "future"})
            else:
                # The element we are working with is not in the database.

                if (
                    not PyFunceble.INTERN["file_to_test"]
                    in PyFunceble.INTERN["whois_db"]
                ):
                    # The file path is not in the database.

                    # We initiate it.
                    PyFunceble.INTERN["whois_db"][
                        PyFunceble.INTERN["file_to_test"]
                    ] = {}

                # We create the first dataset.
                PyFunceble.INTERN["whois_db"][PyFunceble.INTERN["file_to_test"]].update(
                    {
                        PyFunceble.INTERN["to_test"]: {
                            "epoch": str(self.epoch),
                            "state": state,
                            "expiration_date": self.expiration_date,
                        }
                    }
                )

            # We do a safety backup of our database.
            self._backup()

def add ( self ) : if self . _authorization ( ) : if self . epoch < int ( PyFunceble . time ( ) ) : state = "past" else : state = "future" if self . is_in_database ( ) : if ( str ( self . epoch ) != PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ PyFunceble . INTERN [ "to_test" ] ] [ "epoch" ] ) : PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ PyFunceble . INTERN [ "to_test" ] ] . update ( { "epoch" : str ( self . epoch ) , "state" : state , "expiration_date" : self . expiration_date , } ) elif self . is_time_older ( ) : if ( PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ PyFunceble . INTERN [ "to_test" ] ] [ "state" ] != "past" ) : PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ PyFunceble . INTERN [ "to_test" ] ] . update ( { "state" : "past" } ) elif ( PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ PyFunceble . INTERN [ "to_test" ] ] [ "state" ] != "future" ) : PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] [ PyFunceble . INTERN [ "to_test" ] ] . update ( { "state" : "future" } ) else : if ( not PyFunceble . INTERN [ "file_to_test" ] in PyFunceble . INTERN [ "whois_db" ] ) : PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] = { } PyFunceble . INTERN [ "whois_db" ] [ PyFunceble . INTERN [ "file_to_test" ] ] . update ( { PyFunceble . INTERN [ "to_test" ] : { "epoch" : str ( self . epoch ) , "state" : state , "expiration_date" : self . expiration_date , } } ) self . _backup ( )

Add the currently tested element into the database .


def travis_permissions(cls):
        """
        Set permissions in order to avoid issues before commiting.
        """

        if PyFunceble.CONFIGURATION["travis"]:
            try:
                build_dir = PyFunceble.environ["TRAVIS_BUILD_DIR"]
                commands = [
                    "sudo chown -R travis:travis %s" % (build_dir),
                    "sudo chgrp -R travis %s" % (build_dir),
                    "sudo chmod -R g+rwX %s" % (build_dir),
                    "sudo chmod 777 -Rf %s.git"
                    % (build_dir + PyFunceble.directory_separator),
                    r"sudo find %s -type d -exec chmod g+x '{}' \;" % (build_dir),
                ]

                for command in commands:
                    Command(command).execute()

                if Command("git config core.sharedRepository").execute() == "":
                    Command("git config core.sharedRepository group").execute()
            except KeyError:
                pass

Set permissions in order to avoid issues before commiting .


def _travis(self):
        """
        Logic behind autosave under Travis CI.
        """

        if PyFunceble.CONFIGURATION["travis"]:
            try:
                _ = PyFunceble.environ["TRAVIS_BUILD_DIR"]
                time_autorisation = False

                try:
                    time_autorisation = int(PyFunceble.time()) >= int(
                        PyFunceble.INTERN["start"]
                    ) + (int(PyFunceble.CONFIGURATION["travis_autosave_minutes"]) * 60)
                except KeyError:
                    if self.last and not self.bypass:
                        raise Exception(
                            "Please review the way `ExecutionTime()` is called."
                        )

                if self.last or time_autorisation or self.bypass:
                    Percentage().log()
                    self.travis_permissions()

                    command = 'git add --all && git commit -a -m "%s"'

                    if self.last or self.bypass:
                        if PyFunceble.CONFIGURATION["command_before_end"]:
                            for line in Command(
                                PyFunceble.CONFIGURATION["command_before_end"]
                            ).run():
                                sys_stdout.write("{}\n".format(line))

                            self.travis_permissions()

                        message = (
                            PyFunceble.CONFIGURATION["travis_autosave_final_commit"]
                            + " [ci skip]"
                        )

                        Command(command % message).execute()
                    else:
                        if PyFunceble.CONFIGURATION["command"]:
                            for line in Command(
                                PyFunceble.CONFIGURATION["command"]
                            ).run():
                                sys_stdout.write("{}\n".format(line))

                            self.travis_permissions()

                        Command(
                            command % PyFunceble.CONFIGURATION["travis_autosave_commit"]
                        ).execute()

                    print(
                        Command(
                            "git push origin %s"
                            % PyFunceble.CONFIGURATION["travis_branch"]
                        ).execute()
                    )
                    exit(0)
            except KeyError:
                pass


def nslookup(cls):
        """
        Implementation of UNIX nslookup.
        """

        try:
            # We try to get the addresse information of the given domain or IP.

            if "current_test_data" in PyFunceble.INTERN:  # pragma: no cover
                # The end-user want more information whith his test.

                if not Check().is_ip_valid():
                    # The element we are testing is not an IP.

                    # We request the address informations.
                    request = PyFunceble.socket.getaddrinfo(
                        PyFunceble.INTERN["to_test"],
                        80,
                        0,
                        0,
                        PyFunceble.socket.IPPROTO_TCP,
                    )

                    for sequence in request:
                        # We loop through the sequence returned by the request.

                        # We append the NS informations into the nslookup index.
                        PyFunceble.INTERN["current_test_data"]["nslookup"].append(
                            sequence[-1][0]
                        )
                else:
                    # The element we are testing is an IP.
                    request = PyFunceble.socket.gethostbyaddr(
                        PyFunceble.INTERN["to_test"]
                    )

                    # We append the NS informations into the nslookup index.
                    PyFunceble.INTERN["current_test_data"]["nslookup"][
                        "hostname"
                    ] = request[0]
                    PyFunceble.INTERN["current_test_data"]["nslookup"][
                        "aliases"
                    ] = request[1]
                    PyFunceble.INTERN["current_test_data"]["nslookup"]["ips"] = request[
                        2
                    ]
            else:

                if not Check().is_ip_valid():
                    # The element we are testing is not an IP.
                    PyFunceble.socket.getaddrinfo(
                        PyFunceble.INTERN["to_test"],
                        80,
                        0,
                        0,
                        PyFunceble.socket.IPPROTO_TCP,
                    )
                else:
                    # The element we are testing is an IP.
                    PyFunceble.socket.gethostbyaddr(PyFunceble.INTERN["to_test"])

            # It was done successfuly, we return True.
            # Note: we don't need to read the addresses so we consider as successful
            # as long as there is no error.
            return True

        except (OSError, PyFunceble.socket.herror, PyFunceble.socket.gaierror):
            # One of the listed exception is matched.

            # It was done unsuccesfuly, we return False.
            return False

def nslookup ( cls ) : try : if "current_test_data" in PyFunceble . INTERN : if not Check ( ) . is_ip_valid ( ) : request = PyFunceble . socket . getaddrinfo ( PyFunceble . INTERN [ "to_test" ] , 80 , 0 , 0 , PyFunceble . socket . IPPROTO_TCP , ) for sequence in request : PyFunceble . INTERN [ "current_test_data" ] [ "nslookup" ] . append ( sequence [ - 1 ] [ 0 ] ) else : request = PyFunceble . socket . gethostbyaddr ( PyFunceble . INTERN [ "to_test" ] ) PyFunceble . INTERN [ "current_test_data" ] [ "nslookup" ] [ "hostname" ] = request [ 0 ] PyFunceble . INTERN [ "current_test_data" ] [ "nslookup" ] [ "aliases" ] = request [ 1 ] PyFunceble . INTERN [ "current_test_data" ] [ "nslookup" ] [ "ips" ] = request [ 2 ] else : if not Check ( ) . is_ip_valid ( ) : PyFunceble . socket . getaddrinfo ( PyFunceble . INTERN [ "to_test" ] , 80 , 0 , 0 , PyFunceble . socket . IPPROTO_TCP , ) else : PyFunceble . socket . gethostbyaddr ( PyFunceble . INTERN [ "to_test" ] ) return True except ( OSError , PyFunceble . socket . herror , PyFunceble . socket . gaierror ) : return False


def whois(cls, whois_server, domain=None, timeout=None):  # pragma: no cover
        """
        Implementation of UNIX whois.

        :param whois_server: The WHOIS server to use to get the record.
        :type whois_server: str

        :param domain: The domain to get the whois record from.
        :type domain: str

        :param timeout: The timeout to apply to the request.
        :type timeout: int

        :return: The whois record from the given whois server, if exist.
        :rtype: str|None
        """

        if domain is None:
            # The domain is not given (localy).

            # We consider the domain as the domain or IP we are currently testing.
            domain = PyFunceble.INTERN["to_test"]

        if timeout is None:
            # The time is not given (localy).

            # We consider the timeout from the configuration as the timeout to use.
            timeout = PyFunceble.CONFIGURATION["seconds_before_http_timeout"]

        if whois_server:
            # A whois server is given.

            # We initiate a PyFunceble.socket.
            req = PyFunceble.socket.socket(
                PyFunceble.socket.AF_INET, PyFunceble.socket.SOCK_STREAM
            )

            if timeout % 3 == 0:
                # The timeout is modulo 3.

                # We report the timeout to our initiated PyFunceble.socket.
                req.settimeout(timeout)
            else:
                # The timeout is not modulo 3.

                # We report 3 seconds as the timeout to our initiated PyFunceble.socket.
                req.settimeout(3)

            try:
                # We try to connect to the whois server at the port 43.
                req.connect((whois_server, 43))
            except PyFunceble.socket.error:
                # We got an error.

                # We return None.
                return None

            # We send end encode the domain we want the data from.
            req.send((domain + "\r\n").encode())

            # We initiate a bytes variable which will save the response
            # from the server.
            response = b""

            while True:
                # We loop infinitly.
                try:
                    # We try to receive the data in a buffer of 4096 bytes.
                    data = req.recv(4096)
                except (PyFunceble.socket.timeout, ConnectionResetError):
                    # We got an error.

                    # We close the connection.
                    req.close()

                    # And we return None.
                    return None

                # Everything goes right.

                # We append data to the response we got.
                response += data

                if not data:
                    # The data is empty.

                    # We break the loop.
                    break

            # We close the connection.
            req.close()

            try:

                # We finally decode and return the response we got from the
                # server.
                return response.decode()

            except UnicodeDecodeError:
                # We got an encoding error.

                # We decode the response.
                # Note: Because we don't want to deal with other issue, we
                # decided to use `replace` in order to automatically replace
                # all non utf-8 encoded characters.
                return response.decode("utf-8", "replace")

        # The whois server is not given.

        # We return None.
        return None

def whois ( cls , whois_server , domain = None , timeout = None ) : if domain is None : domain = PyFunceble . INTERN [ "to_test" ] if timeout is None : timeout = PyFunceble . CONFIGURATION [ "seconds_before_http_timeout" ] if whois_server : req = PyFunceble . socket . socket ( PyFunceble . socket . AF_INET , PyFunceble . socket . SOCK_STREAM ) if timeout % 3 == 0 : req . settimeout ( timeout ) else : req . settimeout ( 3 ) try : req . connect ( ( whois_server , 43 ) ) except PyFunceble . socket . error : return None req . send ( ( domain + "\r\n" ) . encode ( ) ) response = b"" while True : try : data = req . recv ( 4096 ) except ( PyFunceble . socket . timeout , ConnectionResetError ) : req . close ( ) return None response += data if not data : break req . close ( ) try : return response . decode ( ) except UnicodeDecodeError : return response . decode ( "utf-8" , "replace" ) return None


def get(cls):  # pragma: no cover
        """
        Execute the logic behind the URL handling.

        :return: The status of the URL.
        :rtype: str
        """

        if Check().is_url_valid() or PyFunceble.CONFIGURATION["local"]:
            # * The url is valid.
            # or
            # * We are testing in/for a local or private network.

            if "current_test_data" in PyFunceble.INTERN:
                PyFunceble.INTERN["current_test_data"]["url_syntax_validation"] = True

            # We initiate the HTTP status code.
            PyFunceble.INTERN.update({"http_code": HTTPCode().get()})

            # We initiate the list of active status code.
            active_list = []
            active_list.extend(PyFunceble.HTTP_CODE["list"]["potentially_up"])
            active_list.extend(PyFunceble.HTTP_CODE["list"]["up"])

            # We initiate the list of inactive status code.
            inactive_list = []
            inactive_list.extend(PyFunceble.HTTP_CODE["list"]["potentially_down"])
            inactive_list.append("*" * 3)

            if PyFunceble.INTERN["http_code"] in active_list:
                # The extracted HTTP status code is in the list of active list.

                # We handle and return the up status.
                return URLStatus(PyFunceble.STATUS["official"]["up"]).handle()

            if PyFunceble.INTERN["http_code"] in inactive_list:
                # The extracted HTTP status code is in the list of inactive list.

                # We handle and return the down status.
                return URLStatus(PyFunceble.STATUS["official"]["down"]).handle()

        # The extracted HTTP status code is not in the list of active nor invalid list.

        if "current_test_data" in PyFunceble.INTERN:
            # The end-user want more information whith his test.

            # We update the url_syntax_validation index.
            PyFunceble.INTERN["current_test_data"]["url_syntax_validation"] = False

        # We handle and return the invalid down status.
        return URLStatus(PyFunceble.STATUS["official"]["invalid"]).handle()

def get ( cls ) : if Check ( ) . is_url_valid ( ) or PyFunceble . CONFIGURATION [ "local" ] : if "current_test_data" in PyFunceble . INTERN : PyFunceble . INTERN [ "current_test_data" ] [ "url_syntax_validation" ] = True PyFunceble . INTERN . update ( { "http_code" : HTTPCode ( ) . get ( ) } ) active_list = [ ] active_list . extend ( PyFunceble . HTTP_CODE [ "list" ] [ "potentially_up" ] ) active_list . extend ( PyFunceble . HTTP_CODE [ "list" ] [ "up" ] ) inactive_list = [ ] inactive_list . extend ( PyFunceble . HTTP_CODE [ "list" ] [ "potentially_down" ] ) inactive_list . append ( "*" * 3 ) if PyFunceble . INTERN [ "http_code" ] in active_list : return URLStatus ( PyFunceble . STATUS [ "official" ] [ "up" ] ) . handle ( ) if PyFunceble . INTERN [ "http_code" ] in inactive_list : return URLStatus ( PyFunceble . STATUS [ "official" ] [ "down" ] ) . handle ( ) if "current_test_data" in PyFunceble . INTERN : PyFunceble . INTERN [ "current_test_data" ] [ "url_syntax_validation" ] = False return URLStatus ( PyFunceble . STATUS [ "official" ] [ "invalid" ] ) . handle ( )

Execute the logic behind the URL handling .


def get(self):
        """
        Return the referer aka the WHOIS server of the current domain extension.
        """

        if not PyFunceble.CONFIGURATION["local"]:
            # We are not running a test in a local network.

            if self.domain_extension not in self.ignored_extension:
                # The extension of the domain we are testing is not into
                # the list of ignored extensions.

                # We set the referer to None as we do not have any.
                referer = None

                if self.domain_extension in PyFunceble.INTERN["iana_db"]:
                    # The domain extension is in the iana database.

                    if not PyFunceble.CONFIGURATION["no_whois"]:
                        # We are authorized to use WHOIS for the test result.

                        # We get the referer from the database.
                        referer = PyFunceble.INTERN["iana_db"][self.domain_extension]

                        if not referer:
                            # The referer is not filled.

                            # We log the case of the current extension.
                            Logs().referer_not_found(self.domain_extension)

                            # And we handle and return None status.
                            return None

                        # The referer is into the database.

                        # We return the extracted referer.
                        return referer

                    # We are not authorized to use WHOIS for the test result.

                    # We return None.
                    return None

                # The domain extension is not in the iana database.

                # We return False, it is an invalid domain.
                return False

            # The extension of the domain we are testing is not into
            # the list of ignored extensions.

            # We return None, the domain does not have a whois server.
            return None

        # We are running a test in a local network.

        # We return None.
        return None

def get ( self ) : if not PyFunceble . CONFIGURATION [ "local" ] : if self . domain_extension not in self . ignored_extension : referer = None if self . domain_extension in PyFunceble . INTERN [ "iana_db" ] : if not PyFunceble . CONFIGURATION [ "no_whois" ] : referer = PyFunceble . INTERN [ "iana_db" ] [ self . domain_extension ] if not referer : Logs ( ) . referer_not_found ( self . domain_extension ) return None return referer return None return False return None return None

Return the referer aka the WHOIS server of the current domain extension .


def _get_current_object(self):
        """Get current object.
        This is useful if you want the real
        object behind the proxy at a time for performance reasons or because
        you want to pass the object into a different context.
        """
        loc = object.__getattribute__(self, '_Proxy__local')
        if not hasattr(loc, '__release_local__'):
            return loc(*self.__args, **self.__kwargs)
        try:  # pragma: no cover
            # not sure what this is about
            return getattr(loc, self.__name__)
        except AttributeError:  # pragma: no cover
            raise RuntimeError('no object bound to {0.__name__}'.format(self))

def
_get_current_object
(
self
)
:
loc
=
object
.
__getattribute__
(
self
,
'_Proxy__local'
)
if
not
hasattr
(
loc
,
'__release_local__'
)
:
return
loc
(
*
self
.
__args
,
**
self
.
__kwargs
)
try
:
return
getattr
(
loc
,
self
.
__name__
)
except
AttributeError
:
raise
RuntimeError
(
'no object bound to {0.__name__}'
.
format
(
self
)
)

Get current object . This is useful if you want the real object behind the proxy at a time for performance reasons or because you want to pass the object into a different context .


def standard_paths():
    """Yield paths to standard modules."""
    for is_plat_spec in [True, False]:
        path = distutils.sysconfig.get_python_lib(standard_lib=True,
                                                  plat_specific=is_plat_spec)

        for name in os.listdir(path):
            yield name

        try:
            for name in os.listdir(os.path.join(path, 'lib-dynload')):
                yield name
        except OSError:  # pragma: no cover
            pass

def standard_paths ( ) : for is_plat_spec in [ True , False ] : path = distutils . sysconfig . get_python_lib ( standard_lib = True , plat_specific = is_plat_spec ) for name in os . listdir ( path ) : yield name try : for name in os . listdir ( os . path . join ( path , 'lib-dynload' ) ) : yield name except OSError : pass


def standard_package_names():
    """Yield standard module names."""
    for name in standard_paths():
        if name.startswith('_') or '-' in name:
            continue

        if '.' in name and name.rsplit('.')[-1] not in ['so', 'py', 'pyc']:
            continue

        yield name.split('.')[0]

def standard_package_names ( ) : for name in standard_paths ( ) : if name . startswith ( '_' ) or '-' in name : continue if '.' in name and name . rsplit ( '.' ) [ - 1 ] not in [ 'so' , 'py' , 'pyc' ] : continue yield name . split ( '.' ) [ 0 ]


def unused_import_line_numbers(messages):
    """Yield line numbers of unused imports."""
    for message in messages:
        if isinstance(message, pyflakes.messages.UnusedImport):
            yield message.lineno

def unused_import_line_numbers ( messages ) : for message in messages : if isinstance ( message , pyflakes . messages . UnusedImport ) : yield message . lineno


def unused_import_module_name(messages):
    """Yield line number and module name of unused imports."""
    pattern = r'\'(.+?)\''
    for message in messages:
        if isinstance(message, pyflakes.messages.UnusedImport):
            module_name = re.search(pattern, str(message))
            module_name = module_name.group()[1:-1]
            if module_name:
                yield (message.lineno, module_name)

def unused_import_module_name ( messages ) : pattern = r'\'(.+?)\'' for message in messages : if isinstance ( message , pyflakes . messages . UnusedImport ) : module_name = re . search ( pattern , str ( message ) ) module_name = module_name . group ( ) [ 1 : - 1 ] if module_name : yield ( message . lineno , module_name )

Yield line number and module name of unused imports .


def star_import_used_line_numbers(messages):
    """Yield line number of star import usage."""
    for message in messages:
        if isinstance(message, pyflakes.messages.ImportStarUsed):
            yield message.lineno

def star_import_used_line_numbers ( messages ) : for message in messages : if isinstance ( message , pyflakes . messages . ImportStarUsed ) : yield message . lineno

Yield line number of star import usage .


def star_import_usage_undefined_name(messages):
    """Yield line number, undefined name, and its possible origin module."""
    for message in messages:
        if isinstance(message, pyflakes.messages.ImportStarUsage):
            undefined_name = message.message_args[0]
            module_name = message.message_args[1]
            yield (message.lineno, undefined_name, module_name)

def star_import_usage_undefined_name ( messages ) : for message in messages : if isinstance ( message , pyflakes . messages . ImportStarUsage ) : undefined_name = message . message_args [ 0 ] module_name = message . message_args [ 1 ] yield ( message . lineno , undefined_name , module_name )

Yield line number undefined name and its possible origin module .


def unused_variable_line_numbers(messages):
    """Yield line numbers of unused variables."""
    for message in messages:
        if isinstance(message, pyflakes.messages.UnusedVariable):
            yield message.lineno

def unused_variable_line_numbers ( messages ) : for message in messages : if isinstance ( message , pyflakes . messages . UnusedVariable ) : yield message . lineno

Yield line numbers of unused variables .


def duplicate_key_line_numbers(messages, source):
    """Yield line numbers of duplicate keys."""
    messages = [
        message for message in messages
        if isinstance(message, pyflakes.messages.MultiValueRepeatedKeyLiteral)]

    if messages:
        # Filter out complex cases. We don't want to bother trying to parse
        # this stuff and get it right. We can do it on a key-by-key basis.

        key_to_messages = create_key_to_messages_dict(messages)

        lines = source.split('\n')

        for (key, messages) in key_to_messages.items():
            good = True
            for message in messages:
                line = lines[message.lineno - 1]
                key = message.message_args[0]

                if not dict_entry_has_key(line, key):
                    good = False

            if good:
                for message in messages:
                    yield message.lineno

def duplicate_key_line_numbers ( messages , source ) : messages = [ message for message in messages if isinstance ( message , pyflakes . messages . MultiValueRepeatedKeyLiteral ) ] if messages : key_to_messages = create_key_to_messages_dict ( messages ) lines = source . split ( '\n' ) for ( key , messages ) in key_to_messages . items ( ) : good = True for message in messages : line = lines [ message . lineno - 1 ] key = message . message_args [ 0 ] if not dict_entry_has_key ( line , key ) : good = False if good : for message in messages : yield message . lineno


def create_key_to_messages_dict(messages):
    """Return dict mapping the key to list of messages."""
    dictionary = collections.defaultdict(lambda: [])
    for message in messages:
        dictionary[message.message_args[0]].append(message)
    return dictionary

def create_key_to_messages_dict ( messages ) : dictionary = collections . defaultdict ( lambda : [ ] ) for message in messages : dictionary [ message . message_args [ 0 ] ] . append ( message ) return dictionary

Return dict mapping the key to list of messages .


def check(source):
    """Return messages from pyflakes."""
    if sys.version_info[0] == 2 and isinstance(source, unicode):
        # Convert back to original byte string encoding, otherwise pyflakes
        # call to compile() will complain. See PEP 263. This only affects
        # Python 2.
        try:
            source = source.encode('utf-8')
        except UnicodeError:  # pragma: no cover
            return []

    reporter = ListReporter()
    try:
        pyflakes.api.check(source, filename='<string>', reporter=reporter)
    except (AttributeError, RecursionError, UnicodeDecodeError):
        pass
    return reporter.messages

def check ( source ) : if sys . version_info [ 0 ] == 2 and isinstance ( source , unicode ) : try : source = source . encode ( 'utf-8' ) except UnicodeError : return [ ] reporter = ListReporter ( ) try : pyflakes . api . check ( source , filename = '<string>' , reporter = reporter ) except ( AttributeError , RecursionError , UnicodeDecodeError ) : pass return reporter . messages


def extract_package_name(line):
    """Return package name in import statement."""
    assert '\\' not in line
    assert '(' not in line
    assert ')' not in line
    assert ';' not in line

    if line.lstrip().startswith(('import', 'from')):
        word = line.split()[1]
    else:
        # Ignore doctests.
        return None

    package = word.split('.')[0]
    assert ' ' not in package

    return package

def
extract_package_name
(
line
)
:
assert
'\\'
not
in
line
assert
'('
not
in
line
assert
')'
not
in
line
assert
';'
not
in
line
if
line
.
lstrip
(
)
.
startswith
(
(
'import'
,
'from'
)
)
:
word
=
line
.
split
(
)
[
1
]
else
:
return
package
=
word
.
split
(
'.'
)
[
0
]
assert
' '
not
in
package
return
package

Return package name in import statement .


def multiline_import(line, previous_line=''):
    """Return True if import is spans multiples lines."""
    for symbol in '()':
        if symbol in line:
            return True

    # Ignore doctests.
    if line.lstrip().startswith('>'):
        return True

    return multiline_statement(line, previous_line)

def multiline_import ( line , previous_line = '' ) : for symbol in '()' : if symbol in line : return True if line . lstrip ( ) . startswith ( '>' ) : return True return multiline_statement ( line , previous_line )

Return True if import is spans multiples lines .


def multiline_statement(line, previous_line=''):
    """Return True if this is part of a multiline statement."""
    for symbol in '\\:;':
        if symbol in line:
            return True

    sio = io.StringIO(line)
    try:
        list(tokenize.generate_tokens(sio.readline))
        return previous_line.rstrip().endswith('\\')
    except (SyntaxError, tokenize.TokenError):
        return True

def multiline_statement ( line , previous_line = '' ) : for symbol in '\\:;' : if symbol in line : return True sio = io . StringIO ( line ) try : list ( tokenize . generate_tokens ( sio . readline ) ) return previous_line . rstrip ( ) . endswith ( '\\' ) except ( SyntaxError , tokenize . TokenError ) : return True

Return True if this is part of a multiline statement .


def filter_from_import(line, unused_module):
    """Parse and filter ``from something import a, b, c``.

    Return line without unused import modules, or `pass` if all of the
    module in import is unused.
    """
    (indentation, imports) = re.split(pattern=r'\bimport\b',
                                      string=line, maxsplit=1)
    base_module = re.search(pattern=r'\bfrom\s+([^ ]+)',
                            string=indentation).group(1)

    # Create an imported module list with base module name
    # ex ``from a import b, c as d`` -> ``['a.b', 'a.c as d']``
    imports = re.split(pattern=r',', string=imports.strip())
    imports = [base_module + '.' + x.strip() for x in imports]

    # We compare full module name (``a.module`` not `module`) to
    # guarantee the exact same module as detected from pyflakes.
    filtered_imports = [x.replace(base_module + '.', '')
                        for x in imports if x not in unused_module]

    # All of the import in this statement is unused
    if not filtered_imports:
        return get_indentation(line) + 'pass' + get_line_ending(line)

    indentation += 'import '

    return (
        indentation +
        ', '.join(sorted(filtered_imports)) +
        get_line_ending(line))

def
filter_from_import
(
line
,
unused_module
)
:
(
indentation
,
imports
)
=
re
.
split
(
pattern
=
r'\bimport\b'
,
string
=
line
,
maxsplit
=
1
)
base_module
=
re
.
search
(
pattern
=
r'\bfrom\s+([^ ]+)'
,
string
=
indentation
)
.
group
(
1
)
imports
=
re
.
split
(
pattern
=
r','
,
string
=
imports
.
strip
(
)
)
imports
=
[
base_module
+
'.'
+
x
.
strip
(
)
for
x
in
imports
]
filtered_imports
=
[
x
.
replace
(
base_module
+
'.'
,
''
)
for
x
in
imports
if
x
not
in
unused_module
]
if
not
filtered_imports
:
return
get_indentation
(
line
)
+
'pass'
+
get_line_ending
(
line
)
indentation
+=
'import '
return
(
indentation
+
', '
.
join
(
sorted
(
filtered_imports
)
)
+
get_line_ending
(
line
)
)

Parse and filter from something import a b c .


def break_up_import(line):
    """Return line with imports on separate lines."""
    assert '\\' not in line
    assert '(' not in line
    assert ')' not in line
    assert ';' not in line
    assert '#' not in line
    assert not line.lstrip().startswith('from')

    newline = get_line_ending(line)
    if not newline:
        return line

    (indentation, imports) = re.split(pattern=r'\bimport\b',
                                      string=line, maxsplit=1)

    indentation += 'import '
    assert newline

    return ''.join([indentation + i.strip() + newline
                    for i in sorted(imports.split(','))])

def
break_up_import
(
line
)
:
assert
'\\'
not
in
line
assert
'('
not
in
line
assert
')'
not
in
line
assert
';'
not
in
line
assert
'#'
not
in
line
assert
not
line
.
lstrip
(
)
.
startswith
(
'from'
)
newline
=
get_line_ending
(
line
)
if
not
newline
:
return
line
(
indentation
,
imports
)
=
re
.
split
(
pattern
=
r'\bimport\b'
,
string
=
line
,
maxsplit
=
1
)
indentation
+=
'import '
assert
newline
return
''
.
join
(
[
indentation
+
i
.
strip
(
)
+
newline
for
i
in
sorted
(
imports
.
split
(
','
)
)
]
)

Return line with imports on separate lines .


def filter_code(source, additional_imports=None,
                expand_star_imports=False,
                remove_all_unused_imports=False,
                remove_duplicate_keys=False,
                remove_unused_variables=False,
                ignore_init_module_imports=False,
                ):
    """Yield code with unused imports removed."""
    imports = SAFE_IMPORTS
    if additional_imports:
        imports |= frozenset(additional_imports)
    del additional_imports

    messages = check(source)

    if ignore_init_module_imports:
        marked_import_line_numbers = frozenset()
    else:
        marked_import_line_numbers = frozenset(
            unused_import_line_numbers(messages))
    marked_unused_module = collections.defaultdict(lambda: [])
    for line_number, module_name in unused_import_module_name(messages):
        marked_unused_module[line_number].append(module_name)

    if expand_star_imports and not (
        # See explanations in #18.
        re.search(r'\b__all__\b', source) or
        re.search(r'\bdel\b', source)
    ):
        marked_star_import_line_numbers = frozenset(
            star_import_used_line_numbers(messages))
        if len(marked_star_import_line_numbers) > 1:
            # Auto expanding only possible for single star import
            marked_star_import_line_numbers = frozenset()
        else:
            undefined_names = []
            for line_number, undefined_name, _ \
                    in star_import_usage_undefined_name(messages):
                undefined_names.append(undefined_name)
            if not undefined_names:
                marked_star_import_line_numbers = frozenset()
    else:
        marked_star_import_line_numbers = frozenset()

    if remove_unused_variables:
        marked_variable_line_numbers = frozenset(
            unused_variable_line_numbers(messages))
    else:
        marked_variable_line_numbers = frozenset()

    if remove_duplicate_keys:
        marked_key_line_numbers = frozenset(
            duplicate_key_line_numbers(messages, source))
    else:
        marked_key_line_numbers = frozenset()

    line_messages = get_messages_by_line(messages)

    sio = io.StringIO(source)
    previous_line = ''
    for line_number, line in enumerate(sio.readlines(), start=1):
        if '#' in line:
            yield line
        elif line_number in marked_import_line_numbers:
            yield filter_unused_import(
                line,
                unused_module=marked_unused_module[line_number],
                remove_all_unused_imports=remove_all_unused_imports,
                imports=imports,
                previous_line=previous_line)
        elif line_number in marked_variable_line_numbers:
            yield filter_unused_variable(line)
        elif line_number in marked_key_line_numbers:
            yield filter_duplicate_key(line, line_messages[line_number],
                                       line_number, marked_key_line_numbers,
                                       source)
        elif line_number in marked_star_import_line_numbers:
            yield filter_star_import(line, undefined_names)
        else:
            yield line

        previous_line = line

def filter_code ( source , additional_imports = None , expand_star_imports = False , remove_all_unused_imports = False , remove_duplicate_keys = False , remove_unused_variables = False , ignore_init_module_imports = False , ) : imports = SAFE_IMPORTS if additional_imports : imports |= frozenset ( additional_imports ) del additional_imports messages = check ( source ) if ignore_init_module_imports : marked_import_line_numbers = frozenset ( ) else : marked_import_line_numbers = frozenset ( unused_import_line_numbers ( messages ) ) marked_unused_module = collections . defaultdict ( lambda : [ ] ) for line_number , module_name in unused_import_module_name ( messages ) : marked_unused_module [ line_number ] . append ( module_name ) if expand_star_imports and not ( re . search ( r'\b__all__\b' , source ) or re . search ( r'\bdel\b' , source ) ) : marked_star_import_line_numbers = frozenset ( star_import_used_line_numbers ( messages ) ) if len ( marked_star_import_line_numbers ) > 1 : marked_star_import_line_numbers = frozenset ( ) else : undefined_names = [ ] for line_number , undefined_name , _ in star_import_usage_undefined_name ( messages ) : undefined_names . append ( undefined_name ) if not undefined_names : marked_star_import_line_numbers = frozenset ( ) else : marked_star_import_line_numbers = frozenset ( ) if remove_unused_variables : marked_variable_line_numbers = frozenset ( unused_variable_line_numbers ( messages ) ) else : marked_variable_line_numbers = frozenset ( ) if remove_duplicate_keys : marked_key_line_numbers = frozenset ( duplicate_key_line_numbers ( messages , source ) ) else : marked_key_line_numbers = frozenset ( ) line_messages = get_messages_by_line ( messages ) sio = io . StringIO ( source ) previous_line = '' for line_number , line in enumerate ( sio . readlines ( ) , start = 1 ) : if '#' in line : yield line elif line_number in marked_import_line_numbers : yield filter_unused_import ( line , unused_module = marked_unused_module [ line_number ] , remove_all_unused_imports = remove_all_unused_imports , imports = imports , previous_line = previous_line ) elif line_number in marked_variable_line_numbers : yield filter_unused_variable ( line ) elif line_number in marked_key_line_numbers : yield filter_duplicate_key ( line , line_messages [ line_number ] , line_number , marked_key_line_numbers , source ) elif line_number in marked_star_import_line_numbers : yield filter_star_import ( line , undefined_names ) else : yield line previous_line = line

Yield code with unused imports removed .


def get_messages_by_line(messages):
    """Return dictionary that maps line number to message."""
    line_messages = {}
    for message in messages:
        line_messages[message.lineno] = message
    return line_messages

def get_messages_by_line ( messages ) : line_messages = { } for message in messages : line_messages [ message . lineno ] = message return line_messages

Return dictionary that maps line number to message .


def filter_star_import(line, marked_star_import_undefined_name):
    """Return line with the star import expanded."""
    undefined_name = sorted(set(marked_star_import_undefined_name))
    return re.sub(r'\*', ', '.join(undefined_name), line)

def
filter_star_import
(
line
,
marked_star_import_undefined_name
)
:
undefined_name
=
sorted
(
set
(
marked_star_import_undefined_name
)
)
return
re
.
sub
(
r'\*'
,
', '
.
join
(
undefined_name
)
,
line
)

Return line with the star import expanded .


def filter_duplicate_key(line, message, line_number, marked_line_numbers,
                         source, previous_line=''):
    """Return '' if first occurrence of the key otherwise return `line`."""
    if marked_line_numbers and line_number == sorted(marked_line_numbers)[0]:
        return ''

    return line

def filter_duplicate_key ( line , message , line_number , marked_line_numbers , source , previous_line = '' ) : if marked_line_numbers and line_number == sorted ( marked_line_numbers ) [ 0 ] : return '' return line

Return if first occurrence of the key otherwise return line .


def dict_entry_has_key(line, key):
    """Return True if `line` is a dict entry that uses `key`.

    Return False for multiline cases where the line should not be removed by
    itself.

    """
    if '#' in line:
        return False

    result = re.match(r'\s*(.*)\s*:\s*(.*),\s*$', line)
    if not result:
        return False

    try:
        candidate_key = ast.literal_eval(result.group(1))
    except (SyntaxError, ValueError):
        return False

    if multiline_statement(result.group(2)):
        return False

    return candidate_key == key

def dict_entry_has_key ( line , key ) : if '#' in line : return False result = re . match ( r'\s*(.*)\s*:\s*(.*),\s*$' , line ) if not result : return False try : candidate_key = ast . literal_eval ( result . group ( 1 ) ) except ( SyntaxError , ValueError ) : return False if multiline_statement ( result . group ( 2 ) ) : return False return candidate_key == key

Return True if line is a dict entry that uses key .


def is_literal_or_name(value):
    """Return True if value is a literal or a name."""
    try:
        ast.literal_eval(value)
        return True
    except (SyntaxError, ValueError):
        pass

    if value.strip() in ['dict()', 'list()', 'set()']:
        return True

    # Support removal of variables on the right side. But make sure
    # there are no dots, which could mean an access of a property.
    return re.match(r'^\w+\s*$', value)

def is_literal_or_name ( value ) : try : ast . literal_eval ( value ) return True except ( SyntaxError , ValueError ) : pass if value . strip ( ) in [ 'dict()' , 'list()' , 'set()' ] : return True return re . match ( r'^\w+\s*$' , value )

Return True if value is a literal or a name .


def useless_pass_line_numbers(source):
    """Yield line numbers of unneeded "pass" statements."""
    sio = io.StringIO(source)
    previous_token_type = None
    last_pass_row = None
    last_pass_indentation = None
    previous_line = ''
    for token in tokenize.generate_tokens(sio.readline):
        token_type = token[0]
        start_row = token[2][0]
        line = token[4]

        is_pass = (token_type == tokenize.NAME and line.strip() == 'pass')

        # Leading "pass".
        if (start_row - 1 == last_pass_row and
                get_indentation(line) == last_pass_indentation and
                token_type in ATOMS and
                not is_pass):
            yield start_row - 1

        if is_pass:
            last_pass_row = start_row
            last_pass_indentation = get_indentation(line)

        # Trailing "pass".
        if (is_pass and
                previous_token_type != tokenize.INDENT and
                not previous_line.rstrip().endswith('\\')):
            yield start_row

        previous_token_type = token_type
        previous_line = line

def useless_pass_line_numbers ( source ) : sio = io . StringIO ( source ) previous_token_type = None last_pass_row = None last_pass_indentation = None previous_line = '' for token in tokenize . generate_tokens ( sio . readline ) : token_type = token [ 0 ] start_row = token [ 2 ] [ 0 ] line = token [ 4 ] is_pass = ( token_type == tokenize . NAME and line . strip ( ) == 'pass' ) if ( start_row - 1 == last_pass_row and get_indentation ( line ) == last_pass_indentation and token_type in ATOMS and not is_pass ) : yield start_row - 1 if is_pass : last_pass_row = start_row last_pass_indentation = get_indentation ( line ) if ( is_pass and previous_token_type != tokenize . INDENT and not previous_line . rstrip ( ) . endswith ( '\\' ) ) : yield start_row previous_token_type = token_type previous_line = line

Yield line numbers of unneeded pass statements .


def filter_useless_pass(source):
    """Yield code with useless "pass" lines removed."""
    try:
        marked_lines = frozenset(useless_pass_line_numbers(source))
    except (SyntaxError, tokenize.TokenError):
        marked_lines = frozenset()

    sio = io.StringIO(source)
    for line_number, line in enumerate(sio.readlines(), start=1):
        if line_number not in marked_lines:
            yield line

def filter_useless_pass ( source ) : try : marked_lines = frozenset ( useless_pass_line_numbers ( source ) ) except ( SyntaxError , tokenize . TokenError ) : marked_lines = frozenset ( ) sio = io . StringIO ( source ) for line_number , line in enumerate ( sio . readlines ( ) , start = 1 ) : if line_number not in marked_lines : yield line

Yield code with useless pass lines removed .


def get_indentation(line):
    """Return leading whitespace."""
    if line.strip():
        non_whitespace_index = len(line) - len(line.lstrip())
        return line[:non_whitespace_index]
    else:
        return ''

def get_indentation ( line ) : if line . strip ( ) : non_whitespace_index = len ( line ) - len ( line . lstrip ( ) ) return line [ : non_whitespace_index ] else : return ''


def get_line_ending(line):
    """Return line ending."""
    non_whitespace_index = len(line.rstrip()) - len(line)
    if not non_whitespace_index:
        return ''
    else:
        return line[non_whitespace_index:]

def get_line_ending ( line ) : non_whitespace_index = len ( line . rstrip ( ) ) - len ( line ) if not non_whitespace_index : return '' else : return line [ non_whitespace_index : ]


def fix_code(source, additional_imports=None, expand_star_imports=False,
             remove_all_unused_imports=False, remove_duplicate_keys=False,
             remove_unused_variables=False, ignore_init_module_imports=False):
    """Return code with all filtering run on it."""
    if not source:
        return source

    # pyflakes does not handle "nonlocal" correctly.
    if 'nonlocal' in source:
        remove_unused_variables = False

    filtered_source = None
    while True:
        filtered_source = ''.join(
            filter_useless_pass(''.join(
                filter_code(
                    source,
                    additional_imports=additional_imports,
                    expand_star_imports=expand_star_imports,
                    remove_all_unused_imports=remove_all_unused_imports,
                    remove_duplicate_keys=remove_duplicate_keys,
                    remove_unused_variables=remove_unused_variables,
                    ignore_init_module_imports=ignore_init_module_imports,
                ))))

        if filtered_source == source:
            break
        source = filtered_source

    return filtered_source

def fix_code ( source , additional_imports = None , expand_star_imports = False , remove_all_unused_imports = False , remove_duplicate_keys = False , remove_unused_variables = False , ignore_init_module_imports = False ) : if not source : return source if 'nonlocal' in source : remove_unused_variables = False filtered_source = None while True : filtered_source = '' . join ( filter_useless_pass ( '' . join ( filter_code ( source , additional_imports = additional_imports , expand_star_imports = expand_star_imports , remove_all_unused_imports = remove_all_unused_imports , remove_duplicate_keys = remove_duplicate_keys , remove_unused_variables = remove_unused_variables , ignore_init_module_imports = ignore_init_module_imports , ) ) ) ) if filtered_source == source : break source = filtered_source return filtered_source

Return code with all filtering run on it .


def _split_comma_separated(string):
    """Return a set of strings."""
    return set(text.strip() for text in string.split(',') if text.strip())

def _split_comma_separated ( string ) : return set ( text . strip ( ) for text in string . split ( ',' ) if text . strip ( ) )


def is_python_file(filename):
    """Return True if filename is Python file."""
    if filename.endswith('.py'):
        return True

    try:
        with open_with_encoding(
                filename,
                None,
                limit_byte_check=MAX_PYTHON_FILE_DETECTION_BYTES) as f:
            text = f.read(MAX_PYTHON_FILE_DETECTION_BYTES)
            if not text:
                return False
            first_line = text.splitlines()[0]
    except (IOError, IndexError):
        return False

    if not PYTHON_SHEBANG_REGEX.match(first_line):
        return False

    return True

def is_python_file ( filename ) : if filename . endswith ( '.py' ) : return True try : with open_with_encoding ( filename , None , limit_byte_check = MAX_PYTHON_FILE_DETECTION_BYTES ) as f : text = f . read ( MAX_PYTHON_FILE_DETECTION_BYTES ) if not text : return False first_line = text . splitlines ( ) [ 0 ] except ( IOError , IndexError ) : return False if not PYTHON_SHEBANG_REGEX . match ( first_line ) : return False return True

Return True if filename is Python file .


def is_exclude_file(filename, exclude):
    """Return True if file matches exclude pattern."""
    base_name = os.path.basename(filename)

    if base_name.startswith('.'):
        return True

    for pattern in exclude:
        if fnmatch.fnmatch(base_name, pattern):
            return True
        if fnmatch.fnmatch(filename, pattern):
            return True
    return False

def is_exclude_file ( filename , exclude ) : base_name = os . path . basename ( filename ) if base_name . startswith ( '.' ) : return True for pattern in exclude : if fnmatch . fnmatch ( base_name , pattern ) : return True if fnmatch . fnmatch ( filename , pattern ) : return True return False

Return True if file matches exclude pattern .


def find_files(filenames, recursive, exclude):
    """Yield filenames."""
    while filenames:
        name = filenames.pop(0)
        if recursive and os.path.isdir(name):
            for root, directories, children in os.walk(name):
                filenames += [os.path.join(root, f) for f in children
                              if match_file(os.path.join(root, f),
                                            exclude)]
                directories[:] = [d for d in directories
                                  if match_file(os.path.join(root, d),
                                                exclude)]
        else:
            if not is_exclude_file(name, exclude):
                yield name

def find_files ( filenames , recursive , exclude ) : while filenames : name = filenames . pop ( 0 ) if recursive and os . path . isdir ( name ) : for root , directories , children in os . walk ( name ) : filenames += [ os . path . join ( root , f ) for f in children if match_file ( os . path . join ( root , f ) , exclude ) ] directories [ : ] = [ d for d in directories if match_file ( os . path . join ( root , d ) , exclude ) ] else : if not is_exclude_file ( name , exclude ) : yield name


def _main(argv, standard_out, standard_error):
    """Return exit status.

    0 means no error.
    """
    import argparse
    parser = argparse.ArgumentParser(description=__doc__, prog='autoflake')
    parser.add_argument('-c', '--check', action='store_true',
                        help='return error code if changes are needed')
    parser.add_argument('-i', '--in-place', action='store_true',
                        help='make changes to files instead of printing diffs')
    parser.add_argument('-r', '--recursive', action='store_true',
                        help='drill down directories recursively')
    parser.add_argument('--exclude', metavar='globs',
                        help='exclude file/directory names that match these '
                             'comma-separated globs')
    parser.add_argument('--imports',
                        help='by default, only unused standard library '
                             'imports are removed; specify a comma-separated '
                             'list of additional modules/packages')
    parser.add_argument('--expand-star-imports', action='store_true',
                        help='expand wildcard star imports with undefined '
                             'names; this only triggers if there is only '
                             'one star import in the file; this is skipped if '
                             'there are any uses of `__all__` or `del` in the '
                             'file')
    parser.add_argument('--remove-all-unused-imports', action='store_true',
                        help='remove all unused imports (not just those from '
                             'the standard library)')
    parser.add_argument('--ignore-init-module-imports', action='store_true',
                        help='exclude __init__.py when removing unused '
                             'imports')
    parser.add_argument('--remove-duplicate-keys', action='store_true',
                        help='remove all duplicate keys in objects')
    parser.add_argument('--remove-unused-variables', action='store_true',
                        help='remove unused variables')
    parser.add_argument('--version', action='version',
                        version='%(prog)s ' + __version__)
    parser.add_argument('files', nargs='+', help='files to format')

    args = parser.parse_args(argv[1:])

    if args.remove_all_unused_imports and args.imports:
        print('Using both --remove-all and --imports is redundant',
              file=standard_error)
        return 1

    if args.exclude:
        args.exclude = _split_comma_separated(args.exclude)
    else:
        args.exclude = set([])

    filenames = list(set(args.files))
    failure = False
    for name in find_files(filenames, args.recursive, args.exclude):
        try:
            fix_file(name, args=args, standard_out=standard_out)
        except IOError as exception:
            print(unicode(exception), file=standard_error)
            failure = True

    return 1 if failure else 0

Return exit status.

    0 means no error.


def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the ObtainLease response payload and decode it
        into its constituent parts.

        Args:
            input_stream (stream): A data stream containing encoded object
                data, supporting a read method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            ValueError: Raised if the data attribute is missing from the
                encoded payload.
        """
        super(ObtainLeaseResponsePayload, self).read(
            input_stream,
            kmip_version=kmip_version
        )
        local_stream = utils.BytearrayStream(input_stream.read(self.length))

        if self.is_tag_next(enums.Tags.UNIQUE_IDENTIFIER, local_stream):
            self._unique_identifier = primitives.TextString(
                tag=enums.Tags.UNIQUE_IDENTIFIER
            )
            self._unique_identifier.read(
                local_stream,
                kmip_version=kmip_version
            )
        if self.is_tag_next(enums.Tags.LEASE_TIME, local_stream):
            self._lease_time = primitives.Interval(
                tag=enums.Tags.LEASE_TIME
            )
            self._lease_time.read(local_stream, kmip_version=kmip_version)
        if self.is_tag_next(enums.Tags.LAST_CHANGE_DATE, local_stream):
            self._last_change_date = primitives.DateTime(
                tag=enums.Tags.LAST_CHANGE_DATE
            )
            self._last_change_date.read(
                local_stream,
                kmip_version=kmip_version
            )

        self.is_oversized(local_stream)

def read ( self , input_stream , kmip_version = enums . KMIPVersion . KMIP_1_0 ) : super ( ObtainLeaseResponsePayload , self ) . read ( input_stream , kmip_version = kmip_version ) local_stream = utils . BytearrayStream ( input_stream . read ( self . length ) ) if self . is_tag_next ( enums . Tags . UNIQUE_IDENTIFIER , local_stream ) : self . _unique_identifier = primitives . TextString ( tag = enums . Tags . UNIQUE_IDENTIFIER ) self . _unique_identifier . read ( local_stream , kmip_version = kmip_version ) if self . is_tag_next ( enums . Tags . LEASE_TIME , local_stream ) : self . _lease_time = primitives . Interval ( tag = enums . Tags . LEASE_TIME ) self . _lease_time . read ( local_stream , kmip_version = kmip_version ) if self . is_tag_next ( enums . Tags . LAST_CHANGE_DATE , local_stream ) : self . _last_change_date = primitives . DateTime ( tag = enums . Tags . LAST_CHANGE_DATE ) self . _last_change_date . read ( local_stream , kmip_version = kmip_version ) self . is_oversized ( local_stream )

Read the data encoding the ObtainLease response payload and decode it into its constituent parts .


def write(self, output_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the ObtainLease response payload to a stream.

        Args:
            output_stream (stream): A data stream in which to encode object
                data, supporting a write method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            ValueError: Raised if the data attribute is not defined.
        """
        local_stream = utils.BytearrayStream()

        if self._unique_identifier:
            self._unique_identifier.write(
                local_stream,
                kmip_version=kmip_version
            )
        if self._lease_time:
            self._lease_time.write(
                local_stream,
                kmip_version=kmip_version
            )
        if self._last_change_date:
            self._last_change_date.write(
                local_stream,
                kmip_version=kmip_version
            )

        self.length = local_stream.length()
        super(ObtainLeaseResponsePayload, self).write(
            output_stream,
            kmip_version=kmip_version
        )
        output_stream.write(local_stream.buffer)

def write ( self , output_stream , kmip_version = enums . KMIPVersion . KMIP_1_0 ) : local_stream = utils . BytearrayStream ( ) if self . _unique_identifier : self . _unique_identifier . write ( local_stream , kmip_version = kmip_version ) if self . _lease_time : self . _lease_time . write ( local_stream , kmip_version = kmip_version ) if self . _last_change_date : self . _last_change_date . write ( local_stream , kmip_version = kmip_version ) self . length = local_stream . length ( ) super ( ObtainLeaseResponsePayload , self ) . write ( output_stream , kmip_version = kmip_version ) output_stream . write ( local_stream . buffer )

Write the data encoding the ObtainLease response payload to a stream .


def write(self, output_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the Cancel request payload to a stream.

        Args:
            output_stream (stream): A data stream in which to encode object
                data, supporting a write method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            ValueError: Raised if the data attribute is not defined.
        """
        local_stream = utils.BytearrayStream()

        if self._asynchronous_correlation_value:
            self._asynchronous_correlation_value.write(
                local_stream,
                kmip_version=kmip_version
            )

        self.length = local_stream.length()
        super(CancelRequestPayload, self).write(
            output_stream,
            kmip_version=kmip_version
        )
        output_stream.write(local_stream.buffer)

def write ( self , output_stream , kmip_version = enums . KMIPVersion . KMIP_1_0 ) : local_stream = utils . BytearrayStream ( ) if self . _asynchronous_correlation_value : self . _asynchronous_correlation_value . write ( local_stream , kmip_version = kmip_version ) self . length = local_stream . length ( ) super ( CancelRequestPayload , self ) . write ( output_stream , kmip_version = kmip_version ) output_stream . write ( local_stream . buffer )

Write the data encoding the Cancel request payload to a stream .


def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the Cancel response payload and decode it into
        its constituent parts.

        Args:
            input_stream (stream): A data stream containing encoded object
                data, supporting a read method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.

        Raises:
            ValueError: Raised if the data attribute is missing from the
                encoded payload.
        """
        super(CancelResponsePayload, self).read(
            input_stream,
            kmip_version=kmip_version
        )
        local_stream = utils.BytearrayStream(input_stream.read(self.length))

        if self.is_tag_next(
                enums.Tags.ASYNCHRONOUS_CORRELATION_VALUE,
                local_stream
        ):
            self._asynchronous_correlation_value = primitives.ByteString(
                tag=enums.Tags.ASYNCHRONOUS_CORRELATION_VALUE
            )
            self._asynchronous_correlation_value.read(
                local_stream,
                kmip_version=kmip_version
            )
        if self.is_tag_next(enums.Tags.CANCELLATION_RESULT, local_stream):
            self._cancellation_result = primitives.Enumeration(
                enums.CancellationResult,
                tag=enums.Tags.CANCELLATION_RESULT
            )
            self._cancellation_result.read(
                local_stream,
                kmip_version=kmip_version
            )

        self.is_oversized(local_stream)

def read ( self , input_stream , kmip_version = enums . KMIPVersion . KMIP_1_0 ) : super ( CancelResponsePayload , self ) . read ( input_stream , kmip_version = kmip_version ) local_stream = utils . BytearrayStream ( input_stream . read ( self . length ) ) if self . is_tag_next ( enums . Tags . ASYNCHRONOUS_CORRELATION_VALUE , local_stream ) : self . _asynchronous_correlation_value = primitives . ByteString ( tag = enums . Tags . ASYNCHRONOUS_CORRELATION_VALUE ) self . _asynchronous_correlation_value . read ( local_stream , kmip_version = kmip_version ) if self . is_tag_next ( enums . Tags . CANCELLATION_RESULT , local_stream ) : self . _cancellation_result = primitives . Enumeration ( enums . CancellationResult , tag = enums . Tags . CANCELLATION_RESULT ) self . _cancellation_result . read ( local_stream , kmip_version = kmip_version ) self . is_oversized ( local_stream )

Read the data encoding the Cancel response payload and decode it into its constituent parts .


def create(cls, name_value, name_type):
        '''
            Returns a Name object, populated with the given value and type
        '''
        if isinstance(name_value, Name.NameValue):
            value = name_value
        elif isinstance(name_value, str):
            value = cls.NameValue(name_value)
        else:
            name = 'Name'
            msg = exceptions.ErrorStrings.BAD_EXP_RECV
            member = 'name_value'
            raise TypeError(msg.format('{0}.{1}'.format(name, member),
                                       'name_value', type(Name.NameValue),
                                       type(name_value)))

        if isinstance(name_type, Name.NameType):
            n_type = name_type
        elif isinstance(name_type, Enum):
            n_type = cls.NameType(name_type)
        else:
            name = 'Name'
            msg = exceptions.ErrorStrings.BAD_EXP_RECV
            member = 'name_type'
            raise TypeError(msg.format('{0}.{1}'.format(name, member),
                                       'name_type', type(Name.NameType),
                                       type(name_type)))

        return Name(name_value=value,
                    name_type=n_type)

def create ( cls , name_value , name_type ) : if isinstance ( name_value , Name . NameValue ) : value = name_value elif isinstance ( name_value , str ) : value = cls . NameValue ( name_value ) else : name = 'Name' msg = exceptions . ErrorStrings . BAD_EXP_RECV member = 'name_value' raise TypeError ( msg . format ( '{0}.{1}' . format ( name , member ) , 'name_value' , type ( Name . NameValue ) , type ( name_value ) ) ) if isinstance ( name_type , Name . NameType ) : n_type = name_type elif isinstance ( name_type , Enum ) : n_type = cls . NameType ( name_type ) else : name = 'Name' msg = exceptions . ErrorStrings . BAD_EXP_RECV member = 'name_type' raise TypeError ( msg . format ( '{0}.{1}' . format ( name , member ) , 'name_type' , type ( Name . NameType ) , type ( name_type ) ) ) return Name ( name_value = value , name_type = n_type )

Returns a Name object populated with the given value and type


def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the Digest object and decode it into its
        constituent parts.

        Args:
            istream (Stream): A data stream containing encoded object data,
                supporting a read method; usually a BytearrayStream object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.
        """
        super(Digest, self).read(istream, kmip_version=kmip_version)
        tstream = BytearrayStream(istream.read(self.length))

        self.hashing_algorithm.read(tstream, kmip_version=kmip_version)
        self.digest_value.read(tstream, kmip_version=kmip_version)
        self.key_format_type.read(tstream, kmip_version=kmip_version)

        self.is_oversized(tstream)
        self.validate()

def read ( self , istream , kmip_version = enums . KMIPVersion . KMIP_1_0 ) : super ( Digest , self ) . read ( istream , kmip_version = kmip_version ) tstream = BytearrayStream ( istream . read ( self . length ) ) self . hashing_algorithm . read ( tstream , kmip_version = kmip_version ) self . digest_value . read ( tstream , kmip_version = kmip_version ) self . key_format_type . read ( tstream , kmip_version = kmip_version ) self . is_oversized ( tstream ) self . validate ( )

Read the data encoding the Digest object and decode it into its constituent parts .


def write(self, ostream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the Digest object to a stream.

        Args:
            ostream (Stream): A data stream in which to encode object data,
                supporting a write method; usually a BytearrayStream object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.
        """
        tstream = BytearrayStream()

        self.hashing_algorithm.write(tstream, kmip_version=kmip_version)
        self.digest_value.write(tstream, kmip_version=kmip_version)
        self.key_format_type.write(tstream, kmip_version=kmip_version)

        self.length = tstream.length()
        super(Digest, self).write(ostream, kmip_version=kmip_version)
        ostream.write(tstream.buffer)

def write ( self , ostream , kmip_version = enums . KMIPVersion . KMIP_1_0 ) : tstream = BytearrayStream ( ) self . hashing_algorithm . write ( tstream , kmip_version = kmip_version ) self . digest_value . write ( tstream , kmip_version = kmip_version ) self . key_format_type . write ( tstream , kmip_version = kmip_version ) self . length = tstream . length ( ) super ( Digest , self ) . write ( ostream , kmip_version = kmip_version ) ostream . write ( tstream . buffer )

Write the data encoding the Digest object to a stream .


def create(cls,
               hashing_algorithm=HashingAlgorithmEnum.SHA_256,
               digest_value=b'',
               key_format_type=KeyFormatTypeEnum.RAW):
        """
        Construct a Digest object from provided digest values.

        Args:
            hashing_algorithm (HashingAlgorithm): An enumeration representing
                the hash algorithm used to compute the digest. Optional,
                defaults to HashingAlgorithm.SHA_256.
            digest_value (byte string): The bytes of the digest hash. Optional,
                defaults to the empty byte string.
            key_format_type (KeyFormatType): An enumeration representing the
                format of the key corresponding to the digest. Optional,
                defaults to KeyFormatType.RAW.

        Returns:
            Digest: The newly created Digest.

        Example:
            >>> x = Digest.create(HashingAlgorithm.MD5, b'\x00',
            ... KeyFormatType.RAW)
            >>> x.hashing_algorithm
            HashingAlgorithm(value=HashingAlgorithm.MD5)
            >>> x.digest_value
            DigestValue(value=bytearray(b'\x00'))
            >>> x.key_format_type
            KeyFormatType(value=KeyFormatType.RAW)
        """
        algorithm = HashingAlgorithm(hashing_algorithm)
        value = DigestValue(bytearray(digest_value))
        format_type = KeyFormatType(key_format_type)

        return Digest(hashing_algorithm=algorithm,
                      digest_value=value,
                      key_format_type=format_type)

def create ( cls , hashing_algorithm = HashingAlgorithmEnum . SHA_256 , digest_value = b'' , key_format_type = KeyFormatTypeEnum . RAW ) : algorithm = HashingAlgorithm ( hashing_algorithm ) value = DigestValue ( bytearray ( digest_value ) ) format_type = KeyFormatType ( key_format_type ) return Digest ( hashing_algorithm = algorithm , digest_value = value , key_format_type = format_type )

Construct a Digest object from provided digest values .


def read(self, istream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the ApplicationSpecificInformation object and
        decode it into its constituent parts.

        Args:
            istream (Stream): A data stream containing encoded object data,
                supporting a read method; usually a BytearrayStream object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.
        """
        super(ApplicationSpecificInformation, self).read(
            istream,
            kmip_version=kmip_version
        )
        tstream = BytearrayStream(istream.read(self.length))

        self.application_namespace.read(tstream, kmip_version=kmip_version)
        self.application_data.read(tstream, kmip_version=kmip_version)

        self.is_oversized(tstream)
        self.validate()

def read ( self , istream , kmip_version = enums . KMIPVersion . KMIP_1_0 ) : super ( ApplicationSpecificInformation , self ) . read ( istream , kmip_version = kmip_version ) tstream = BytearrayStream ( istream . read ( self . length ) ) self . application_namespace . read ( tstream , kmip_version = kmip_version ) self . application_data . read ( tstream , kmip_version = kmip_version ) self . is_oversized ( tstream ) self . validate ( )

Read the data encoding the ApplicationSpecificInformation object and decode it into its constituent parts .


def write(self, ostream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Write the data encoding the ApplicationSpecificInformation object to a
        stream.

        Args:
            ostream (Stream): A data stream in which to encode object data,
                supporting a write method; usually a BytearrayStream object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be encoded. Optional,
                defaults to KMIP 1.0.
        """
        tstream = BytearrayStream()

        self.application_namespace.write(tstream, kmip_version=kmip_version)
        self.application_data.write(tstream, kmip_version=kmip_version)

        self.length = tstream.length()
        super(ApplicationSpecificInformation, self).write(
            ostream,
            kmip_version=kmip_version
        )
        ostream.write(tstream.buffer)

def write ( self , ostream , kmip_version = enums . KMIPVersion . KMIP_1_0 ) : tstream = BytearrayStream ( ) self . application_namespace . write ( tstream , kmip_version = kmip_version ) self . application_data . write ( tstream , kmip_version = kmip_version ) self . length = tstream . length ( ) super ( ApplicationSpecificInformation , self ) . write ( ostream , kmip_version = kmip_version ) ostream . write ( tstream . buffer )

Write the data encoding the ApplicationSpecificInformation object to a stream .


def create(cls, application_namespace, application_data):
        """
        Construct an ApplicationSpecificInformation object from provided data
        and namespace values.

        Args:
            application_namespace (str): The name of the application namespace.
            application_data (str): Application data related to the namespace.

        Returns:
            ApplicationSpecificInformation: The newly created set of
                application information.

        Example:
            >>> x = ApplicationSpecificInformation.create('namespace', 'data')
            >>> x.application_namespace.value
            'namespace'
            >>> x.application_data.value
            'data'
        """
        namespace = ApplicationNamespace(application_namespace)
        data = ApplicationData(application_data)
        return ApplicationSpecificInformation(
            application_namespace=namespace, application_data=data)

def create ( cls , application_namespace , application_data ) : namespace = ApplicationNamespace ( application_namespace ) data = ApplicationData ( application_data ) return ApplicationSpecificInformation ( application_namespace = namespace , application_data = data )

Construct an ApplicationSpecificInformation object from provided data and namespace values .


def read(self, input_stream, kmip_version=enums.KMIPVersion.KMIP_1_0):
        """
        Read the data encoding the DerivationParameters struct and decode it
        into its constituent parts.

        Args:
            input_stream (stream): A data stream containing encoded object
                data, supporting a read method; usually a BytearrayStream
                object.
            kmip_version (KMIPVersion): An enumeration defining the KMIP
                version with which the object will be decoded. Optional,
                defaults to KMIP 1.0.
        """
        super(DerivationParameters, self).read(
            input_stream,
            kmip_version=kmip_version
        )
        local_stream = BytearrayStream(input_stream.read(self.length))

        if self.is_tag_next(
                enums.Tags.CRYPTOGRAPHIC_PARAMETERS,
                local_stream
        ):
            self._cryptographic_parameters = CryptographicParameters()
            self._cryptographic_parameters.read(
                local_stream,
                kmip_version=kmip_version
            )

        if self.is_tag_next(enums.Tags.INITIALIZATION_VECTOR, local_stream):
            self._initialization_vector = ByteString(
                tag=enums.Tags.INITIALIZATION_VECTOR
            )
            self._initialization_vector.read(
                local_stream,
                kmip_version=kmip_version
            )

        if self.is_tag_next(enums.Tags.DERIVATION_DATA, local_stream):
            self._derivation_data = ByteString(tag=enums.Tags.DERIVATION_DATA)
            self._derivation_data.read(local_stream, kmip_version=kmip_version)

        if self.is_tag_next(enums.Tags.SALT, local_stream):
            self._salt = ByteString(tag=enums.Tags.SALT)
            self._salt.read(local_stream, kmip_version=kmip_version)

        if self.is_tag_next(Tags.ITERATION_COUNT, local_stream):
            self._iteration_count = Integer(tag=Tags.ITERATION_COUNT)
            self._iteration_count.read(local_stream, kmip_version=kmip_version)

        self.is_oversized(local_stream)

def read ( self , input_stream , kmip_version = enums . KMIPVersion . KMIP_1_0 ) : super ( DerivationParameters , self ) . read ( input_stream , kmip_version = kmip_version ) local_stream = BytearrayStream ( input_stream . read ( self . length ) ) if self . is_tag_next ( enums . Tags . CRYPTOGRAPHIC_PARAMETERS , local_stream ) : self . _cryptographic_parameters = CryptographicParameters ( ) self . _cryptographic_parameters . read ( local_stream , kmip_version = kmip_version ) if self . is_tag_next ( enums . Tags . INITIALIZATION_VECTOR , local_stream ) : self . _initialization_vector = ByteString ( tag = enums . Tags . INITIALIZATION_VECTOR ) self . _initialization_vector . read ( local_stream , kmip_version = kmip_version ) if self . is_tag_next ( enums . Tags . DERIVATION_DATA , local_stream ) : self . _derivation_data = ByteString ( tag = enums . Tags . DERIVATION_DATA ) self . _derivation_data . read ( local_stream , kmip_version = kmip_version ) if self . is_tag_next ( enums . Tags . SALT , local_stream ) : self . _salt = ByteString ( tag = enums . Tags . SALT ) self . _salt . read ( local_stream , kmip_version = kmip_version ) if self . is_tag_next ( Tags . ITERATION_COUNT , local_stream ) : self . _iteration_count = Integer ( tag = Tags . ITERATION_COUNT ) self . _iteration_count . read ( local_stream , kmip_version = kmip_version ) self . is_oversized ( local_stream )

Read the data encoding the DerivationParameters struct and decode it into its constituent parts .