ConfigUpdater 4.0.1__tar.gz → 4.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.0.1 → configupdater-4.1}/AUTHORS.rst +2 -0
  2. {configupdater-4.0.1 → configupdater-4.1}/CHANGELOG.rst +19 -0
  3. {configupdater-4.0.1 → configupdater-4.1}/PKG-INFO +1 -1
  4. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/document.py +1 -1
  5. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/option.py +7 -4
  6. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/section.py +1 -1
  7. {configupdater-4.0.1 → configupdater-4.1}/tests/test_configupdater.py +37 -0
  8. {configupdater-4.0.1 → configupdater-4.1}/tests/test_option.py +87 -0
  9. {configupdater-4.0.1 → configupdater-4.1}/tests/test_section.py +33 -0
  10. {configupdater-4.0.1 → configupdater-4.1}/.gitignore +0 -0
  11. {configupdater-4.0.1 → configupdater-4.1}/CONTRIBUTING.rst +0 -0
  12. {configupdater-4.0.1 → configupdater-4.1}/LICENSE.txt +0 -0
  13. {configupdater-4.0.1 → configupdater-4.1}/README.md +0 -0
  14. {configupdater-4.0.1 → configupdater-4.1}/docs/_static/.gitignore +0 -0
  15. {configupdater-4.0.1 → configupdater-4.1}/docs/api.rst +0 -0
  16. {configupdater-4.0.1 → configupdater-4.1}/docs/authors.rst +0 -0
  17. {configupdater-4.0.1 → configupdater-4.1}/docs/changelog.rst +0 -0
  18. {configupdater-4.0.1 → configupdater-4.1}/docs/conf.py +0 -0
  19. {configupdater-4.0.1 → configupdater-4.1}/docs/contributing.rst +0 -0
  20. {configupdater-4.0.1 → configupdater-4.1}/docs/gfx/README.md +0 -0
  21. {configupdater-4.0.1 → configupdater-4.1}/docs/gfx/banner-1280x645.png +0 -0
  22. {configupdater-4.0.1 → configupdater-4.1}/docs/gfx/banner-640x323.png +0 -0
  23. {configupdater-4.0.1 → configupdater-4.1}/docs/gfx/configupdater.svg +0 -0
  24. {configupdater-4.0.1 → configupdater-4.1}/docs/gfx/configupdater_plain.svg +0 -0
  25. {configupdater-4.0.1 → configupdater-4.1}/docs/gfx/favicon.ico +0 -0
  26. {configupdater-4.0.1 → configupdater-4.1}/docs/gfx/icon-92x128.png +0 -0
  27. {configupdater-4.0.1 → configupdater-4.1}/docs/gfx/icon.svg +0 -0
  28. {configupdater-4.0.1 → configupdater-4.1}/docs/gfx/sidebar-254x128.png +0 -0
  29. {configupdater-4.0.1 → configupdater-4.1}/docs/index.rst +0 -0
  30. {configupdater-4.0.1 → configupdater-4.1}/docs/license.rst +0 -0
  31. {configupdater-4.0.1 → configupdater-4.1}/docs/usage.rst +0 -0
  32. {configupdater-4.0.1 → configupdater-4.1}/pyproject.toml +0 -0
  33. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/__init__.py +0 -0
  34. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/block.py +0 -0
  35. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/builder.py +0 -0
  36. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/configupdater.py +0 -0
  37. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/container.py +0 -0
  38. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/parser.py +0 -0
  39. {configupdater-4.0.1 → configupdater-4.1}/src/configupdater/py.typed +0 -0
  40. {configupdater-4.0.1 → configupdater-4.1}/tests/conftest.py +0 -0
  41. {configupdater-4.0.1 → configupdater-4.1}/tests/test_optionxform.py +0 -0
  42. {configupdater-4.0.1 → configupdater-4.1}/tests/test_parser.py +0 -0
  43. {configupdater-4.0.1 → configupdater-4.1}/tests/test_readme.py +0 -0
  44. {configupdater-4.0.1 → configupdater-4.1}/tests/test_setup.cfg +0 -0
  45. {configupdater-4.0.1 → configupdater-4.1}/tools/__init__.py +0 -0
  46. {configupdater-4.0.1 → configupdater-4.1}/tools/release_notes.py +0 -0
  47. {configupdater-4.0.1 → configupdater-4.1}/tools/stdlib_diff.py +0 -0
@@ -10,3 +10,5 @@ Contributors
10
10
  * Sinkleberg
11
11
  * blouirts123 (GitHub)
12
12
  * bysiber (GitHub)
13
+ * civilwarinfrance (GitHub)
14
+ * AlexXuz (GitHub)
@@ -2,6 +2,25 @@
2
2
  Changelog
3
3
  =========
4
4
 
5
+ Version 4.1
6
+ ===========
7
+
8
+ - **Behavior change:** ``Option.as_list()`` returns an empty list instead of
9
+ ``[""]`` for an empty value, e.g. ``key =`` or after ``set_values([])``. An
10
+ empty list and a single empty value are written the same way, so ``set_values([])``
11
+ now round-trips and a later ``append()`` no longer adds an empty first entry,
12
+ PR #182
13
+
14
+ Version 4.0.2
15
+ =============
16
+
17
+ - Make ``Option.as_list()`` use the current value after assignments to or from
18
+ ``None``, so ``append()`` no longer discards values added to a valueless option
19
+ and converting an option to ``None`` no longer raises an ``AttributeError``, PR #179
20
+ - Detach every child block when clearing a document or section, so removed
21
+ sections and options can be attached elsewhere without stale parent links
22
+ (follow-up to PR #43), PR #180
23
+
5
24
  Version 4.0.1
6
25
  =============
7
26
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: ConfigUpdater
3
- Version: 4.0.1
3
+ Version: 4.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/
@@ -179,7 +179,7 @@ class Document(Container[ConfigContent], MutableMapping[str, Section]):
179
179
  return False
180
180
 
181
181
  def clear(self) -> None:
182
- for block in self._structure:
182
+ for block in self._structure.copy():
183
183
  block.detach()
184
184
  self._structure.clear()
185
185
 
@@ -203,16 +203,19 @@ class Option(Block):
203
203
  def as_list(self, separator: str = "\n") -> list[str]:
204
204
  """Returns the (multi-line/element) value as a list
205
205
 
206
- Empty list if value is None, single-element list for a one-element
207
- value and an element for each line in a multi-element value.
206
+ Empty list if value is None or empty (only whitespace), single-element
207
+ list for a one-element value and an element for each line in a
208
+ multi-element value. Since an empty list and a list with a single empty
209
+ string are written the same way, ``set_values([])`` round-trips to ``[]``.
208
210
 
209
211
  Args:
210
212
  separator (str): separator for values, default: line separator
211
213
  """
212
- if self._value_is_none:
214
+ value = self.value
215
+ if value is None or not value.strip():
213
216
  return []
214
217
  else:
215
- return [v.strip() for v in cast(str, self.value).strip().split(separator)]
218
+ return [v.strip() for v in value.strip().split(separator)]
216
219
 
217
220
  def append(self, value: str, **kwargs: Any) -> Option:
218
221
  """Append a value to a multi-line value
@@ -372,6 +372,6 @@ class Section(Block, Container[Content], MutableMapping[str, Option]):
372
372
  return BlockBuilder(self, idx)
373
373
 
374
374
  def clear(self) -> None:
375
- for block in self._structure:
375
+ for block in self._structure.copy():
376
376
  block.detach()
377
377
  self._structure.clear()
@@ -35,6 +35,43 @@ def test_read_write_no_changes(setup_cfg_path, setup_cfg):
35
35
  assert str(updater) == setup_cfg
36
36
 
37
37
 
38
+ @pytest.mark.parametrize("reload", [False, True])
39
+ @pytest.mark.parametrize(
40
+ "config",
41
+ [
42
+ "",
43
+ "[one]\nkey = 1\n",
44
+ "[one]\nkey = 1\n[two]\nkey = 2\n[three]\nkey = 3\n",
45
+ "# preamble\n\n[one]\nkey = 1\n\n[two]\nkey = 2\n",
46
+ ],
47
+ )
48
+ def test_clear_detaches_all_document_blocks(config, reload):
49
+ updater = ConfigUpdater().read_string(config)
50
+ blocks = list(updater.iter_blocks())
51
+ sections = list(updater.iter_sections())
52
+ original_values = updater.to_dict()
53
+
54
+ if reload:
55
+ updater.read_string("[replacement]\nvalue = new\n")
56
+ assert updater.sections() == ["replacement"]
57
+ else:
58
+ reference = ConfigParser()
59
+ reference.read_string(config)
60
+ reference.clear()
61
+ updater.clear()
62
+ assert str(updater) == ""
63
+ assert updater.sections() == reference.sections() == []
64
+
65
+ for block in blocks:
66
+ assert not block.has_container()
67
+ other = ConfigUpdater()
68
+ for section in sections:
69
+ assert all(option.container is section for option in section.iter_options())
70
+ other.add_section(section)
71
+ assert section.container is other
72
+ assert other.to_dict() == original_values
73
+
74
+
38
75
  def test_read_file_like(setup_cfg_path):
39
76
  updater = ConfigUpdater()
40
77
  with open(setup_cfg_path) as fp:
@@ -57,6 +57,93 @@ def test_str_for_set_values():
57
57
  assert str(opt) == "opt = 1, 2, 3, 4\n"
58
58
 
59
59
 
60
+ @pytest.mark.parametrize("separator", ["\n", ", "])
61
+ @pytest.mark.parametrize("prepend_newline", [True, False])
62
+ def test_as_list_and_append_after_setting_valueless_option(separator, prepend_newline):
63
+ source = "# keep comment\n[build]\nplugins\nother : unchanged\n"
64
+ cfg = ConfigUpdater(allow_no_value=True, delimiters=(":",)).read_string(source)
65
+ option = cfg["build"]["plugins"]
66
+ assert option.as_list() == []
67
+ assert str(cfg) == source
68
+ kwargs = {"separator": separator, "prepend_newline": prepend_newline}
69
+ option.set_values(["alpha", "beta"], **kwargs)
70
+ assert option.as_list(separator) == ["alpha", "beta"]
71
+ for value in ["gamma", "delta"]:
72
+ option.append(value, **kwargs)
73
+ assert option.as_list(separator) == ["alpha", "beta", "gamma", "delta"]
74
+ output = StringIO()
75
+ cfg.write(output)
76
+ assert output.getvalue().startswith("# keep comment\n[build]\nplugins :")
77
+ assert output.getvalue().endswith("other : unchanged\n")
78
+ parser = ConfigParser(allow_no_value=True, delimiters=(":",))
79
+ parser.read_string(output.getvalue())
80
+ expected = separator.join(["alpha", "beta", "gamma", "delta"])
81
+ if separator == "\n" and prepend_newline:
82
+ expected = "\n" + expected
83
+ assert parser["build"]["plugins"] == expected
84
+
85
+
86
+ @pytest.mark.parametrize("initial", ["plugins", "plugins = alpha"])
87
+ def test_as_list_after_scalar_assignment(initial):
88
+ cfg = ConfigUpdater(allow_no_value=True).read_string(f"[build]\n{initial}\n")
89
+ option = cfg["build"]["plugins"]
90
+ parser = ConfigParser(allow_no_value=True)
91
+ parser.read_string(f"[build]\n{initial}\n")
92
+ for value in [None, "", "beta", None, "gamma"]:
93
+ option.value = value
94
+ assert option.as_list() == ([value] if value else [])
95
+ parser.set("build", "plugins", value)
96
+ output = StringIO()
97
+ cfg.write(output)
98
+ reparsed = ConfigParser(allow_no_value=True)
99
+ reparsed.read_string(output.getvalue())
100
+ assert dict(reparsed["build"]) == dict(parser["build"])
101
+ option.value = None
102
+ option.append("delta")
103
+ option.append("epsilon")
104
+ assert option.as_list() == ["delta", "epsilon"]
105
+
106
+
107
+ @pytest.mark.parametrize("initial", [None, "alpha"])
108
+ def test_as_list_after_setting_empty_values(initial):
109
+ option = Option("plugins", initial)
110
+ option.set_values([])
111
+ assert option.value == ""
112
+ assert option.as_list() == []
113
+ assert str(option) == "plugins = \n"
114
+ option.append("beta")
115
+ assert option.as_list() == ["beta"]
116
+ assert str(option) == "plugins =\n beta\n"
117
+
118
+
119
+ @pytest.mark.parametrize("value", ["", " ", "\n ", "\n \n "])
120
+ @pytest.mark.parametrize("separator", ["\n", ","])
121
+ def test_as_list_of_empty_value(value, separator):
122
+ option = Option("plugins", "x")
123
+ option.set_values([value], separator=separator, prepend_newline=False)
124
+ assert option.as_list(separator) == []
125
+
126
+
127
+ def test_as_list_of_parsed_empty_value():
128
+ source = "[build]\nplugins =\nother =\n \n"
129
+ cfg = ConfigUpdater().read_string(source)
130
+ parser = ConfigParser()
131
+ parser.read_string(source)
132
+ assert cfg["build"]["plugins"].value == parser["build"]["plugins"] == ""
133
+ assert cfg["build"]["plugins"].as_list() == []
134
+ assert cfg["build"]["other"].as_list() == []
135
+ cfg["build"]["plugins"].append("alpha")
136
+ assert cfg["build"]["plugins"].as_list() == ["alpha"]
137
+
138
+
139
+ def test_append_to_new_valueless_option():
140
+ option = Option("plugins")
141
+ assert option.as_list() == []
142
+ option.append("alpha")
143
+ option.append("beta")
144
+ assert option.as_list() == ["alpha", "beta"]
145
+
146
+
60
147
  @pytest.mark.parametrize("delimiters", [("=", ":"), (":", "="), ("=>", ":"), ":="])
61
148
  @pytest.mark.parametrize("spaces", [True, False])
62
149
  def test_assign_value_to_parsed_no_value_option(delimiters, spaces):
@@ -9,6 +9,39 @@ from configupdater.block import AlreadyAttachedError, NotAttachedError
9
9
  from configupdater.parser import Parser
10
10
 
11
11
 
12
+ @pytest.mark.parametrize(
13
+ "body",
14
+ [
15
+ "",
16
+ "a = 1\n",
17
+ "a = 1\nb = 2\nc = 3\n",
18
+ "a = 1\n# note\nb = 2\n\nc = 3\n",
19
+ ],
20
+ )
21
+ def test_clear_detaches_all_section_blocks(body):
22
+ updater = ConfigUpdater().read_string("[source]\n" + body)
23
+ section = updater["source"]
24
+ blocks = list(section.iter_blocks())
25
+ options = list(section.iter_options())
26
+ original_values = section.to_dict()
27
+ reference = ConfigParser()
28
+ reference.read_string("[source]\n" + body)
29
+ reference["source"].clear()
30
+
31
+ section.clear()
32
+
33
+ assert section.container is updater
34
+ assert section.options() == list(reference["source"]) == []
35
+ assert str(section) == "[source]\n"
36
+ for block in blocks:
37
+ assert not block.has_container()
38
+ other = ConfigUpdater().read_string("[target]\n")
39
+ for option in options:
40
+ other["target"].add_option(option)
41
+ assert option.container is other["target"]
42
+ assert other["target"].to_dict() == original_values
43
+
44
+
12
45
  def test_add_before_skip_comments():
13
46
  doc = Parser().read_string("[first]\na = 1\n")
14
47
  doc["first"].add_after.space().comment("target comment").section("target")
File without changes
File without changes
File without changes
File without changes
File without changes