Skip to content

Template

Module to store the template domain functions and classes.

EpisodeConfig (BaseModel) pydantic-model

Episode configuration.

Attributes:

Name Type Description
title str

Episode title.

description str

Episode description.

pub_date datetime

Episode publication date.

url str

Episode URL.

author str

Episode author.

Source code in yaml2rss/domain/template.py
class EpisodeConfig(BaseModel):
    """Episode configuration.

    Attributes:
        title: Episode title.
        description: Episode description.
        pub_date: Episode publication date.
        url: Episode URL.
        author: Episode author.
    """

    title: str
    description: str
    pub_date: datetime
    url: str
    author: str = None

FileDetails (BaseModel) pydantic-model

File details.

Attributes:

Name Type Description
file FilePath

File path.

length int

File length in bytes.

duration int

File duration in seconds.

type str

File mime type.

Source code in yaml2rss/domain/template.py
class FileDetails(BaseModel):
    """File details.

    Attributes:
        file: File path.
        length: File length in bytes.
        duration: File duration in seconds.
        type: File mime type.
    """

    file: FilePath
    length: int
    duration: int
    type: str

    @root_validator(pre=True)
    @classmethod
    def validate_length(cls, values):
        """Get file length in bytes if None.

        Arguments:
            cls: Class.
            values: Instance initialization values.

        Returns:
            Values with required modifications.
        """
        if "length" not in values or values["length"] is None:
            values["length"] = getsize(values["file"])

        return values

    @root_validator(pre=True)
    @classmethod
    def validate_duration(cls, values):
        """Get file duration in seconds if None.

        Arguments:
            cls: Class.
            values: Instance initialization values.

        Returns:
            Values with required modifications.
        """
        if "duration" not in values or values["duration"] is None:
            values["duration"] = int(get_duration(filename=values["file"]))

        return values

    @root_validator(pre=True)
    @classmethod
    def validate_type(cls, values):
        """Get file mime type if None.

        Arguments:
            cls: Class.
            values: Instance initialization values.

        Returns:
            Values with required modifications.
        """
        if "type" not in values or values["type"] is None:
            values["type"] = mime.from_file(values["file"])

        if values["type"] not in SUPPORTED_AUDIO_MIME_TYPES:
            raise ValidationError()

        return values

validate_duration(values) classmethod

Get file duration in seconds if None.

Parameters:

Name Type Description Default
cls

Class.

required
values

Instance initialization values.

required

Returns:

Type Description

Values with required modifications.

Source code in yaml2rss/domain/template.py
@root_validator(pre=True)
@classmethod
def validate_duration(cls, values):
    """Get file duration in seconds if None.

    Arguments:
        cls: Class.
        values: Instance initialization values.

    Returns:
        Values with required modifications.
    """
    if "duration" not in values or values["duration"] is None:
        values["duration"] = int(get_duration(filename=values["file"]))

    return values

validate_length(values) classmethod

Get file length in bytes if None.

Parameters:

Name Type Description Default
cls

Class.

required
values

Instance initialization values.

required

Returns:

Type Description

Values with required modifications.

Source code in yaml2rss/domain/template.py
@root_validator(pre=True)
@classmethod
def validate_length(cls, values):
    """Get file length in bytes if None.

    Arguments:
        cls: Class.
        values: Instance initialization values.

    Returns:
        Values with required modifications.
    """
    if "length" not in values or values["length"] is None:
        values["length"] = getsize(values["file"])

    return values

validate_type(values) classmethod

Get file mime type if None.

Parameters:

Name Type Description Default
cls

Class.

required
values

Instance initialization values.

required

Returns:

Type Description

Values with required modifications.

Source code in yaml2rss/domain/template.py
@root_validator(pre=True)
@classmethod
def validate_type(cls, values):
    """Get file mime type if None.

    Arguments:
        cls: Class.
        values: Instance initialization values.

    Returns:
        Values with required modifications.
    """
    if "type" not in values or values["type"] is None:
        values["type"] = mime.from_file(values["file"])

    if values["type"] not in SUPPORTED_AUDIO_MIME_TYPES:
        raise ValidationError()

    return values

PodcastEpisodeConfig (EpisodeConfig) pydantic-model

Extension of episode configuration for a podcast episode.

Parameters:

Name Type Description Default
file_details

Details of the podcast audio file.

required
Source code in yaml2rss/domain/template.py
class PodcastEpisodeConfig(EpisodeConfig):
    """Extension of episode configuration for a podcast episode.

    Arguments:
        file_details: Details of the podcast audio file.
    """

    file_details: FileDetails

PodcastSeasonConfig (SeasonConfig) pydantic-model

Pocast season configuration.

Parameters:

Name Type Description Default
episodes

List of podcast season episodes configuration.

required
Source code in yaml2rss/domain/template.py
class PodcastSeasonConfig(SeasonConfig):
    """Pocast season configuration.

    Arguments:
        episodes: List of podcast season episodes configuration.
    """

    episodes: List[PodcastEpisodeConfig]

PodcastTemplateConfig (TemplateConfig) pydantic-model

Template for podcast RSS feed.

Parameters:

Name Type Description Default
image_path

Path to feed image relative to link.

required
image_title

Feed image title.

required
files_root_url

Base URL for the feed audio files.

required
files_root_path

Path to the directory containing the audio files.

required
seasons

Podcast seasons configuration.

required
Source code in yaml2rss/domain/template.py
class PodcastTemplateConfig(TemplateConfig):
    """Template for podcast RSS feed.

    Arguments:
        image_path: Path to feed image relative to `link`.
        image_title: Feed image title.
        files_root_url: Base URL for the feed audio files.
        files_root_path: Path to the directory containing the audio files.
        seasons: Podcast seasons configuration.
    """

    image_path: FilePath
    image_title: str
    files_root_url: HttpUrl
    files_root_path: DirectoryPath = Path("")
    seasons: PodcastSeasonsConfig

    @root_validator(pre=True)
    @classmethod
    def validate_image_title(cls, values):
        """Set the episode author to the feed author if None.

        Arguments:
            cls: Class.
            values: Instance initialization values.

        Returns:
            Values with required modifications.
        """
        if "image_title" not in values or values["image_title"] is None:
            values["image_title"] = values["title"]

        return values

    @root_validator(pre=True)
    @classmethod
    def validate_files_root_url(cls, values):
        """Set the files root URl to `link` if None.

        Arguments:
            cls: Class.
            values: Instance initialization values.

        Returns:
            Values with required modifications.
        """
        if "files_root_url" not in values or values["files_root_url"] is None:
            values["files_root_url"] = values["link"]

        return values

    @root_validator(pre=True)
    @classmethod
    def validate_episode_file(cls, values):
        """Add the `files_root_path` to the episodes files.

        Arguments:
            cls: Class.
            values: Instance initialization values.

        Returns:
            Values with required modifications.
        """
        for season in values["seasons"]:
            for episode in season["episodes"]:
                file_details: dict = episode["file_details"]
                if "file" in file_details and file_details["file"] is not None:
                    episode["file_details"]["file"] = (
                        values["files_root_path"] + file_details["file"]
                    )

        return values

    @root_validator(pre=True)
    @classmethod
    def validate_episode_url(cls, values):
        """Set the episodes URL to `files_root_url` + `file` if None.

        Arguments:
            cls: Class.
            values: Instance initialization values.

        Returns:
            Values with required modifications.
        """
        for season in values["seasons"]:
            for episode in season["episodes"]:
                if "url" not in episode or episode["url"] is None:
                    episode["url"] = (
                        values["files_root_url"]
                        + episode["file_details"]["file"]
                    )

        return values

validate_episode_file(values) classmethod

Add the files_root_path to the episodes files.

Parameters:

Name Type Description Default
cls

Class.

required
values

Instance initialization values.

required

Returns:

Type Description

Values with required modifications.

Source code in yaml2rss/domain/template.py
@root_validator(pre=True)
@classmethod
def validate_episode_file(cls, values):
    """Add the `files_root_path` to the episodes files.

    Arguments:
        cls: Class.
        values: Instance initialization values.

    Returns:
        Values with required modifications.
    """
    for season in values["seasons"]:
        for episode in season["episodes"]:
            file_details: dict = episode["file_details"]
            if "file" in file_details and file_details["file"] is not None:
                episode["file_details"]["file"] = (
                    values["files_root_path"] + file_details["file"]
                )

    return values

validate_episode_url(values) classmethod

Set the episodes URL to files_root_url + file if None.

Parameters:

Name Type Description Default
cls

Class.

required
values

Instance initialization values.

required

Returns:

Type Description

Values with required modifications.

Source code in yaml2rss/domain/template.py
@root_validator(pre=True)
@classmethod
def validate_episode_url(cls, values):
    """Set the episodes URL to `files_root_url` + `file` if None.

    Arguments:
        cls: Class.
        values: Instance initialization values.

    Returns:
        Values with required modifications.
    """
    for season in values["seasons"]:
        for episode in season["episodes"]:
            if "url" not in episode or episode["url"] is None:
                episode["url"] = (
                    values["files_root_url"]
                    + episode["file_details"]["file"]
                )

    return values

validate_files_root_url(values) classmethod

Set the files root URl to link if None.

Parameters:

Name Type Description Default
cls

Class.

required
values

Instance initialization values.

required

Returns:

Type Description

Values with required modifications.

Source code in yaml2rss/domain/template.py
@root_validator(pre=True)
@classmethod
def validate_files_root_url(cls, values):
    """Set the files root URl to `link` if None.

    Arguments:
        cls: Class.
        values: Instance initialization values.

    Returns:
        Values with required modifications.
    """
    if "files_root_url" not in values or values["files_root_url"] is None:
        values["files_root_url"] = values["link"]

    return values

validate_image_title(values) classmethod

Set the episode author to the feed author if None.

Parameters:

Name Type Description Default
cls

Class.

required
values

Instance initialization values.

required

Returns:

Type Description

Values with required modifications.

Source code in yaml2rss/domain/template.py
@root_validator(pre=True)
@classmethod
def validate_image_title(cls, values):
    """Set the episode author to the feed author if None.

    Arguments:
        cls: Class.
        values: Instance initialization values.

    Returns:
        Values with required modifications.
    """
    if "image_title" not in values or values["image_title"] is None:
        values["image_title"] = values["title"]

    return values

SeasonConfig (BaseModel) pydantic-model

Season configuration.

Parameters:

Name Type Description Default
episodes

List of season episodes configuration.

required
Source code in yaml2rss/domain/template.py
class SeasonConfig(BaseModel):
    """Season configuration.

    Arguments:
        episodes: List of season episodes configuration.
    """

    episodes: Sequence[EpisodeConfig]

TemplateConfig (BaseModel) pydantic-model

Generic feed template configuration.

Attributes:

Name Type Description
title str

Feed title.

description str

Feed description/summary.

link HttpUrl

Feed home page link (base URL).

feed_path Path

Path of the feed XML file relative to link.

author str

Feed author.

email EmailStr

Feed author email.

copyright str

Feed copyright notice.

language str

Feed language two-letter code.

last_build_date datetime

Feed last build date.

seaons

Feed seasons configuration.

Source code in yaml2rss/domain/template.py
class TemplateConfig(BaseModel):
    """Generic feed template configuration.

    Attributes:
        title: Feed title.
        description: Feed description/summary.
        link: Feed home page link (base URL).
        feed_path: Path of the feed XML file relative to `link`.
        author: Feed author.
        email: Feed author email.
        copyright: Feed copyright notice.
        language: Feed language two-letter code.
        last_build_date: Feed last build date.
        seaons: Feed seasons configuration.
    """

    title: str
    description: str
    link: HttpUrl
    feed_path: Path
    author: str
    email: EmailStr
    copyright: str = None
    language: str = "en"
    last_build_date: datetime = datetime.now()
    seasons: SeasonsConfig

    @root_validator(pre=True)
    @classmethod
    def validate_copyright(cls, values):
        """Set the copyright notice if None.

        Arguments:
            cls: Class.
            values: Instance initialization values.

        Returns:
            Values with required modifications.
        """
        if "copyright" not in values or values["copyright"] is None:
            values["copyright"] = f"Copyright {values['author']}."

        return values

    @root_validator(pre=True)
    @classmethod
    def validate_episode_author(cls, values):
        """Set the episode author to the feed author if None.

        Arguments:
            cls: Class.
            values: Instance initialization values.

        Returns:
            Values with required modifications.
        """
        for season in values["seasons"]:
            for episode in season["episodes"]:
                if "author" not in episode or episode["author"] is None:
                    episode["author"] = values["author"]

        return values

Set the copyright notice if None.

Parameters:

Name Type Description Default
cls

Class.

required
values

Instance initialization values.

required

Returns:

Type Description

Values with required modifications.

Source code in yaml2rss/domain/template.py
@root_validator(pre=True)
@classmethod
def validate_copyright(cls, values):
    """Set the copyright notice if None.

    Arguments:
        cls: Class.
        values: Instance initialization values.

    Returns:
        Values with required modifications.
    """
    if "copyright" not in values or values["copyright"] is None:
        values["copyright"] = f"Copyright {values['author']}."

    return values

validate_episode_author(values) classmethod

Set the episode author to the feed author if None.

Parameters:

Name Type Description Default
cls

Class.

required
values

Instance initialization values.

required

Returns:

Type Description

Values with required modifications.

Source code in yaml2rss/domain/template.py
@root_validator(pre=True)
@classmethod
def validate_episode_author(cls, values):
    """Set the episode author to the feed author if None.

    Arguments:
        cls: Class.
        values: Instance initialization values.

    Returns:
        Values with required modifications.
    """
    for season in values["seasons"]:
        for episode in season["episodes"]:
            if "author" not in episode or episode["author"] is None:
                episode["author"] = values["author"]

    return values

render(template, template_config)

Render the template with the passed configuration.

Parameters:

Name Type Description Default
template Template

Jinja template.

required
template_config TemplateConfig

Template variable values to replace.

required

Returns:

Type Description
str

Rendered template.

Source code in yaml2rss/domain/template.py
def render(template: jinja2.Template, template_config: TemplateConfig) -> str:
    """Render the template with the passed configuration.

    Arguments:
        template: Jinja template.
        template_config: Template variable values to replace.

    Returns:
        Rendered template.
    """
    return template.render(template_config)

Last update: 2022-07-27