Skip to content

Commit

Permalink
Add SourceType enum and use it for source_type in ReuseInfo
Browse files Browse the repository at this point in the history
This commit adds a new enum `SourceType` with three possible values to indicate if the source type is a `.license file`, `file header` or `DEP5 file`. It then updates the usage of `source_type` by replacing the string type with the new `SourceType` enum type. This improves readability and makes the code more maintainable.
  • Loading branch information
linozen committed Jun 1, 2023
1 parent 322dd8a commit 7346234
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 17 additions & 1 deletion src/reuse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import os
import re
from dataclasses import dataclass, field
from enum import Enum, auto
from typing import NamedTuple, Optional, Set

try:
Expand Down Expand Up @@ -88,6 +89,21 @@
_IGNORE_FILE_PATTERNS.extend(_IGNORE_SPDX_PATTERNS)


class SourceType(Enum):
"""
An enumeration representing the types of sources for license information.
Potential values:
DOT_LICENSE_FILE: A .license file containing license information.
FILE_HEADER: A file header containing license information.
DEP5_FILE: A .reuse/dep5 file containing license information.
"""

DOT_LICENSE_FILE = ".license file"
FILE_HEADER = "file header"
DEP5_FILE = ".reuse/dep5 file"


@dataclass(frozen=True)
class ReuseInfo:
"""Simple dataclass holding licensing and copyright information"""
Expand All @@ -96,7 +112,7 @@ class ReuseInfo:
copyright_lines: Set[str] = field(default_factory=set)
contributor_lines: Set[str] = field(default_factory=set)
source_path: Optional[str] = None
source_type: Optional[str] = None
source_type: Optional[SourceType] = None

def contains_copyright_or_licensing(self) -> bool:
"""Either *spdx_expressions* or *copyright_lines* is non-empty."""
Expand Down
9 changes: 5 additions & 4 deletions src/reuse/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
_IGNORE_MESON_PARENT_DIR_PATTERNS,
IdentifierNotFound,
ReuseInfo,
SourceType,
)
from ._licenses import EXCEPTION_MAP, LICENSE_MAP
from ._util import (
Expand Down Expand Up @@ -151,7 +152,7 @@ def reuse_info_of(self, path: PathLike) -> ReuseInfo:
"""
path = _determine_license_path(path)
source_path = ""
source_type = ""
source_type = None

_LOGGER.debug(f"searching '{path}' for SPDX information")

Expand Down Expand Up @@ -189,9 +190,9 @@ def reuse_info_of(self, path: PathLike) -> ReuseInfo:
if file_result:
source_path = str(path)
if path.suffix == ".license":
source_type = ".license file"
source_type = SourceType.DOT_LICENSE_FILE
else:
source_type = "file header"
source_type = SourceType.FILE_HEADER

except (ExpressionError, ParseError):
_LOGGER.error(
Expand Down Expand Up @@ -224,7 +225,7 @@ def reuse_info_of(self, path: PathLike) -> ReuseInfo:
spdx_expressions=dep5_result.spdx_expressions,
copyright_lines=dep5_result.copyright_lines,
source_path=source_path,
source_type="DEP5 file",
source_type=SourceType.DEP5_FILE,
)
# There is a file header or a .license file
return ReuseInfo(
Expand Down

0 comments on commit 7346234

Please sign in to comment.