Exemplo n.º 1
0
 def test_words(self):
     self.assertEqual(
         ['one', 'two', 'three'],
         list(split_list(' one, two, three ')))
Exemplo n.º 2
0
 def test_basic(self):
     self.assertEqual(
         ['1', '2', '3'],
         list(split_list('1, 2, 3')))
Exemplo n.º 3
0
 def test_space_suffix(self):
     self.assertEqual(
         ['1', '2'],
         list(split_list('1, 2   ')))
    def from_stream(cls, stream, filename=None):
        '''Parse a configuration from a stream

        This functions turns a configuration file into a
        :class:`Configuration`, with some sanity checks along the way. It is
        based on :class:`ConfigParser.SafeConfigParser`.

        The `stream` object must have a :meth:`readline` method. `filename`
        will be used in error reporting, if available.

        :param stream: Stream to parse
        :type stream: File-like object
        :param filename: Filename of input
        :type filename: `str`

        :returns: Parsed :class:`Configuration`
        :rtype: :class:`Configuration`

        :raise ConfigurationError: Various configurations issues detected
        '''

        parser = ConfigParser.SafeConfigParser()
        parser.readfp(stream, filename)

        ring_sections = [
            section for section in parser.sections()
            if section.startswith(RING_SECTION_PREFIX)]
        sp_sections = [
            section for section in parser.sections()
            if section.startswith(STORAGE_POLICY_SECTION_PREFIX)]

        _default = object()

        def get(section, setting, default=_default):
            '''Safely retrieve a value from a configuration.'''

            if not parser.has_option(section, setting):
                if default is not _default:
                    return default
                else:
                    raise ConfigurationError(
                        'Section %r lacks a %r setting' % (section, setting))

            return parser.get(section, setting)

        rings = {}
        for section in ring_sections:
            name = section[len(RING_SECTION_PREFIX):]
            if not name:
                raise ConfigurationError('Invalid section name %r' % section)

            location = get(section, RING_LOCATION_OPTION)
            if not location:
                raise ConfigurationError(
                    'Invalid %r setting in %r' %
                    (RING_LOCATION_OPTION, section))

            endpoints = get(section, RING_SPROXYD_ENDPOINTS_OPTION)

            endpoints2 = set()
            for endpoint in utils.split_list(endpoints):
                try:
                    endpoints2.add(Endpoint(endpoint))
                except ValueError as exc:
                    raise ConfigurationError(
                        'Error parsing endpoint %r in %r: %s' %
                        (endpoint, section, exc.message))

            if not endpoints2:
                raise ConfigurationError(
                    'Invalid %r setting in %r' %
                    (RING_SPROXYD_ENDPOINTS_OPTION, section))

            rings[name] = Ring(name, location, endpoints2)

        policies = list()
        for section in sp_sections:
            index = section[len(STORAGE_POLICY_SECTION_PREFIX):]
            if not index:
                raise ConfigurationError('Invalid section name %r' % section)

            try:
                index2 = int(index)
            except (ValueError, TypeError):
                raise ConfigurationError('Invalid policy index: %r' % index)

            read = get(section, STORAGE_POLICY_READ_OPTION, '')
            write = get(section, STORAGE_POLICY_WRITE_OPTION)

            read2 = set()
            for ring in utils.split_list(read):
                if ring not in rings:
                    raise ConfigurationError(
                        'Unknown %r ring %r in policy %r' %
                        (STORAGE_POLICY_READ_OPTION, ring, index2))

                read2.add(rings[ring])

            write2 = list(utils.split_list(write))

            if len(write2) > 1:
                raise ConfigurationError('Multiple %r rings defined in %r' %
                                         (STORAGE_POLICY_WRITE_OPTION, section))

            write3 = set()
            for ring in write2:
                if ring not in rings:
                    raise ConfigurationError(
                        'Unknown %r ring %r in policy %r' %
                        (STORAGE_POLICY_WRITE_OPTION, ring, index2))

                write3.add(rings[ring])

            policies.append(StoragePolicy(index2, read2, write3))

        return cls(policies)
Exemplo n.º 5
0
 def test_empty_string(self):
     self.assertEqual(
         [],
         list(split_list('')))
Exemplo n.º 6
0
    def from_stream(cls, stream, filename=None):
        '''Parse a configuration from a stream

        This functions turns a configuration file into a
        :class:`Configuration`, with some sanity checks along the way. It is
        based on :class:`ConfigParser.SafeConfigParser`.

        The `stream` object must have a :meth:`readline` method. `filename`
        will be used in error reporting, if available.

        :param stream: Stream to parse
        :type stream: File-like object
        :param filename: Filename of input
        :type filename: `str`

        :returns: Parsed :class:`Configuration`
        :rtype: :class:`Configuration`

        :raise ConfigurationError: Various configurations issues detected
        '''

        parser = ConfigParser.SafeConfigParser()
        parser.readfp(stream, filename)

        ring_sections = [
            section for section in parser.sections()
            if section.startswith(RING_SECTION_PREFIX)
        ]
        sp_sections = [
            section for section in parser.sections()
            if section.startswith(STORAGE_POLICY_SECTION_PREFIX)
        ]

        _default = object()

        def get(section, setting, default=_default):
            '''Safely retrieve a value from a configuration.'''

            if not parser.has_option(section, setting):
                if default is not _default:
                    return default
                else:
                    raise ConfigurationError('Section %r lacks a %r setting' %
                                             (section, setting))

            return parser.get(section, setting)

        rings = {}
        for section in ring_sections:
            name = section[len(RING_SECTION_PREFIX):]
            if not name:
                raise ConfigurationError('Invalid section name %r' % section)

            location = get(section, RING_LOCATION_OPTION)
            if not location:
                raise ConfigurationError('Invalid %r setting in %r' %
                                         (RING_LOCATION_OPTION, section))

            endpoints = get(section, RING_SPROXYD_ENDPOINTS_OPTION)

            endpoints2 = set()
            for endpoint in utils.split_list(endpoints):
                try:
                    endpoints2.add(Endpoint(endpoint))
                except ValueError as exc:
                    raise ConfigurationError(
                        'Error parsing endpoint %r in %r: %s' %
                        (endpoint, section, exc.message))

            if not endpoints2:
                raise ConfigurationError(
                    'Invalid %r setting in %r' %
                    (RING_SPROXYD_ENDPOINTS_OPTION, section))

            rings[name] = Ring(name, location, endpoints2)

        policies = list()
        for section in sp_sections:
            index = section[len(STORAGE_POLICY_SECTION_PREFIX):]
            if not index:
                raise ConfigurationError('Invalid section name %r' % section)

            try:
                index2 = int(index)
            except (ValueError, TypeError):
                raise ConfigurationError('Invalid policy index: %r' % index)

            read = get(section, STORAGE_POLICY_READ_OPTION, '')
            write = get(section, STORAGE_POLICY_WRITE_OPTION)

            read2 = set()
            for ring in utils.split_list(read):
                if ring not in rings:
                    raise ConfigurationError(
                        'Unknown %r ring %r in policy %r' %
                        (STORAGE_POLICY_READ_OPTION, ring, index2))

                read2.add(rings[ring])

            write2 = list(utils.split_list(write))

            if len(write2) > 1:
                raise ConfigurationError(
                    'Multiple %r rings defined in %r' %
                    (STORAGE_POLICY_WRITE_OPTION, section))

            write3 = set()
            for ring in write2:
                if ring not in rings:
                    raise ConfigurationError(
                        'Unknown %r ring %r in policy %r' %
                        (STORAGE_POLICY_WRITE_OPTION, ring, index2))

                write3.add(rings[ring])

            policies.append(StoragePolicy(index2, read2, write3))

        return cls(policies)
Exemplo n.º 7
0
 def test_words(self):
     self.assertEqual(['one', 'two', 'three'],
                      list(split_list(' one, two, three ')))
Exemplo n.º 8
0
 def test_space_suffix(self):
     self.assertEqual(['1', '2'], list(split_list('1, 2   ')))
Exemplo n.º 9
0
 def test_basic(self):
     self.assertEqual(['1', '2', '3'], list(split_list('1, 2, 3')))
Exemplo n.º 10
0
 def test_empty_string(self):
     self.assertEqual([], list(split_list('')))