summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Kluyver <takowl@gmail.com>2012-07-26 00:00:54 +0100
committerThomas Kluyver <takowl@gmail.com>2012-07-26 00:00:54 +0100
commit2780bc434aac3817097a5a09b0efd93d2c373a02 (patch)
tree28ab94a89bba0fbd49598f349031acafa5c948d8
parentddd7a41027420a81e8976050052e92ebc1fad506 (diff)
Fix sorting menus with non-ASCII entries on Python 2.
Closes fd.o bug #52492
-rw-r--r--test/test-menu.py29
-rw-r--r--xdg/Menu.py14
2 files changed, 32 insertions, 11 deletions
diff --git a/test/test-menu.py b/test/test-menu.py
index d31fe1a..7f3df3b 100644
--- a/test/test-menu.py
+++ b/test/test-menu.py
@@ -1,5 +1,7 @@
#!/usr/bin/python
+from __future__ import print_function
+import io
import os.path
import shutil
import sys
@@ -11,19 +13,19 @@ import xdg.DesktopEntry
import resources
def show_menu(menu, depth = 0):
-# print depth*"-" + "\x1b[01m" + menu.getName().encode("ascii", 'ignore') + "\x1b[0m"
-# depth += 1
+ print(depth*"-" + "\x1b[01m" + menu.getName() + "\x1b[0m")
+ depth += 1
for entry in menu.getEntries():
if isinstance(entry, xdg.Menu.Menu):
show_menu(entry, depth)
elif isinstance(entry, xdg.Menu.MenuEntry):
-# print depth*"-" + entry.DesktopEntry.getName().encode("ascii", 'ignore')
- print(menu.getPath() + "/\t" + entry.DesktopFileID + "\t" + entry.DesktopEntry.getFileName())
-# elif isinstance(entry, xdg.Menu.Separator):
-# print depth*"-" + "|||"
-# elif isinstance(entry, xdg.Menu.Header):
-# print depth*"-" + "\x1b[01m" + entry.Name + "\x1b[0m"
-# depth -= 1
+ print(depth*"-" + entry.DesktopEntry.getName())
+ print(depth*" " + menu.getPath(), entry.DesktopFileID, entry.DesktopEntry.getFileName())
+ elif isinstance(entry, xdg.Menu.Separator):
+ print(depth*"-" + "|||")
+ elif isinstance(entry, xdg.Menu.Header):
+ print(depth*"-" + "\x1b[01m" + entry.Name + "\x1b[0m")
+ depth -= 1
class MenuTest(unittest.TestCase):
def setUp(self):
@@ -44,3 +46,12 @@ class MenuTest(unittest.TestCase):
menu.getGenericName()
menu.getComment()
menu.getIcon()
+
+ def test_unicode_menuentry(self):
+ test_file = os.path.join(self.tmpdir, "unicode.desktop")
+ with io.open(test_file, 'w', encoding='utf-8') as f:
+ f.write(resources.unicode_desktop)
+
+ entry = xdg.Menu.MenuEntry(test_file)
+ assert entry == entry
+ assert not entry < entry
diff --git a/xdg/Menu.py b/xdg/Menu.py
index ead2654..0d4ad30 100644
--- a/xdg/Menu.py
+++ b/xdg/Menu.py
@@ -23,12 +23,22 @@ import subprocess
from xdg.BaseDirectory import *
from xdg.DesktopEntry import *
from xdg.Exceptions import *
+from xdg.util import PY3
import xdg.Locale
import xdg.Config
ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE
+def _strxfrm(s):
+ """Wrapper around locale.strxfrm that accepts unicode strings on Python 2.
+
+ See Python bug #2481.
+ """
+ if (not PY3) and isinstance(s, unicode):
+ s = s.encode('utf-8')
+ return locale.strxfrm(s)
+
class Menu:
"""Menu containing sub menus under menu.Entries
@@ -104,7 +114,7 @@ class Menu:
def _key(self):
"""Key function for locale-aware sorting."""
- return locale.strxfrm(self.getName())
+ return _strxfrm(self.getName())
def __lt__(self, other):
try:
@@ -480,7 +490,7 @@ class MenuEntry:
def _key(self):
"""Key function for locale-aware sorting."""
- return locale.strxfrm(self.DesktopEntry.getName())
+ return _strxfrm(self.DesktopEntry.getName())
def __lt__(self, other):
try: