-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raw conversion of dataclasses to models
Signed-off-by: GitHub <[email protected]>
- Loading branch information
1 parent
ac2011a
commit 7605666
Showing
3 changed files
with
18 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,26 @@ | ||
"""Models for package metadata.""" | ||
|
||
from dataclasses import dataclass | ||
from typing import Self | ||
|
||
from .models_json import URL, JSONPackageMetadata | ||
|
||
from pydantic import BaseModel | ||
|
||
@dataclass(frozen=True) | ||
class Distribution: | ||
|
||
class Distribution(BaseModel): | ||
"""Metadata for a distribution.""" | ||
|
||
filename: str | ||
url: str | ||
|
||
@classmethod | ||
def from_json_api_data(cls: type[Self], data: URL) -> Self: | ||
"""Build an instance from the JSON API data. | ||
Parameters | ||
---------- | ||
data | ||
The URL of the file hosting the distribution. | ||
Returns | ||
------- | ||
Distribution | ||
An object representing a distribution. | ||
""" | ||
return cls( | ||
filename=data.filename, | ||
url=data.url, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Release: | ||
class Release(BaseModel): | ||
"""Metadata for a release.""" | ||
|
||
version: str | ||
distributions: list[Distribution] | ||
|
||
@classmethod | ||
def from_json_api_data(cls: type[Self], data: JSONPackageMetadata) -> Self: | ||
""" | ||
Build an instance from the JSON API data. | ||
Parameters | ||
---------- | ||
data | ||
The JSON API metadata for a package release. | ||
|
||
Returns | ||
------- | ||
Release | ||
An object representing a package release. | ||
""" | ||
return cls( | ||
version=data.info.version, | ||
distributions=[Distribution.from_json_api_data(json_api_data) for json_api_data in data.urls], | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Package: | ||
class Package(BaseModel): | ||
"""Metadata for a package.""" | ||
|
||
title: str | ||
releases: list[Release] | ||
|
||
@classmethod | ||
def from_json_api_data(cls: type[Self], data: JSONPackageMetadata) -> Self: | ||
""" | ||
Build an instance from the JSON API data. | ||
Parameters | ||
---------- | ||
data | ||
The JSON API metadata for a package. | ||
Returns | ||
------- | ||
Package | ||
An object representing a package. | ||
""" | ||
return cls( | ||
title=data.info.name, | ||
releases=[Release.from_json_api_data(data)], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters