about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--examples/plugin_pmount.py67
1 files changed, 47 insertions, 20 deletions
diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py
index 38c4a17c..feb54c83 100644
--- a/examples/plugin_pmount.py
+++ b/examples/plugin_pmount.py
@@ -3,38 +3,65 @@
 # This plugin creates a bunch of keybindings used to mount and unmount
 # the devices using pmount(1).
 #
-# alt+m       <letter>            <digit>: mount /dev/sd<letter><digit>
-# alt+m       <uppercase letter>         : mount /dev/sd<letter>
-# alt+shift+m <letter>            <digit>: unmount /dev/sd<letter><digit>
-# alt+shift+m <uppercase letter>         : unmount /dev/sd<letter>
-# alt+shift+n                            : list the devices
+# (multiple partitions): alt+m <letter> <digit>  : mount /dev/sd<letter><digit>
+# (one partition):       alt+m <letter>          : mount /dev/sd<letter>1
+# (no partitions):       alt+m <letter>          : mount /dev/sd<letter>
+#
+# (multiple partitions): alt+M <letter> <digit>  : unmount /dev/sd<letter><digit>
+# (one partition):       alt+M <letter>          : unmount /dev/sd<letter>1
+# (no partitions):       alt+M <letter>          : unmount /dev/sd<letter>
+#
+# alt+n : list the devices
 
 from __future__ import (absolute_import, division, print_function)
 
 import ranger.api
+import subprocess
 
 MOUNT_KEY = '<alt>m'
 UMOUNT_KEY = '<alt>M'
-LIST_MOUNTS_KEY = '<alt>N'
-
-
+LIST_MOUNTS_KEY = '<alt>n'
 HOOK_INIT_OLD = ranger.api.hook_init
 
-
 def hook_init(fm):
     fm.execute_console("map {key} shell -p lsblk".format(key=LIST_MOUNTS_KEY))
-    for disk in "abcdefgh":
-        fm.execute_console("map {key}{0} chain shell pmount sd{1}; cd /media/sd{1}".format(
-            disk.upper(), disk, key=MOUNT_KEY))
-        fm.execute_console("map {key}{0} chain cd; chain shell pumount sd{1}".format(
-            disk.upper(), disk, key=UMOUNT_KEY))
-        for part in "123456789":
+
+    diskcmd = "lsblk -lno NAME | grep -w 'sd[a-z]' | sed 's/sd//'"
+    disks = subprocess.check_output(diskcmd,
+            shell=True).decode('utf-8').replace('\r', '').replace('\n', '')
+
+    for disk in disks:
+        partcmd = "lsblk -lno NAME /dev/sd{0} | sed 's/sd{0}//' | tail -n 1".format(disk)
+        
+        try:
+            numparts = int(subprocess.check_output(partcmd,
+                shell=True).decode('utf-8').replace('\r', '').replace('\n', ''))
+        except ValueError:
+            numparts = 0
+
+        if numparts == 0:
+            # no partition, mount the whole device
+            fm.execute_console("map {key}{0} chain shell pmount sd{0}; cd /media/sd{0}".format(
+                disk, key=MOUNT_KEY))
+            fm.execute_console("map {key}{0} chain cd; chain shell pumount sd{0}".format(
+                disk, key=UMOUNT_KEY))
+            
+        elif numparts == 1:
+            # only one partition, mount the partition
             fm.execute_console(
-                "map {key}{0}{1} chain shell pmount sd{0}{1}; cd /media/sd{0}{1}".format(
-                    disk, part, key=MOUNT_KEY)
-            )
-            fm.execute_console("map {key}{0}{1} chain cd; shell pumount sd{0}{1}".format(
-                disk, part, key=UMOUNT_KEY))
+                    "map {key}{0} chain shell pmount sd{0}1; cd /media/sd{0}1".format(
+                        disk, key=MOUNT_KEY))
+            fm.execute_console("map {key}{0} chain cd; shell pumount sd{0}1".format(
+                disk, key=UMOUNT_KEY))
+        
+        else:
+            # use range start 1, /dev/sd{device}0 doesn't exist
+            for part in range(1, numparts + 1):
+                fm.execute_console(
+                    "map {key}{0}{1} chain shell pmount sd{0}{1}; cd /media/sd{0}{1}".format(
+                        disk, part, key=MOUNT_KEY))
+                fm.execute_console("map {key}{0}{1} chain cd; shell pumount sd{0}{1}".format(
+                    disk, part, key=UMOUNT_KEY))
 
     return HOOK_INIT_OLD(fm)