Skip to content

Commit

Permalink
Add valid property to sources (#295)
Browse files Browse the repository at this point in the history
Add valid property to sources

Related to packit/packit#2129.
RELEASE NOTES BEGIN
Sources now have a valid property that indicates whether a source is valid in the current context, meaning it is not present in a false branch of any condition.
RELEASE NOTES END

Reviewed-by: Laura Barcziová
  • Loading branch information
softwarefactory-project-zuul[bot] authored Oct 26, 2023
2 parents 4a8d268 + 47c21fa commit 39bcedf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
22 changes: 18 additions & 4 deletions specfile/sourcelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import copy
from typing import TYPE_CHECKING, Any, Dict, List, Optional, overload

from specfile.conditions import process_conditions
from specfile.formatter import formatted
from specfile.macro_definitions import MacroDefinitions
from specfile.macros import Macros
from specfile.sections import Section
from specfile.tags import Comments
Expand All @@ -22,24 +24,31 @@ class SourcelistEntry:
Attributes:
location: Literal location of the source/patch as stored in the spec file.
comments: List of comments associated with the source/patch.
valid: Whether the entry is not located in a false branch of a condition.
"""

def __init__(
self, location: str, comments: Comments, context: Optional["Specfile"] = None
self,
location: str,
comments: Comments,
valid: bool = True,
context: Optional["Specfile"] = None,
) -> None:
"""
Constructs a `SourceListEntry` object.
Args:
location: Literal location of the source/patch as stored in the spec file.
comments: List of comments associated with the source/patch.
valid: Whether the entry is not located in a false branch of a condition.
context: `Specfile` instance that defines the context for macro expansions.
Returns:
Constructed instance of `SourceListEntry` class.
"""
self.location = location
self.comments = comments.copy()
self.valid = valid
self._context = context

def __eq__(self, other: object) -> bool:
Expand All @@ -50,7 +59,8 @@ def __eq__(self, other: object) -> bool:
@formatted
def __repr__(self) -> str:
return (
f"SourcelistEntry({self.location!r}, {self.comments!r}, {self._context!r})"
f"SourcelistEntry({self.location!r}, {self.comments!r}, "
f"{self.valid!r}, {self._context!r})"
)

def __deepcopy__(self, memo: Dict[int, Any]) -> "SourcelistEntry":
Expand Down Expand Up @@ -134,11 +144,15 @@ def parse(
Returns:
Constructed instance of `Sourcelist` class.
"""
macro_definitions = MacroDefinitions.parse(list(section))
lines = process_conditions(list(section), macro_definitions, context)
data = []
buffer: List[str] = []
for line in section:
for line, valid in lines:
if line and not line.lstrip().startswith("#"):
data.append(SourcelistEntry(line, Comments.parse(buffer), context))
data.append(
SourcelistEntry(line, Comments.parse(buffer), valid, context)
)
buffer = []
else:
buffer.append(line)
Expand Down
23 changes: 21 additions & 2 deletions specfile/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def comments(self) -> Comments:
"""List of comments associated with the source."""
...

@property
@abstractmethod
def valid(self) -> bool:
"""Whether the source is not located in a false branch of a condition."""
...


class TagSource(Source):
"""Class that represents a source backed by a spec file tag."""
Expand Down Expand Up @@ -161,6 +167,11 @@ def comments(self) -> Comments:
"""List of comments associated with the source."""
return self._tag.comments

@property
def valid(self) -> bool:
"""Whether the source is not located in a false branch of a condition."""
return self._tag.valid


class ListSource(Source):
"""Class that represents a source backed by a line in a %sourcelist section."""
Expand Down Expand Up @@ -224,6 +235,11 @@ def comments(self) -> Comments:
"""List of comments associated with the source."""
return self._source.comments

@property
def valid(self) -> bool:
"""Whether the source is not located in a false branch of a condition."""
return self._source.valid


class Sources(collections.abc.MutableSequence):
"""Class that represents a sequence of all sources."""
Expand Down Expand Up @@ -537,12 +553,15 @@ def insert(self, i: int, location: str) -> None:
container.insert(
index,
SourcelistEntry( # type: ignore[arg-type]
location, Comments(), context=self._context
location,
Comments(),
container[index - 1].valid,
context=self._context,
),
)
elif self._sourcelists:
self._sourcelists[-1].append(
SourcelistEntry(location, Comments(), context=self._context)
SourcelistEntry(location, Comments(), True, context=self._context)
)
else:
index, name, separator = self._get_initial_tag_setup()
Expand Down

0 comments on commit 39bcedf

Please sign in to comment.