Skip to content

Commit 0492e71

Browse files
committed
Cleanup
1 parent 2c3ba99 commit 0492e71

2 files changed

Lines changed: 45 additions & 39 deletions

File tree

dfetch/manifest/manifest.py

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,17 @@ def selected_projects(self, names: Sequence[str]) -> Sequence[ProjectEntry]:
383383
possibles=[project.name for project in all_projects],
384384
)
385385

386+
def _find_doc_project(self, name: str) -> Any | None:
387+
"""Return the raw YAML mapping for the project with *name*, or None."""
388+
for project in self._doc["manifest"]["projects"].as_marked_up():
389+
if project["name"] == name:
390+
return project
391+
return None
392+
386393
def remove(self, project_name: str) -> None:
387394
"""Remove a project from the manifest."""
388-
doc_projects = self._doc["manifest"]["projects"]
389-
names = [p["name"].data for p in doc_projects]
395+
doc_projects = self._doc["manifest"]["projects"].as_marked_up()
396+
names = [p["name"] for p in doc_projects]
390397
try:
391398
del doc_projects[names.index(project_name)]
392399
except ValueError as exc:
@@ -426,16 +433,15 @@ def find_name_in_manifest(self, name: str) -> ManifestEntryLocation:
426433
FileNotFoundError: If manifest text is not available
427434
RuntimeError: If the project name is not found
428435
"""
429-
for p in self._doc["manifest"]["projects"]:
430-
if p["name"].data == name:
431-
mu = p.as_marked_up()
432-
line_0, col_0 = mu.lc.value("name")
433-
return ManifestEntryLocation(
434-
line_number=line_0 + 1,
435-
start=col_0 + 1,
436-
end=col_0 + len(name),
437-
)
438-
raise RuntimeError(f"{name} was not found in the manifest!")
436+
p = self._find_doc_project(name)
437+
if p is None:
438+
raise RuntimeError(f"{name} was not found in the manifest!")
439+
line_0, col_0 = p.lc.value("name")
440+
return ManifestEntryLocation(
441+
line_number=line_0 + 1,
442+
start=col_0 + 1,
443+
end=col_0 + len(name),
444+
)
439445

440446
# ---------------- YAML updates ----------------
441447
def _normalize_string_scalars(self) -> None:
@@ -475,35 +481,34 @@ def append_project_entry(self, project_entry: "ProjectEntry") -> None:
475481

476482
def update_project_version(self, project: ProjectEntry) -> None:
477483
"""Update a project's version in the manifest in-place, preserving layout, comments, and line endings."""
478-
for p in self._doc["manifest"]["projects"]:
479-
if p["name"].data == project.name:
480-
mu = p.as_marked_up()
481-
insert_pos = 1 # right after 'name:' for any newly added key
482-
for key, value in project.version._asdict().items():
483-
if value not in (None, ""):
484-
logger.debug(
485-
f"Updating {project.name} version field '{key}' to '{value}' in manifest"
486-
)
487-
if key in mu:
488-
mu[key] = _yaml_str(value)
489-
else:
490-
mu.insert(insert_pos, key, _yaml_str(value))
491-
insert_pos += 1
484+
p = self._find_doc_project(project.name)
485+
if p is not None:
486+
mu = p
487+
insert_pos = 1 # right after 'name:' for any newly added key
488+
for key, value in project.version._asdict().items():
489+
if value not in (None, ""):
490+
logger.debug(
491+
f"Updating {project.name} version field '{key}' to '{value}' in manifest"
492+
)
493+
if key in mu:
494+
mu[key] = _yaml_str(value)
492495
else:
493-
# Remove any previously-pinned key that is no longer active
494-
# (e.g. an old 'revision' when the project is now pinned by tag).
495-
mu.pop(key, None)
496-
497-
if project.integrity and project.integrity.hash:
498-
mu["integrity"] = CommentedMap({"hash": project.integrity.hash})
496+
mu.insert(insert_pos, key, _yaml_str(value))
497+
insert_pos += 1
499498
else:
500-
# Remove a stale integrity block if the project no longer carries one.
501-
mu.pop("integrity", None)
502-
break
499+
# Remove any previously-pinned key that is no longer active
500+
# (e.g. an old 'revision' when the project is now pinned by tag).
501+
mu.pop(key, None)
502+
503+
if project.integrity and project.integrity.hash:
504+
mu["integrity"] = CommentedMap({"hash": project.integrity.hash})
505+
else:
506+
# Remove a stale integrity block if the project no longer carries one.
507+
mu.pop("integrity", None)
503508

504509
def check_name_uniqueness(self, project_name: str) -> None:
505510
"""Raise if *project_name* is already used in the manifest."""
506-
if project_name in {project.name for project in self.projects}:
511+
if self._find_doc_project(project_name) is not None:
507512
raise ValueError(
508513
f"Project with name '{project_name}' already exists in manifest!"
509514
)

tests/test_add.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,21 @@ def _make_subproject(
7979

8080
def test_check_name_uniqueness_raises_when_duplicate():
8181
m = Mock()
82-
m.projects = [_make_project("foo"), _make_project("bar")]
82+
existing = {"foo": Mock(), "bar": Mock()}
83+
m._find_doc_project.side_effect = lambda name: existing.get(name)
8384
with pytest.raises(ValueError, match="already exists"):
8485
Manifest.check_name_uniqueness(m, "foo")
8586

8687

8788
def test_check_name_uniqueness_passes_for_new_name():
8889
m = Mock()
89-
m.projects = [_make_project("foo")]
90+
m._find_doc_project.return_value = None
9091
Manifest.check_name_uniqueness(m, "bar") # should not raise
9192

9293

9394
def test_check_name_uniqueness_passes_for_empty_manifest():
9495
m = Mock()
95-
m.projects = []
96+
m._find_doc_project.return_value = None
9697
Manifest.check_name_uniqueness(m, "anything")
9798

9899

0 commit comments

Comments
 (0)