summaryrefslogtreecommitdiff
path: root/gnomemusic/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'gnomemusic/utils.py')
-rw-r--r--gnomemusic/utils.py52
1 files changed, 30 insertions, 22 deletions
diff --git a/gnomemusic/utils.py b/gnomemusic/utils.py
index 5d3262a0..208f8707 100644
--- a/gnomemusic/utils.py
+++ b/gnomemusic/utils.py
@@ -23,12 +23,12 @@
# delete this exception statement from your version.
from enum import Enum, IntEnum
+from typing import List, Union
import re
import unicodedata
from gettext import gettext as _
-from gi.repository import Gio, GLib
-from gi._gi import pygobject_new_full
+from gi.repository import Gio, GLib, Gtk
from gnomemusic.musiclogger import MusicLogger
@@ -46,6 +46,18 @@ class ArtSize(Enum):
self.height = height
+class CoreObjectType(Enum):
+ """Indicates the type of the CoreObject passed"""
+ ALBUM = 0
+ ARTIST = 1
+ SONG = 2
+
+
+class DefaultIconType(Enum):
+ ALBUM = "folder-music-symbolic"
+ ARTIST = "music-artist-symbolic"
+
+
class SongStateIcon(Enum):
"""Enum for icons used in song playing and validation"""
ERROR = "dialog-error-symbolic"
@@ -138,13 +150,13 @@ def get_media_year(item):
"""Returns the year when the media was published.
:param Grl.Media item: A Grilo Media object
- :return: The publication year or '----' if not defined
- :rtype: str
+ :return: The publication year or None if not defined
+ :rtype: str or None
"""
date = item.get_publication_date()
if not date:
- return "----"
+ return None
return str(date.get_year())
@@ -160,7 +172,7 @@ def seconds_to_string(duration):
minutes = seconds // 60
seconds %= 60
- return '{:d}∶{:02d}'.format(minutes, seconds)
+ return '{:d}:{:02d}'.format(minutes, seconds)
def normalize_caseless(text):
@@ -173,7 +185,7 @@ def normalize_caseless(text):
return unicodedata.normalize("NFKD", text.casefold())
-def natural_sort_names(name_a, name_b):
+def natural_sort_names(name_a: str, name_b: str) -> int:
"""Natural order comparison of two strings.
A natural order is an alphabetical order which takes into account
@@ -184,22 +196,18 @@ def natural_sort_names(name_a, name_b):
:param str name_a: first string to compare
:param str name_b: second string to compare
- :returns: False if name_a should be before name_b. True otherwise.
- :rtype: boolean
+ :returns: Gtk Ordering
+ :rtype: int
"""
- def _extract_numbers(text):
+ def _extract_numbers(text: str) -> List[Union[int, str]]:
return [int(tmp) if tmp.isdigit() else tmp
for tmp in re.split(r"(\d+)", normalize_caseless(text))]
- return _extract_numbers(name_b) < _extract_numbers(name_a)
-
-
-def wrap_list_store_sort_func(func):
- """PyGI wrapper for SortListModel set_sort_func.
- """
- def wrap(a, b, *user_data):
- a = pygobject_new_full(a, False)
- b = pygobject_new_full(b, False)
- return func(a, b, *user_data)
-
- return wrap
+ extract_a = _extract_numbers(name_a)
+ extract_b = _extract_numbers(name_b)
+ if extract_a < extract_b:
+ return Gtk.Ordering.SMALLER
+ elif extract_a > extract_b:
+ return Gtk.Ordering.LARGER
+ else:
+ return Gtk.Ordering.EQUAL