summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorlverweijen <lverweijen>2016-03-08 19:20:22 +0100
committerlverweijen <lverweijen>2016-03-08 19:29:50 +0100
commitd14d9386c006e5f802285056a42f3173080a1b95 (patch)
tree1d7e36d1f782bfdaa99a82791a39083878e9e216
parent7ee4b45bd2619fb07c9b139940003224bedb1de3 (diff)
downloadranger-d14d9386c006e5f802285056a42f3173080a1b95.tar.gz
Make natural_sort's behaviour better defined
The old version relied on comparing integers to strings which is not
supported in Python 3 anymore and not a good practice in general anyway.
It was also the case that 'hello2' was ordered before 'hello' instead of
after, which I found to be non-intuitive.
-rw-r--r--ranger/container/fsobject.py6
-rw-r--r--tests/ranger/container/test_fsobject.py38
2 files changed, 41 insertions, 3 deletions
diff --git a/ranger/container/fsobject.py b/ranger/container/fsobject.py
index 1daf6d70..db6772ac 100644
--- a/ranger/container/fsobject.py
+++ b/ranger/container/fsobject.py
@@ -30,7 +30,7 @@ else:
     from string import maketrans
 _unsafe_chars = '\n' + ''.join(map(chr, range(32))) + ''.join(map(chr, range(128, 256)))
 _safe_string_table = maketrans(_unsafe_chars, '?' * len(_unsafe_chars))
-_extract_number_re = re.compile(r'(\d+)')
+_extract_number_re = re.compile(r'(\d+|\D)')
 
 def safe_path(path):
     return path.translate(_safe_string_table)
@@ -134,12 +134,12 @@ class FileSystemObject(FileManagerAware, SettingsAware):
 
     @lazy_property
     def basename_natural(self):
-        return [int(s) if s.isdigit() else s \
+        return [('0', int(s)) if s.isdigit() else (s, 0) \
                 for s in _extract_number_re.split(self.relative_path)]
 
     @lazy_property
     def basename_natural_lower(self):
-        return [int(s) if s.isdigit() else s \
+        return [('0', int(s)) if s.isdigit() else (s, 0) \
                 for s in _extract_number_re.split(self.relative_path_lower)]
 
     @lazy_property
diff --git a/tests/ranger/container/test_fsobject.py b/tests/ranger/container/test_fsobject.py
new file mode 100644
index 00000000..305ab626
--- /dev/null
+++ b/tests/ranger/container/test_fsobject.py
@@ -0,0 +1,38 @@
+import pytest
+import operator
+
+from ranger.container.fsobject import FileSystemObject
+
+
+class MockFM(object):
+    """Used to fullfill the dependency by FileSystemObject."""
+
+    default_linemodes = []
+
+
+def create_filesystem_object(path):
+    """Create a FileSystemObject without an fm object."""
+    fso = FileSystemObject.__new__(FileSystemObject)
+    fso.fm = MockFM()
+    fso.__init__(path)
+    return fso
+
+
+def test_basename_natural1():
+    """Test filenames without extensions."""
+    fsos = [create_filesystem_object(path)
+            for path in "hello", "hello1", "hello2"]
+    print("fsos", repr(fsos))
+    print("sorted(fsos)", repr(sorted(fsos[::-1])))
+    assert(fsos == sorted(fsos[::-1], key=operator.attrgetter("basename_natural")))
+    assert(fsos == sorted(fsos[::-1], key=operator.attrgetter("basename_natural_lower")))
+
+
+def test_basename_natural2():
+    """Test filenames with extensions."""
+    fsos = [create_filesystem_object(path)
+            for path in "hello", "hello.txt", "hello1.txt", "hello2.txt"]
+    print("fsos", repr(fsos))
+    print("sorted(fsos)", repr(sorted(fsos[::-1])))
+    assert(fsos == sorted(fsos[::-1], key=operator.attrgetter("basename_natural")))
+    assert(fsos == sorted(fsos[::-1], key=operator.attrgetter("basename_natural_lower")))
ae0fe74381fa0a474d64b0fee94'>b06433bc ^
2c5ea01d ^
b0a216f5 ^

2c5ea01d ^
2c5ea01d ^
582f3519 ^
b06433bc ^
582f3519 ^

94c5d83e ^
dee6cfa6 ^
e9e4b4ff ^
b0a216f5 ^
a082b66a ^

b0a216f5 ^
e9e4b4ff ^





ad75190c ^
e9e4b4ff ^
c7720fff ^



8d21b83c ^


e9e4b4ff ^
25a4162d ^
e9e4b4ff ^
0c2c782d ^
636d9393 ^
b0a216f5 ^
b3bc8431 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78