[gnome-music] query: Ignore punctuation when sorting albums & artists



commit b3314453bab047330a4a660126f8f29b594bd5fb
Author: Divyanshu Vishwakarma <divyanshu divix gmail com>
Date:   Thu Jul 21 01:45:30 2016 +0530

    query: Ignore punctuation when sorting albums & artists
    
    Makes use of the new regex support in Tracker 1.9: use fn:replace
    with a regex to remove punctuation at beginning and end of album/artist
    strings. Bump minimum required tracker version to 1.9. PEP-8 and PEP-257
    fixes.
    
    Based on work by Kevin Haller.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742531

 configure.ac        |    2 +-
 gnomemusic/query.py |   23 ++++++++++++++++++-----
 2 files changed, 19 insertions(+), 6 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index e91018c..3ecc22d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -37,7 +37,7 @@ PKG_CHECK_MODULES(MEDIAART, [libmediaart-2.0])
 PYGOBJECT_MIN_VERSION=3.21.1
 PKG_CHECK_MODULES(PYGOBJECT, [pygobject-3.0 >= $PYGOBJECT_MIN_VERSION])
 
-TRACKER_MIN_VERSION=1.8.0
+TRACKER_MIN_VERSION=1.9.0
 PKG_CHECK_MODULES(TRACKER, [tracker-sparql-1.0 >= $TRACKER_MIN_VERSION])
 
 GLIB_COMPILE_RESOURCES=`$PKG_CONFIG --variable glib_compile_resources gio-2.0`
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index ca0d3fa..812efef 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -34,8 +34,9 @@ logger = logging.getLogger(__name__)
 
 import time
 sparql_midnight_dateTime_format = "%Y-%m-%dT00:00:00Z"
-SECONDS_PER_DAY = 86400
 
+SECONDS_PER_DAY = 86400
+PUNCTUATION_FILTER = " !\\\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~"
 
 class Query():
 
@@ -65,10 +66,22 @@ class Query():
 
     @staticmethod
     def order_by_statement(attr):
-        """Returns a SPARQL ORDER BY statement sorting by the given attribute, ignoring
-            articles as defined in _("the"). 'Attr' should be given without parentheses,
-            e.g., "attr='?author'"."""
-        return_statement = "fn:lower-case(%(attribute)s)" % {'attribute': attr}
+        """Returns a specifically sorted SPARQL ORDER BY statement.
+
+        Returns a SPARQL ORDER BY statement sorting by the given
+        attribute, ignoring articles as defined in _("the") as well as
+        the punctuation at the start and the end of the string.
+
+        :param str attr: The attribute to order by
+        :return: The sparql order by statement
+        :rtype: str
+        """
+        return_statement = """fn:replace(fn:lower-case(%(attribute)s),
+        "^[%(punctuation)s]+|[%(punctuation)s]+$", "")
+        """.replace('\n', ' ').strip() % {
+            'attribute': attr,
+            'punctuation': PUNCTUATION_FILTER
+        }
         # TRANSLATORS: _("the") should be a space-separated list of all-lowercase articles
         # (such as 'the') that should be ignored when alphabetizing artists/albums. This
         # list should include 'the' regardless of language. If some articles occur more


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]