Esempio n. 1
0
 def test_camelcase_to_dash(self):
     self.assertEqual(camelcase_to_dash('CamelCase'), 'camel-case')
     self.assertEqual(camelcase_to_dash('CamelCASE'), 'camel-case')
     self.assertEqual(camelcase_to_dash('CAMELCase'), 'camel-case')
     self.assertEqual(camelcase_to_dash('MyCAMELCase'), 'my-camel-case')
     self.assertEqual(camelcase_to_dash('Camel123Case'), 'camel123-case')
     self.assertEqual(camelcase_to_dash('CAMEL123Case'), 'camel123-case')
     self.assertEqual(camelcase_to_dash('Camel-Case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('camel-case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('Camel_Case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('camel_case'), 'camel-case')
Esempio n. 2
0
    def from_config(cls, section: configparser.SectionProxy, option_handler_id: str = None) -> OptionHandler:
        """
        Create a handler of this class based on the configuration in the config section.

        :param section: The configuration section
        :param option_handler_id: Optional extra identifier
        :return: A handler object
        :rtype: OptionHandler
        """
        sub_options = []

        for name, value in section.items():
            # Strip numbers from the end, this can be used to supply the same option multiple times
            name = name.rstrip('0123456789-')

            if '-' in name or '_' in name:
                suboption_name = name.replace('_', '-').lower()
            else:
                suboption_name = camelcase_to_dash(name)

            suboption = name_registry.get(suboption_name)
            if not suboption:
                raise configparser.ParsingError("Unknown suboption: {}".format(suboption_name))

            for suboption_value in re.split('[,\t ]+', value):
                if not suboption_value:
                    raise configparser.ParsingError("{} option has no value".format(name))

                sub_options.append(suboption.from_string(suboption_value))

        return cls(sub_options)
Esempio n. 3
0
    def get_name(self, item: object) -> str:
        """
        Get the name for the by_name mapping.

        :param item: The item to determine the name of
        :return: The name to use as key in the mapping
        """
        return camelcase_to_dash(item.__name__)
Esempio n. 4
0
    def get_name(self, item: object) -> str:
        """
        Get the name for the by_name mapping.

        :param item: The item to determine the name of
        :return: The name to use as key in the mapping
        """
        return camelcase_to_dash(item.__name__)
Esempio n. 5
0
    def filter_description(self) -> str:
        """
        A short description of this filter for log messages.

        :return: The description
        """
        simple_name = camelcase_to_dash(self.__class__.__name__)
        if simple_name.endswith('-filter'):
            simple_name = simple_name[:-7]

        return "{} in {}".format(simple_name, [str(prefix) for prefix in self.filter_condition])
Esempio n. 6
0
    def filter_description(self) -> str:
        """
        A short description of this filter for log messages.

        :return: The description
        """
        simple_name = camelcase_to_dash(self.__class__.__name__)
        if simple_name.endswith('-filter'):
            simple_name = simple_name[:-7]

        return "{}={}".format(simple_name, self.filter_condition)
Esempio n. 7
0
    def filter_description(self) -> str:
        """
        A short description of this filter for log messages.

        :return: The description
        """
        simple_name = camelcase_to_dash(self.__class__.__name__)
        if simple_name.endswith('-filter'):
            simple_name = simple_name[:-7]

        return simple_name + ' ' + ' and '.join(["{operator} {value}".format(operator=condition.operator.__name__,
                                                                             value=condition.limit)
                                                 for condition in self.filter_condition])
Esempio n. 8
0
def message_type(value: str) -> Type[Message]:
    """
    Parse the value as the name of a DHCPv6 message type

    :param value: The name of the message type
    :return: The message class
    """
    from dhcpkit.ipv6.message_registry import message_registry

    # Prepare the value
    search_value = camelcase_to_dash(value)

    try:
        return message_registry.by_name[search_value]
    except KeyError:
        raise ValueError("{} is not a valid message type".format(value))
def message_type(value: str) -> Type[Message]:
    """
    Parse the value as the name of a DHCPv6 message type

    :param value: The name of the message type
    :return: The message class
    """
    from dhcpkit.ipv6.message_registry import message_registry

    # Prepare the value
    search_value = camelcase_to_dash(value)

    try:
        return message_registry.by_name[search_value]
    except KeyError:
        raise ValueError("{} is not a valid message type".format(value))
Esempio n. 10
0
    def normalise_section_name(section: str) -> str:
        """
        Normalise a section name.

        :param section: The raw name of the section
        :returns: The normalised name
        """
        # Collapse multiple spaces
        section = re.sub(r'[\t ]+', ' ', section)

        # Split
        parts = section.split(' ')
        parts[0] = parts[0].lower()

        # Special section names
        if parts[0] == 'interface':
            # Check name structure
            if len(parts) != 2:
                raise configparser.ParsingError("Interface sections must be named [interface xyz] "
                                                "where 'xyz' is an interface name")

        elif parts[0] == 'option':
            # Check name structure
            if not (2 <= len(parts) <= 3):
                raise configparser.ParsingError("Option sections must be named [option xyz] or [option xyz id]"
                                                "where 'xyz' is an option handler name and 'id' is an identifier "
                                                "to distinguish between multiple handlers of the same type")

            if '-' in parts[1] or '_' in parts[1]:
                parts[1] = parts[1].replace('_', '-').lower()
            else:
                parts[1] = camelcase_to_dash(parts[1])

            # If the name ends with
            if parts[1].endswith('-option-handler'):
                parts[1] = parts[1][:-15]

        elif parts[0] not in ('logging', 'server',):
            raise configparser.ParsingError("Invalid section name: [{}]".format(section))

        # Reconstruct
        return ' '.join(parts)
Esempio n. 11
0
def register(subclass: type):
    """
    Register a new option type in the option registry.

    :param subclass: A subclass of Option that implements the option
    """
    if not issubclass(subclass, NTPSubOption):
        raise TypeError('Only NTPSubOptions can be registered')

    # Store based on number
    # noinspection PyUnresolvedReferences
    registry[subclass.suboption_type] = subclass

    # Store based on name
    name = subclass.__name__
    if name.startswith('NTP'):
        name = name[3:]
    if name.endswith('SubOption'):
        name = name[:-9]
    name = camelcase_to_dash(name)
    name_registry[name] = subclass
Esempio n. 12
0
 def test_camelcase_to_dash(self):
     self.assertEqual(camelcase_to_dash('CamelCase'), 'camel-case')
     self.assertEqual(camelcase_to_dash('CamelCASE'), 'camel-case')
     self.assertEqual(camelcase_to_dash('CAMELCase'), 'camel-case')
     self.assertEqual(camelcase_to_dash('MyCAMELCase'), 'my-camel-case')
     self.assertEqual(camelcase_to_dash('Camel123Case'), 'camel123-case')
     self.assertEqual(camelcase_to_dash('CAMEL123Case'), 'camel123-case')
     self.assertEqual(camelcase_to_dash('Camel-Case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('camel-case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('Camel_Case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('camel_case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('SimpleCamelCase'),
                      'simple-camel-case')
     self.assertEqual(camelcase_to_dash('DHCPTest'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP-Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP--Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP_Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP__Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP-_Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP_-Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCPv6Test'), 'dhc-pv6-test')
     self.assertEqual(camelcase_to_dash('DHCPV6Test'), 'dhcpv6-test')
     self.assertEqual(camelcase_to_dash('DHCPVersion6plusTest'),
                      'dhcp-version6plus-test')
Esempio n. 13
0
 def test_camelcase_to_dash(self):
     self.assertEqual(camelcase_to_dash('CamelCase'), 'camel-case')
     self.assertEqual(camelcase_to_dash('CamelCASE'), 'camel-case')
     self.assertEqual(camelcase_to_dash('CAMELCase'), 'camel-case')
     self.assertEqual(camelcase_to_dash('MyCAMELCase'), 'my-camel-case')
     self.assertEqual(camelcase_to_dash('Camel123Case'), 'camel123-case')
     self.assertEqual(camelcase_to_dash('CAMEL123Case'), 'camel123-case')
     self.assertEqual(camelcase_to_dash('Camel-Case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('camel-case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('Camel_Case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('camel_case'), 'camel-case')
     self.assertEqual(camelcase_to_dash('SimpleCamelCase'), 'simple-camel-case')
     self.assertEqual(camelcase_to_dash('DHCPTest'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP-Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP--Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP_Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP__Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP-_Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCP_-Test'), 'dhcp-test')
     self.assertEqual(camelcase_to_dash('DHCPv6Test'), 'dhc-pv6-test')
     self.assertEqual(camelcase_to_dash('DHCPV6Test'), 'dhcpv6-test')
     self.assertEqual(camelcase_to_dash('DHCPVersion6plusTest'), 'dhcp-version6plus-test')