Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Add RichMarkup mixin #1732

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions plextraktsync/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

from plexapi.exceptions import PlexApiException
from requests import RequestException
from rich.markup import escape
from trakt.errors import TraktException
from trakt.tv import TVShow

from plextraktsync.factory import logger
from plextraktsync.mixin.RichMarkup import RichMarkup
from plextraktsync.trakt.TraktLookup import TraktLookup

if TYPE_CHECKING:
Expand All @@ -21,7 +21,7 @@
from plextraktsync.trakt.types import TraktMedia


class Media:
class Media(RichMarkup):
"""
Class containing Plex and Trakt media items (Movie, Episode)
"""
Expand Down Expand Up @@ -55,7 +55,7 @@ def title_link(self):
if self.plex:
return self.plex.title_link

return f"[green]{escape(self.title)}[/]"
return self.markup_title(self.title)

@cached_property
def media_type(self):
Expand Down
10 changes: 10 additions & 0 deletions plextraktsync/mixin/RichMarkup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from rich.markup import escape


class RichMarkup:
def markup_link(self, link: str, title: str):
return f"[link={link}]{self.markup_title(title)}[/]"

@staticmethod
def markup_title(title: str):
return f"[green]{escape(title)}[/]"
7 changes: 3 additions & 4 deletions plextraktsync/plex/PlexGuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from functools import cached_property
from typing import TYPE_CHECKING

from rich.markup import escape

from plextraktsync.factory import factory
from plextraktsync.mixin.RichMarkup import RichMarkup

if TYPE_CHECKING:
from plextraktsync.plex.PlexLibraryItem import PlexLibraryItem


class PlexGuid:
class PlexGuid(RichMarkup):
def __init__(self, guid: str, type: str, pm: PlexLibraryItem | None = None):
self.guid = guid
self.type = type
Expand Down Expand Up @@ -79,7 +78,7 @@ def title_link(self):
if self.pm:
return self.pm.title_link

return f"[green]{escape(str(self))}[/]"
return self.markup_title(str(self))

def __str__(self):
return f"<PlexGuid:{self.guid}>"
8 changes: 4 additions & 4 deletions plextraktsync/plex/PlexLibraryItem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from functools import cached_property
from typing import TYPE_CHECKING

from rich.markup import escape
from trakt.utils import timestamp

from plextraktsync.decorators.retry import retry
from plextraktsync.factory import factory
from plextraktsync.mixin.RichMarkup import RichMarkup
from plextraktsync.plex.PlexGuid import PlexGuid

if TYPE_CHECKING:
Expand All @@ -18,7 +18,7 @@
from plextraktsync.plex.types import PlexMedia


class PlexLibraryItem:
class PlexLibraryItem(RichMarkup):
def __init__(self, item: PlexMedia, plex: PlexApi = None):
self.item = item
self.plex = plex
Expand Down Expand Up @@ -349,9 +349,9 @@ def title_link(self):
if self.plex:
link = self.plex.media_url(self)

return f"[link={link}][green]{escape(self.title)}[/][/]"
return self.markup_link(link, self.title)

return f"[green]{escape(self.title)}[/]"
return self.markup_title(self.title)

def __repr__(self):
try:
Expand Down
6 changes: 3 additions & 3 deletions plextraktsync/plex/PlexLibrarySection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

from plexapi import X_PLEX_CONTAINER_SIZE
from plexapi.exceptions import NotFound
from rich.markup import escape

from plextraktsync.decorators.retry import retry
from plextraktsync.mixin.RichMarkup import RichMarkup
from plextraktsync.plex.PlexLibraryItem import PlexLibraryItem

if TYPE_CHECKING:
Expand All @@ -16,7 +16,7 @@
from plextraktsync.plex.types import PlexMedia


class PlexLibrarySection:
class PlexLibrarySection(RichMarkup):
def __init__(self, section: ShowSection | MovieSection, plex: PlexApi = None):
self.section = section
self.plex = plex
Expand Down Expand Up @@ -44,7 +44,7 @@ def link(self):

@property
def title_link(self):
return f"[link={self.link}][green]{escape(self.title)}[/][/]"
return self.markup_link(self.link, self.title)

def find_by_title(self, name: str):
try:
Expand Down
9 changes: 4 additions & 5 deletions plextraktsync/plex/PlexPlaylist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from functools import cached_property
from typing import TYPE_CHECKING

from rich.markup import escape

from plextraktsync.decorators.flatten import flatten_dict
from plextraktsync.factory import logging
from plextraktsync.media import Media
from plextraktsync.mixin.RichMarkup import RichMarkup

if TYPE_CHECKING:
from plexapi.playlist import Playlist
Expand All @@ -16,7 +15,7 @@
from plextraktsync.plex.types import PlexMedia


class PlexPlaylist:
class PlexPlaylist(RichMarkup):
def __init__(self, server: PlexServer, name: str):
self.server = server
self.name = name
Expand Down Expand Up @@ -90,9 +89,9 @@ def title_link(self):
if self.playlist is not None:
link = self.playlist._getWebURL()

return f"[link={link}][green]{escape(self.name)}[/][/]"
return self.markup_link(link, self.name)

return f"[green]{escape(self.name)}[/]"
return self.markup_title(self.name)

@staticmethod
def same_list(list_a: list[PlexMedia], list_b: list[PlexMedia]) -> bool:
Expand Down
Loading