ConfigUpdater 4.1__tar.gz → 4.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. {configupdater-4.1 → configupdater-4.1.1}/AUTHORS.rst +1 -0
  2. {configupdater-4.1 → configupdater-4.1.1}/CHANGELOG.rst +7 -0
  3. {configupdater-4.1 → configupdater-4.1.1}/PKG-INFO +1 -1
  4. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/builder.py +1 -0
  5. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/container.py +21 -1
  6. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/document.py +1 -7
  7. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/section.py +1 -8
  8. {configupdater-4.1 → configupdater-4.1.1}/tests/test_configupdater.py +50 -0
  9. {configupdater-4.1 → configupdater-4.1.1}/.gitignore +0 -0
  10. {configupdater-4.1 → configupdater-4.1.1}/CONTRIBUTING.rst +0 -0
  11. {configupdater-4.1 → configupdater-4.1.1}/LICENSE.txt +0 -0
  12. {configupdater-4.1 → configupdater-4.1.1}/README.md +0 -0
  13. {configupdater-4.1 → configupdater-4.1.1}/docs/_static/.gitignore +0 -0
  14. {configupdater-4.1 → configupdater-4.1.1}/docs/api.rst +0 -0
  15. {configupdater-4.1 → configupdater-4.1.1}/docs/authors.rst +0 -0
  16. {configupdater-4.1 → configupdater-4.1.1}/docs/changelog.rst +0 -0
  17. {configupdater-4.1 → configupdater-4.1.1}/docs/conf.py +0 -0
  18. {configupdater-4.1 → configupdater-4.1.1}/docs/contributing.rst +0 -0
  19. {configupdater-4.1 → configupdater-4.1.1}/docs/gfx/README.md +0 -0
  20. {configupdater-4.1 → configupdater-4.1.1}/docs/gfx/banner-1280x645.png +0 -0
  21. {configupdater-4.1 → configupdater-4.1.1}/docs/gfx/banner-640x323.png +0 -0
  22. {configupdater-4.1 → configupdater-4.1.1}/docs/gfx/configupdater.svg +0 -0
  23. {configupdater-4.1 → configupdater-4.1.1}/docs/gfx/configupdater_plain.svg +0 -0
  24. {configupdater-4.1 → configupdater-4.1.1}/docs/gfx/favicon.ico +0 -0
  25. {configupdater-4.1 → configupdater-4.1.1}/docs/gfx/icon-92x128.png +0 -0
  26. {configupdater-4.1 → configupdater-4.1.1}/docs/gfx/icon.svg +0 -0
  27. {configupdater-4.1 → configupdater-4.1.1}/docs/gfx/sidebar-254x128.png +0 -0
  28. {configupdater-4.1 → configupdater-4.1.1}/docs/index.rst +0 -0
  29. {configupdater-4.1 → configupdater-4.1.1}/docs/license.rst +0 -0
  30. {configupdater-4.1 → configupdater-4.1.1}/docs/usage.rst +0 -0
  31. {configupdater-4.1 → configupdater-4.1.1}/pyproject.toml +0 -0
  32. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/__init__.py +0 -0
  33. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/block.py +0 -0
  34. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/configupdater.py +0 -0
  35. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/option.py +0 -0
  36. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/parser.py +0 -0
  37. {configupdater-4.1 → configupdater-4.1.1}/src/configupdater/py.typed +0 -0
  38. {configupdater-4.1 → configupdater-4.1.1}/tests/conftest.py +0 -0
  39. {configupdater-4.1 → configupdater-4.1.1}/tests/test_option.py +0 -0
  40. {configupdater-4.1 → configupdater-4.1.1}/tests/test_optionxform.py +0 -0
  41. {configupdater-4.1 → configupdater-4.1.1}/tests/test_parser.py +0 -0
  42. {configupdater-4.1 → configupdater-4.1.1}/tests/test_readme.py +0 -0
  43. {configupdater-4.1 → configupdater-4.1.1}/tests/test_section.py +0 -0
  44. {configupdater-4.1 → configupdater-4.1.1}/tests/test_setup.cfg +0 -0
  45. {configupdater-4.1 → configupdater-4.1.1}/tools/__init__.py +0 -0
  46. {configupdater-4.1 → configupdater-4.1.1}/tools/release_notes.py +0 -0
  47. {configupdater-4.1 → configupdater-4.1.1}/tools/stdlib_diff.py +0 -0
@@ -12,3 +12,4 @@ Contributors
12
12
  * bysiber (GitHub)
13
13
  * civilwarinfrance (GitHub)
14
14
  * AlexXuz (GitHub)
15
+ * youdie006 (GitHub)
@@ -2,6 +2,13 @@
2
2
  Changelog
3
3
  =========
4
4
 
5
+ Version 4.1.1
6
+ =============
7
+
8
+ - Insert a newline before blocks added with ``add_after`` or ``insert_at`` when the
9
+ file does not end with one, as ``add_section`` and ``add_option`` already do
10
+ (follow-up to PR #178), PR #185
11
+
5
12
  Version 4.1
6
13
  ===========
7
14
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: ConfigUpdater
3
- Version: 4.1
3
+ Version: 4.1.1
4
4
  Summary: Parser like ConfigParser but for updating configuration files
5
5
  Project-URL: Homepage, https://github.com/pyscaffold/configupdater
6
6
  Project-URL: Documentation, https://configupdater.readthedocs.io/
@@ -21,6 +21,7 @@ class BlockBuilder:
21
21
  self._idx = idx
22
22
 
23
23
  def _insert(self, block: Block) -> Self:
24
+ self._idx = self._container._ensure_newline_before(self._idx)
24
25
  self._container.structure.insert(self._idx, block)
25
26
  self._idx += 1
26
27
  return self
@@ -10,7 +10,7 @@ from __future__ import annotations
10
10
  from collections.abc import Iterator
11
11
  from copy import deepcopy
12
12
  from textwrap import indent
13
- from typing import TYPE_CHECKING, Any, Generic, Self, TypeVar
13
+ from typing import TYPE_CHECKING, Any, Generic, Self, TypeVar, cast
14
14
 
15
15
  if TYPE_CHECKING:
16
16
  from .block import Block
@@ -75,6 +75,26 @@ class Container(Generic[T]):
75
75
  del self._structure[idx]
76
76
  return self
77
77
 
78
+ def _ensure_newline_before(self, idx: int) -> int:
79
+ """Insert a newline at index idx if the block before it doesn't end with one
80
+
81
+ This happens after the last block of a file without a trailing newline.
82
+ A block inserted there would otherwise be glued onto that line.
83
+
84
+ Returns:
85
+ index where the new block has to be inserted
86
+ """
87
+ from .block import Space
88
+
89
+ idx = min(idx, len(self._structure))
90
+ if idx > 0 and not str(self._structure[idx - 1]).endswith("\n"):
91
+ space = Space(container=self)
92
+ space.add_line("\n")
93
+ # Every container (document and section) holds `Space` blocks
94
+ self._structure.insert(idx, cast(T, space))
95
+ idx += 1
96
+ return idx
97
+
78
98
  def iter_blocks(self) -> Iterator[T]:
79
99
  """Iterate over all blocks inside container."""
80
100
  return iter(self._structure)
@@ -204,13 +204,7 @@ class Document(Container[ConfigContent], MutableMapping[str, Section]):
204
204
  if self.has_section(section_obj.name):
205
205
  raise DuplicateSectionError(section_obj.name)
206
206
 
207
- # Ensure a newline before the new section if the last block
208
- # doesn't end with one (e.g. file without trailing newline)
209
- if self._structure and not str(self._structure[-1]).endswith("\n"):
210
- space = Space(container=self)
211
- space.add_line("\n")
212
- self._structure.append(space)
213
-
207
+ self._ensure_newline_before(len(self._structure))
214
208
  section_obj.attach(self)
215
209
  self._structure.append(section_obj)
216
210
 
@@ -71,14 +71,7 @@ class Section(Block, Container[Content], MutableMapping[str, Option]):
71
71
  entry (Option): key value pair as Option object
72
72
  """
73
73
  entry.attach(self)
74
-
75
- # Ensure a newline before the new option if the last block
76
- # doesn't end with one (e.g. file without trailing newline)
77
- if self._structure and not str(self._structure[-1]).endswith("\n"):
78
- space = Space(container=self)
79
- space.add_line("\n")
80
- self._structure.append(space)
81
-
74
+ self._ensure_newline_before(len(self._structure))
82
75
  self._structure.append(entry)
83
76
  return self
84
77
 
@@ -811,6 +811,56 @@ def test_add_section_no_trailing_newline():
811
811
  assert "value2[section2]" not in result
812
812
 
813
813
 
814
+ @pytest.mark.parametrize("newline", ["\n", "\r\n"])
815
+ @pytest.mark.parametrize(
816
+ "body", ["", "{nl}key : value", "{nl}# tail", "{nl}key = one{nl} two"]
817
+ )
818
+ @pytest.mark.parametrize(
819
+ ("method", "added"),
820
+ [
821
+ ("option", "new_key = v\n"),
822
+ ("comment", "# note\n"),
823
+ ("space", "\n"),
824
+ ("section", "[t]\n"),
825
+ ],
826
+ )
827
+ def test_builder_no_trailing_newline(newline, body, method, added):
828
+ original = "[s]" + body.format(nl=newline)
829
+ updater = ConfigUpdater().read_string(original)
830
+ expected = ConfigParser(interpolation=None)
831
+ expected.read_string(original)
832
+ section = updater["s"]
833
+ builder = section.insert_at(len(section.structure))
834
+ if method == "option":
835
+ builder.option("new_key", "v")
836
+ expected.set("s", "new_key", "v")
837
+ elif method == "comment":
838
+ builder.comment("note")
839
+ elif method == "space":
840
+ builder.space()
841
+ else:
842
+ section.add_after.section("t")
843
+ expected.add_section("t")
844
+ assert str(updater) == original + "\n" + added
845
+ parser = ConfigParser(interpolation=None)
846
+ parser.read_string(str(updater))
847
+ assert {s: dict(parser[s]) for s in parser} == {
848
+ s: dict(expected[s]) for s in expected
849
+ }
850
+
851
+
852
+ def test_builder_insert_at_start_no_trailing_newline():
853
+ updater = ConfigUpdater().read_string("[s]\nkey = v")
854
+ updater["s"].insert_at(0).option("first", "1")
855
+ assert str(updater) == "[s]\nfirst = 1\nkey = v"
856
+
857
+
858
+ def test_builder_insert_past_end_no_trailing_newline():
859
+ updater = ConfigUpdater().read_string("[s]\nkey = v")
860
+ updater["s"].insert_at(10).option("new", "1").comment("note")
861
+ assert str(updater) == "[s]\nkey = v\nnew = 1\n# note\n"
862
+
863
+
814
864
  test6_cfg_out_overwritten = """
815
865
  [section0]
816
866
  key0 = 42
File without changes
File without changes
File without changes
File without changes
File without changes