def ohm_to_spec_list(ohm, value_transform=lambda x: x):
    """Convert OrderedHierarchicalMapping to specification list

    The value_transform argument should be a function that transforms
    the values of the ohm to the entries of the spec list.
    """
    spec = []
    level_start = "["
    level_stop = "]"

    for key, value in ohm.items():
        split_key = key.split(ohm.SECTION_SEPARATOR)
        index = None
        if len(split_key) > 1:
            for level, part in zip(count_up(1), split_key[:-1]):
                section_header = (level * level_start + part + level * level_stop).encode()
                if section_header in spec:
                    index = spec.index(section_header)
                    continue
                else:
                    spec += [section_header]
                    index = spec.index(section_header)

        spec_entry = split_key[-1] + " = " + value_transform(value)
        if index is None:
            spec += [spec_entry.encode()]
        else:
            spec.insert(index + 1, spec_entry.encode())

    return spec
Example #2
0
    def test_deletate_singleton_next(self):
        """Test using a delegated singleton as an iterator
        """

        class FooSingleton(Singleton):
            """Singleton Iterator
            """

            def __init__(self):
                """Initializer
                """
                self.state = 0

            def __iter__(self):
                """Make FooSingleton an iterator...
                """
                return self

            def __next__(self):
                """Returns the next value
                """
                if self.state < 3:
                    self.state += 1
                    return self.state
                else:
                    raise StopIteration

            def next(self):
                """For python 2.x compatibility"""
                return self.__next__()

        singleton = delegate_singleton(FooSingleton)

        for item, expected in zip(singleton, count_up(1)):
            self.assertEqual(item, expected)
Example #3
0
    def test_deletate_singleton_next(self):
        """Test using a delegated singleton as an iterator
        """
        class FooSingleton(Singleton):
            """Singleton Iterator
            """
            def __init__(self):
                """Initializer
                """
                self.state = 0

            def __iter__(self):
                """Make FooSingleton an iterator...
                """
                return self

            def __next__(self):
                """Returns the next value
                """
                if self.state < 3:
                    self.state += 1
                    return self.state
                else:
                    raise StopIteration

            def next(self):
                """For python 2.x compatibility"""
                return self.__next__()

        singleton = delegate_singleton(FooSingleton)

        for item, expected in zip(singleton, count_up(1)):
            self.assertEqual(item, expected)
Example #4
0
    def test_delegate_singleton_iter(self):
        """Test iterating over a delegated singleton
        """
        class FooSingleton(Singleton, list):
            """Iterable Singleton
            """
            pass

        singleton = delegate_singleton(FooSingleton)

        for i in range(10):
            singleton.append(i)

        for item, expected in zip(singleton, count_up()):
            self.assertEqual(item, expected)
Example #5
0
    def test_delegate_singleton_iter(self):
        """Test iterating over a delegated singleton
        """

        class FooSingleton(Singleton, list):
            """Iterable Singleton
            """

            pass

        singleton = delegate_singleton(FooSingleton)

        for i in range(10):
            singleton.append(i)

        for item, expected in zip(singleton, count_up()):
            self.assertEqual(item, expected)
def ohm_to_spec_list(ohm, value_transform=lambda x: x):
    """Convert OrderedHierarchicalMapping to specification list

    The value_transform argument should be a function that transforms
    the values of the ohm to the entries of the spec list.
    """
    spec = []
    level_start = "["
    level_stop = "]"

    sorted_items = sort_ohm(ohm)

    def skip_to_header(spec, section_header):
        """Skips to the next header"""
        index = spec.index(section_header)
        for elem in spec[index:]:
            if not elem[0] == six.b('[')[0]:
                continue
            else:
                index += 1
        return index

    for key, value in sorted_items:
        split_key = key.split(ohm.SECTION_SEPARATOR)
        index = None
        if len(split_key) > 1:
            for level, part in zip(count_up(1), split_key[:-1]):
                section_header = (level * level_start +
                                  part +
                                  level*level_stop).encode()
                if section_header in spec:
                    index = skip_to_header(spec, section_header)
                    continue
                else:
                    spec += [section_header]
                    index = skip_to_header(spec, section_header)

        spec_entry = (split_key[-1] +
                      " = " +
                      value_transform(value))
        if index is None:
            spec += [spec_entry.encode()]
        else:
            spec.insert(index + 1, spec_entry.encode())
    return spec
def ohm_to_spec_list(ohm, value_transform=lambda x: x):
    """Convert OrderedHierarchicalMapping to specification list

    The value_transform argument should be a function that transforms
    the values of the ohm to the entries of the spec list.
    """
    spec = []
    level_start = "["
    level_stop = "]"

    sorted_items = sort_ohm(ohm)

    def skip_to_header(spec, section_header):
        """Skips to the next header"""
        index = spec.index(section_header)
        for elem in spec[index:]:
            if not elem[0] == six.b('[')[0]:
                continue
            else:
                index += 1
        return index

    for key, value in sorted_items:
        split_key = key.split(ohm.SECTION_SEPARATOR)
        index = None
        if len(split_key) > 1:
            for level, part in zip(count_up(1), split_key[:-1]):
                section_header = (level * level_start + part +
                                  level * level_stop).encode()
                if section_header in spec:
                    index = skip_to_header(spec, section_header)
                    continue
                else:
                    spec += [section_header]
                    index = skip_to_header(spec, section_header)

        spec_entry = (split_key[-1] + " = " + value_transform(value))
        if index is None:
            spec += [spec_entry.encode()]
        else:
            spec.insert(index + 1, spec_entry.encode())
    return spec