Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add valid property to sources #295

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading