Skip to content

Commit

Permalink
improve schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Totto16 committed Aug 6, 2023
1 parent 4f6bd2e commit c2ea83a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 43 deletions.
61 changes: 28 additions & 33 deletions schema/content_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@
"properties": {
"type": {
"type": "string",
"enum": [
"series",
"season",
"episode",
"collection"
]
"const": "episode"
},
"scanned_file": {
"$ref": "#/$defs/ScannedFile"
Expand All @@ -51,23 +46,34 @@
"type": "object",
"properties": {
"path": {
"type": "string"
"type": "string",
"title": "file path",
"description": "The file path of the scanned file / folder"
},
"parents": {
"type": "array",
"items": {
"type": "string"
}
},
"minItems": 0,
"maxItems": 3,
"uniqueItems": true,
"title": "parent folders",
"description": "The parent folders of this scanned file / folder"
},
"type": {
"type": "string",
"enum": [
"file",
"folder"
]
],
"title": "file type",
"description": "The type of the file: folder or file"
},
"stats": {
"$ref": "#/$defs/Stats"
"$ref": "#/$defs/Stats",
"title": "file stats",
"description": "The stats of this file"
}
},
"required": [
Expand Down Expand Up @@ -104,10 +110,12 @@
"type": "string"
},
"season": {
"type": "integer"
"type": "integer",
"minimum": 0
},
"episode": {
"type": "integer"
"type": "integer",
"minimum": 1
}
},
"required": [
Expand Down Expand Up @@ -138,12 +146,7 @@
"properties": {
"type": {
"type": "string",
"enum": [
"series",
"season",
"episode",
"collection"
]
"const": "season"
},
"scanned_file": {
"$ref": "#/$defs/ScannedFile"
Expand All @@ -170,7 +173,8 @@
"type": "object",
"properties": {
"season": {
"type": "integer"
"type": "integer",
"minimum": 0
}
},
"required": [
Expand All @@ -183,12 +187,7 @@
"properties": {
"type": {
"type": "string",
"enum": [
"series",
"season",
"episode",
"collection"
]
"const": "series"
},
"scanned_file": {
"$ref": "#/$defs/ScannedFile"
Expand Down Expand Up @@ -218,7 +217,8 @@
"type": "string"
},
"year": {
"type": "integer"
"type": "integer",
"minimum": 1900
}
},
"required": [
Expand All @@ -232,12 +232,7 @@
"properties": {
"type": {
"type": "string",
"enum": [
"series",
"season",
"episode",
"collection"
]
"const": "collection"
},
"scanned_file": {
"$ref": "#/$defs/ScannedFile"
Expand All @@ -262,4 +257,4 @@
}
},
"$schema": "http://json-schema.org/draft/2020-12/schema#"
}
}
2 changes: 2 additions & 0 deletions src/content/collection_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass, field
from pathlib import Path
from typing import (
Literal,
Self,
cast,
)
Expand Down Expand Up @@ -37,6 +38,7 @@ class CollectionContentDict(ContentDict):

@dataclass(slots=True, repr=True)
class CollectionContent(Content):
__type: Literal[ContentType.collection] = field(metadata=alias("type"))
__description: CollectionDescription = field(metadata=alias("description"))
__series: list[SeriesContent] = field(metadata=alias("series"))

Expand Down
7 changes: 6 additions & 1 deletion src/content/episode_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass, field
from pathlib import Path
from typing import (
Literal,
Optional,
Self,
)
Expand Down Expand Up @@ -51,6 +52,7 @@ def itr_print_percent() -> None:

@dataclass(slots=True, repr=True)
class EpisodeContent(Content):
__type: Literal[ContentType.episode] = field(metadata=alias("type"))
__description: EpisodeDescription = field(metadata=alias("description"))
__language: Language = field(metadata=alias("language"))

Expand All @@ -68,7 +70,10 @@ def from_path(
raise NameError(f"Couldn't get EpisodeDescription from '{path}'")

return EpisodeContent(
ContentType.episode, scanned_file, description, Language.unknown(),
ContentType.episode,
scanned_file,
description,
Language.unknown(),
)

@property
Expand Down
43 changes: 34 additions & 9 deletions src/content/general.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env python3

from dataclasses import dataclass
from dataclasses import dataclass, field
from enum import Enum
from hashlib import sha256
from pathlib import Path
from typing import Any, Generic, Optional, Self, TypedDict, TypeVar

from apischema import schema

from classifier import Language
from enlighten import Manager

Expand Down Expand Up @@ -56,8 +58,8 @@ class StatsDict(TypedDict):
@dataclass(slots=True, repr=True)
class EpisodeDescription:
name: str
season: int
episode: int
season: int = field(metadata=schema(min=0))
episode: int = field(metadata=schema(min=1))

def __str__(self: Self) -> str:
return (
Expand All @@ -71,7 +73,7 @@ def __repr__(self: Self) -> str:
@dataclass(slots=True, repr=True)
class SeriesDescription:
name: str
year: int
year: int = field(metadata=schema(min=1900))

def __str__(self: Self) -> str:
return f"<Series name: {self.name} year: {self.year}>"
Expand All @@ -82,7 +84,7 @@ def __repr__(self: Self) -> str:

@dataclass(slots=True, repr=True)
class SeasonDescription:
season: int
season: int = field(metadata=schema(min=0))

def __str__(self: Self) -> str:
return f"<Season season: {self.season}>y"
Expand Down Expand Up @@ -336,10 +338,33 @@ def is_outdated(

@dataclass(slots=True, repr=True)
class ScannedFile:
path: Path
parents: list[str]
type: ScannedFileType # noqa: A003
stats: Stats
path: Path = field(
metadata=schema(
title="file path",
description="The file path of the scanned file / folder",
)
)
parents: list[str] = field(
metadata=schema(
title="parent folders",
description="The parent folders of this scanned file / folder",
min_items=0,
max_items=3,
unique=True,
),
)
type: ScannedFileType = field(
metadata=schema(
title="file type",
description="The type of the file: folder or file",
),
)
stats: Stats = field(
metadata=schema(
title="file stats",
description="The stats of this file",
),
)

@staticmethod
def from_scan(
Expand Down
2 changes: 2 additions & 0 deletions src/content/season_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass, field
from pathlib import Path
from typing import (
Literal,
Optional,
Self,
cast,
Expand Down Expand Up @@ -38,6 +39,7 @@ class SeasonContentDict(ContentDict):

@dataclass(slots=True, repr=True)
class SeasonContent(Content):
__type: Literal[ContentType.season] = field(metadata=alias("type")) # TODO: submit upstream path, to allow this: (to not add "type" in the required field twice)
__description: SeasonDescription = field(metadata=alias("description"))
__episodes: list[EpisodeContent] = field(metadata=alias("episodes"))

Expand Down
2 changes: 2 additions & 0 deletions src/content/series_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dataclasses import dataclass, field
from pathlib import Path
from typing import (
Literal,
Optional,
Self,
cast,
Expand Down Expand Up @@ -38,6 +39,7 @@ class SeriesContentDict(ContentDict):

@dataclass(slots=True, repr=True)
class SeriesContent(Content):
__type: Literal[ContentType.series] = field(metadata=alias("type"))
__description: SeriesDescription = field(metadata=alias("description"))
__seasons: list[SeasonContent] = field(metadata=alias("seasons"))

Expand Down

0 comments on commit c2ea83a

Please sign in to comment.