Skip to content

Commit e3cc970

Browse files
committed
Access sections by both ID and name
Signed-off-by: Nikola Forró <nforro@redhat.com> Assisted-by: Claude Opus 4.6 via Claude Code
1 parent 1985392 commit e3cc970

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

specfile/sections.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ def __contains__(self, id: object) -> bool:
173173
data = super().__getattribute__("data")
174174
except AttributeError:
175175
return False
176-
return any(s.normalized_id == cast(str, id).lower() for s in data)
176+
id_lower = cast(str, id).lower()
177+
return any(
178+
s.normalized_id == id_lower or s.normalized_name == id_lower for s in data
179+
)
177180

178181
def __getattr__(self, id: str) -> Section:
179182
if id not in self:
@@ -209,8 +212,9 @@ def get(self, id: str) -> Section:
209212
return self.data[self.find(id)]
210213

211214
def find(self, id: str) -> int:
215+
id_lower = id.lower()
212216
for i, section in enumerate(self.data):
213-
if section.normalized_id == id.lower():
217+
if section.normalized_id == id_lower or section.normalized_name == id_lower:
214218
return i
215219
raise ValueError
216220

tests/unit/test_sections.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ def test_get():
3939
sections.get("package foo")
4040

4141

42+
def test_contains_and_getattr():
43+
sections = Sections(
44+
[
45+
Section("package"),
46+
Section("package", Options([Token(TokenType.DEFAULT, "baz")]), " "),
47+
Section("install", delimiter=" "),
48+
]
49+
)
50+
assert "package" in sections
51+
assert "install" in sections
52+
assert "install " in sections
53+
assert sections.package == sections[0]
54+
assert getattr(sections, "package baz") == sections[1]
55+
assert sections.install == sections[-1]
56+
assert getattr(sections, "install ") == sections[-1]
57+
58+
4259
@pytest.mark.parametrize(
4360
"id, existing, name, options, content",
4461
[

0 commit comments

Comments
 (0)