Create the data model types

A bunch of data structures to be used instead of the types used by the
infrastructures, i.e. JSON for API and M3U8 for the HLS.

It should provide a stronger decoupling of the modules and pave the way
for #7 and #8.

Implementation uses `namedtuple`s as they are transparent to test for
equality and are natively hashable (can be used in `set`s or as keys to
`dict`s) which is useful for deduping for instance.
This commit is contained in:
Barbagus 2022-12-27 07:55:36 +01:00
parent 305d8ab679
commit 4fa5e1953e
1 changed files with 119 additions and 0 deletions

119
src/delarte/model.py Normal file
View File

@ -0,0 +1,119 @@
# License: GNU AGPL v3: http://www.gnu.org/licenses/
# This file is part of `delarte` (https://git.afpy.org/fcode/delarte.git)
"""Provide data model types."""
from typing import NamedTuple, Optional
class Metadata(NamedTuple):
"""A program metadata."""
id: str
"""The ID string."""
title: str
"""The title."""
subtitle: str
"""The subtitle or secondary title."""
description: str
"""The description."""
duration: int
"""The duration in seconds."""
class RenditionAudio(NamedTuple):
"""A rendition's audio part."""
lang: str
"""ISO 639-1 two-letter language codes, or "mul" for multiple languages."""
is_original: bool
"""Whether audio track is original (no audio description or dubbing)."""
provides_accessibility: bool
"""Whether provides an audio description."""
class RenditionSubtitles(NamedTuple):
"""A rendition's subtitles part."""
lang: str
"""ISO 639-1 two-letter language codes."""
provides_accessibility: bool
"""Whether provides an readable description."""
class Rendition(NamedTuple):
"""A program's content version."""
audio: RenditionAudio
subtitles: Optional[RenditionSubtitles]
@property
def code(self):
"""Return a code string representation."""
# This code string MUST be built in a way that every possible rendition
# object has its own code string.
# Also, it should be as concise as possible because it will be typed
# by the user in the command line.
code = self.audio.lang
if self.audio.provides_accessibility:
# audio accessibility (audio description)
code += "-AD"
if self.subtitles:
if self.subtitles.provides_accessibility:
# visual accessibility (text description)
code += "-TD"
if self.subtitles.lang != self.audio.lang:
# specifies subtitles language only if different from audio language
return code + "-" + self.subtitles.lang
return code
class Variant(NamedTuple):
"""A program's quality version."""
width: int
"""Horizontal part of the resolution."""
height: int
"""Vertical part of the resolution."""
frame_rate: int
"""Frame rate per seconds."""
@property
def code(self):
"""Return a code string representation."""
# This code string MUST be built in a way that every possible variant
# object has its own code string.
# Also, it should be as concise as possible because it will be typed
# by the user in the command line.
#
# So far, it seems variants differ on resolution only.
return f"{self.height}p"
class Source(NamedTuple):
"""A program source."""
metadata: Metadata
rendition: Rendition
variant: Variant
video: str
"""Video track locator."""
audio: str
"""Audio track locator."""
subtitles: Optional[str]
"""Subtitles track locator."""