コード例 #1
0
ファイル: cidr2subject.py プロジェクト: spirillen/PyFunceble
class CIDR2Subject(ConverterBase):
    """
    Converts/Extracts the subjects of from the given CIDR.
    """

    ip_syntax_checker: Optional[IPSyntaxChecker] = None

    def __init__(self, data_to_convert: Optional[Any] = None) -> None:
        super().__init__(data_to_convert=data_to_convert)

        self.ip_syntax_checker = IPSyntaxChecker()

    @ConverterBase.data_to_convert.setter
    def data_to_convert(self, value: Any) -> None:
        """
        Overrites the default behavior.

        :raise TypeError:
            When the given data to convert is not :py:class:`str`
        """

        if not isinstance(value, str):
            raise TypeError(f"<value> should be {str}, {type(value)} given.")

        # pylint: disable=no-member
        super(CIDR2Subject, self.__class__).data_to_convert.fset(self, value)

    def get_converted(self) -> List[str]:
        """
        Provides the subject-s to test.
        """

        result = set()

        subject = self.data_to_convert.strip()

        if subject:
            try:
                self.ip_syntax_checker.set_subject(subject)
                if self.ip_syntax_checker.is_valid_v4_range():
                    result.update(
                        str(x) for x in IPv4Network(self.ip_syntax_checker.subject)
                    )
                elif self.ip_syntax_checker.is_valid_v6_range():
                    ...

                    # Not Implemented yet.
                    result.add(subject)
                else:
                    result.add(subject)
            except ValueError:
                result.add(subject)

        return list(result)
コード例 #2
0
    def test_is_valid_v4(self) -> None:
        """
        Tests the method which let us check if the given subject is valid.
        """

        ip_checker = IPSyntaxChecker()

        expected = True

        for subject in pyf_test_dataset.VALID_IPV4:
            actual = ip_checker.set_subject(subject).is_valid()

            self.assertEqual(expected, actual, subject)