Skip to content

Commit

Permalink
Merge pull request #188 from opossum-tool/refactor-use-snake-case-in-…
Browse files Browse the repository at this point in the history
…models

Refactor use snake case in models and enable linting
  • Loading branch information
abraemer authored Jan 16, 2025
2 parents 4e5b862 + 49e6fe6 commit 0574759
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 112 deletions.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ select = [
"SIM",
# isort
"I",
# naming
"N"
]
extend-select = ["E501"]

Expand Down
2 changes: 1 addition & 1 deletion src/opossum_lib/opossum/file_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ def write_input_json(
zip_file.writestr(
INPUT_JSON_NAME,
TypeAdapter(OpossumInformation).dump_json(
opossum_file_content.input_file, indent=4, exclude_none=True
opossum_file_content.input_file, indent=4, exclude_none=True, by_alias=True
),
)
26 changes: 13 additions & 13 deletions src/opossum_lib/opossum/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,27 @@ def merge_opossum_information(
for opossum_information in expanded_opossum_information
]
).convert_to_file_resource(),
externalAttributions=_merge_dicts_without_duplicates(
external_attributions=_merge_dicts_without_duplicates(
[
opossum_information.externalAttributions
opossum_information.external_attributions
for opossum_information in expanded_opossum_information
]
),
resourcesToAttributions=_merge_resources_to_attributions(
resources_to_attributions=_merge_resources_to_attributions(
[
opossum_information.resourcesToAttributions
opossum_information.resources_to_attributions
for opossum_information in expanded_opossum_information
]
),
attributionBreakpoints=_merge_attribution_breakpoints(
[
opossum_information.attributionBreakpoints
opossum_information.attribution_breakpoints
for opossum_information in expanded_opossum_information
]
),
externalAttributionSources=_merge_dicts_without_duplicates(
[
opossum_information.externalAttributionSources
opossum_information.external_attribution_sources
for opossum_information in expanded_opossum_information
]
),
Expand All @@ -61,29 +61,29 @@ def expand_opossum_package_identifier(
"""IDs for the attributions should be unique per OpossumInformation.
To prevent possible duplicates we add the projectId of the
OpossumInformation to the IDs as a prefix."""
prefix = opossum_information.metadata.projectId
prefix = opossum_information.metadata.project_id
extended_resources_to_attributions = dict()
for (
resource_path,
identifiers,
) in opossum_information.resourcesToAttributions.items():
) in opossum_information.resources_to_attributions.items():
extended_resources_to_attributions[resource_path] = [
prefix + "-" + identifier for identifier in identifiers
]
extended_external_attributions = dict()
for (
identifier,
external_attribution,
) in opossum_information.externalAttributions.items():
) in opossum_information.external_attributions.items():
extended_external_attributions[prefix + "-" + identifier] = external_attribution

return OpossumInformation(
metadata=opossum_information.metadata,
resources=opossum_information.resources,
externalAttributions=extended_external_attributions,
resourcesToAttributions=extended_resources_to_attributions,
attributionBreakpoints=opossum_information.attributionBreakpoints,
externalAttributionSources=opossum_information.externalAttributionSources,
external_attributions=extended_external_attributions,
resources_to_attributions=extended_resources_to_attributions,
attributionBreakpoints=opossum_information.attribution_breakpoints,
externalAttributionSources=opossum_information.external_attribution_sources,
)


Expand Down
89 changes: 47 additions & 42 deletions src/opossum_lib/opossum/opossum_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,33 @@
from typing import Literal, cast

from pydantic import BaseModel, ConfigDict, model_serializer
from pydantic.alias_generators import to_camel

type OpossumPackageIdentifier = str
type ResourcePath = str
type ResourceInFile = dict[str, ResourceInFile] | int


class OpossumInformation(BaseModel):
class CamelBaseModel(BaseModel):
model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)


class OpossumInformation(CamelBaseModel):
model_config = ConfigDict(frozen=True)
metadata: Metadata
resources: ResourceInFile
externalAttributions: dict[OpossumPackageIdentifier, OpossumPackage]
resourcesToAttributions: dict[ResourcePath, list[OpossumPackageIdentifier]]
attributionBreakpoints: list[str] = field(default_factory=list)
externalAttributionSources: dict[str, ExternalAttributionSource] = field(
external_attributions: dict[OpossumPackageIdentifier, OpossumPackage]
resources_to_attributions: dict[ResourcePath, list[OpossumPackageIdentifier]]
attribution_breakpoints: list[str] = field(default_factory=list)
external_attribution_sources: dict[str, ExternalAttributionSource] = field(
default_factory=dict
)
frequentLicenses: list[FrequentLicense] | None = None
filesWithChildren: list[str] | None = None
baseUrlsForSources: BaseUrlsForSources | None = None
frequent_licenses: list[FrequentLicense] | None = None
files_with_children: list[str] | None = None
base_urls_for_sources: BaseUrlsForSources | None = None


class BaseUrlsForSources(BaseModel):
class BaseUrlsForSources(CamelBaseModel):
@model_serializer
def serialize(self) -> dict:
# hack to override not serializing keys with corresponding value none:
Expand All @@ -40,51 +45,51 @@ def serialize(self) -> dict:
model_config = ConfigDict(extra="allow", frozen=True)


class FrequentLicense(BaseModel):
fullName: str
shortName: str
defaultText: str
class FrequentLicense(CamelBaseModel):
full_name: str
short_name: str
default_text: str


class SourceInfo(BaseModel):
class SourceInfo(CamelBaseModel):
model_config = ConfigDict(frozen=True)
name: str
documentConfidence: int | float | None = 0
additionalName: str | None = None
document_confidence: int | float | None = 0
additional_name: str | None = None


class OpossumPackage(BaseModel):
class OpossumPackage(CamelBaseModel):
model_config = ConfigDict(frozen=True)
source: SourceInfo
attributionConfidence: int | None = None
attribution_confidence: int | None = None
comment: str | None = None
packageName: str | None = None
packageVersion: str | None = None
packageNamespace: str | None = None
packageType: str | None = None
packagePURLAppendix: str | None = None
package_name: str | None = None
package_version: str | None = None
package_namespace: str | None = None
package_type: str | None = None
package_p_u_r_l_appendix: str | None = None
copyright: str | None = None
licenseName: str | None = None
licenseText: str | None = None
license_name: str | None = None
license_text: str | None = None
url: str | None = None
firstParty: bool | None = None
excludeFromNotice: bool | None = None
preSelected: bool | None = None
followUp: Literal["FOLLOW_UP"] | None = None
originId: str | None = None
originIds: list[str] | None = None
first_party: bool | None = None
exclude_from_notice: bool | None = None
pre_selected: bool | None = None
follow_up: Literal["FOLLOW_UP"] | None = None
origin_id: str | None = None
origin_ids: list[str] | None = None
criticality: Literal["high"] | Literal["medium"] | None = None
wasPreferred: bool | None = None
was_preferred: bool | None = None


class Metadata(BaseModel):
class Metadata(CamelBaseModel):
model_config = ConfigDict(extra="allow", frozen=True)
projectId: str
fileCreationDate: str
projectTitle: str
projectVersion: str | None = None
expectedReleaseDate: str | None = None
buildDate: str | None = None
project_id: str
file_creation_date: str
project_title: str
project_version: str | None = None
expected_release_date: str | None = None
build_date: str | None = None


class ResourceType(Enum):
Expand All @@ -94,7 +99,7 @@ class ResourceType(Enum):
OTHER = auto()


class Resource(BaseModel):
class Resource(CamelBaseModel):
model_config = ConfigDict(frozen=True)
type: ResourceType
children: dict[str, Resource] = field(default_factory=dict)
Expand Down Expand Up @@ -181,11 +186,11 @@ def convert_to_file_resource(self) -> ResourceInFile:
return self.to_dict()


class ExternalAttributionSource(BaseModel):
class ExternalAttributionSource(CamelBaseModel):
model_config = ConfigDict(frozen=True)
name: str
priority: int
isRelevantForPreferred: bool | None = None
is_relevant_for_preferred: bool | None = None


def _build_resource_tree(resource: ResourceInFile) -> Resource:
Expand Down
2 changes: 1 addition & 1 deletion src/opossum_lib/opossum/output_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class CamelBaseModel(BaseModel):
model_config = ConfigDict(alias_generator=to_camel)
model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)


class Metadata(CamelBaseModel):
Expand Down
14 changes: 7 additions & 7 deletions src/opossum_lib/scancode/convert_scancode_to_opossum.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ def convert_scancode_to_opossum(filename: str) -> OpossumFileContent:

scancode_header = extract_scancode_header(scancode_data, filename)
metadata = Metadata(
projectId=str(uuid.uuid4()),
fileCreationDate=scancode_header.end_timestamp,
projectTitle="ScanCode file",
project_id=str(uuid.uuid4()),
file_creation_date=scancode_header.end_timestamp,
project_title="ScanCode file",
)

return OpossumFileContent(
OpossumInformation(
metadata=metadata,
resources=resources,
externalAttributions=external_attributions,
resourcesToAttributions=resources_to_attributions,
attributionBreakpoints=[],
externalAttributionSources={},
external_attributions=external_attributions,
resources_to_attributions=resources_to_attributions,
attribution_breakpoints=[],
external_attribution_sources={},
)
)

Expand Down
2 changes: 1 addition & 1 deletion src/opossum_lib/scancode/resource_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def get_attribution_info(file: File) -> list[OpossumPackage]:


def get_attribution_key(attribution: OpossumPackage) -> OpossumPackageIdentifier:
return f"{attribution.licenseName}-{hash(attribution)}"
return f"{attribution.license_name}-{hash(attribution)}"


def create_attribution_mapping(
Expand Down
10 changes: 5 additions & 5 deletions src/opossum_lib/spdx/convert_to_opossum.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ def convert_tree_to_opossum_information(tree: DiGraph) -> OpossumInformation:
opossum_information = OpossumInformation(
metadata=metadata,
resources=resources.convert_to_file_resource(),
externalAttributions=external_attributions,
resourcesToAttributions=resources_to_attributions,
external_attributions=external_attributions,
resources_to_attributions=resources_to_attributions,
attributionBreakpoints=attribution_breakpoints,
externalAttributionSources=external_attribution_sources,
)
Expand Down Expand Up @@ -175,8 +175,8 @@ def create_metadata(tree: DiGraph) -> Metadata:
doc_name = tree.nodes["SPDXRef-DOCUMENT"]["element"].name
created = tree.nodes["SPDXRef-DOCUMENT"]["element"].created
metadata = Metadata(
projectId=str(uuid.uuid4()),
fileCreationDate=created.isoformat(),
projectTitle=doc_name,
project_id=str(uuid.uuid4()),
file_creation_date=created.isoformat(),
project_title=doc_name,
)
return metadata
22 changes: 11 additions & 11 deletions tests/test_opossum/test_file_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@

OPOSSUM_INFILE = OpossumInformation(
metadata=Metadata(
projectId="project-id",
fileCreationDate="30-05-2023",
projectTitle="project-title",
project_id="project-id",
file_creation_date="30-05-2023",
project_title="project-title",
),
resources={},
externalAttributions={},
resourcesToAttributions={},
external_attributions={},
resources_to_attributions={},
)

OPOSSUM_OUTFILE = OpossumOutputFile(
metadata=opossum_lib.opossum.output_model.Metadata(
projectId="project-id",
fileCreationDate="30-05-2023",
inputFileMd5Checksum="checksum",
project_id="project-id",
file_creation_date="30-05-2023",
input_file_md5_checksum="checksum",
),
manualAttributions={},
resourcesToAttributions={},
resolvedExternalAttributions=None,
manual_attributions={},
resources_to_attributions={},
resolved_external_attributions=None,
)


Expand Down
Loading

0 comments on commit 0574759

Please sign in to comment.