Skip to content

removing space block from a config with multiple space blocks would always act on first space block, leading to #140

@tinybeebee

Description

@tinybeebee

My initial aim was to reformat an ini file so that each section (sorted) has one and only one blank line before it, which is achieved by removing all sections, clearing extra space blocks, and then adding them back in sorted order plus preceding space block.
However when I remove space block (which is not 1st one) and then iterate structure, it complains non-attached space block. See example and traceback below:

Example :

config = configupdater.ConfigUpdater()
config.add_section('aaa')
config.add_section("bbb")
config.add_section("ccc")
config['bbb'].add_before.space()
config["ccc"].add_before.space()
config['ccc'].previous_block.detach()
for b in config.structure:
    print(b.container_idx, repr(b))

Traceback :

---------------------------------------------------------------------------
NotAttachedError                          Traceback (most recent call last)
Cell In[133], line 2
      1 for b in config.structure:
----> 2     print(b.container_idx, repr(b))

File c:\Programming\Python2\test-python\.venv\Lib\site-packages\configupdater\block.py:131, in Block.container_idx(self)
    128 @property
    129 def container_idx(self: B) -> int:
    130     """Index of the block within its container"""
--> 131     return self.container.structure.index(self)

File c:\Programming\Python2\test-python\.venv\Lib\site-packages\configupdater\block.py:125, in Block.container(self)
    123 """Container holding the block"""
    124 if self._container is None:
--> 125     raise NotAttachedError(self)
    126 return self._container

NotAttachedError: <Space> is not attached to a container yet. Try to insert it first.

If I check container_idx before detaching block, I can see that both space block have container_idx=1.

0 <Section: 'aaa' []>
1 <Space: '\n'>
2 <Section: 'bbb' []>
1 <Space: '\n'>
4 <Section: 'ccc' []>

Versions and main components

  • ConfigUpdater Version: 3.2
  • Python Version: 3.12.10 (main, Apr 9 2025, 04:06:22) [MSC v.1943 64 bit (AMD64)]
  • Operating system: Windows 11
  • How did you install ConfigUpdater: (conda/pip) uv add configupdater

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions