summary refs log tree commit diff stats
diff options
context:
space:
mode:
authornatemaia <natemaia10@gmail.com>2018-09-05 20:32:04 -0700
committernatemaia <natemaia10@gmail.com>2018-09-05 20:32:04 -0700
commit573bc3aff2c6881928fae984280ff03ad5360fe7 (patch)
tree530c445f34e410178510d2058d052eec88f856c6
parentd0f4e2c56af9e0e6452289fd136bb3c9934af225 (diff)
downloadranger-573bc3aff2c6881928fae984280ff03ad5360fe7.tar.gz
Remove extra pipe in subprocess call, rename plugin and add original
-rw-r--r--examples/plugin_pmount.py66
-rw-r--r--examples/plugin_pmount_dynamic.py70
2 files changed, 89 insertions, 47 deletions
diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py
index f5bd1c67..38c4a17c 100644
--- a/examples/plugin_pmount.py
+++ b/examples/plugin_pmount.py
@@ -3,66 +3,38 @@
 # This plugin creates a bunch of keybindings used to mount and unmount
 # the devices using pmount(1).
 #
-# (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
+# 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
 
 from __future__ import (absolute_import, division, print_function)
 
-import subprocess
 import ranger.api
 
 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))
-
-    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
+    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":
             fm.execute_console(
-                "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))
+                "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)
 
diff --git a/examples/plugin_pmount_dynamic.py b/examples/plugin_pmount_dynamic.py
new file mode 100644
index 00000000..1448e15b
--- /dev/null
+++ b/examples/plugin_pmount_dynamic.py
@@ -0,0 +1,70 @@
+# Tested with ranger 1.7.2
+#
+# This plugin creates a bunch of keybindings used to mount and unmount
+# the devices using pmount(1).
+#
+# (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 subprocess
+import ranger.api
+
+MOUNT_KEY = '<alt>m'
+UMOUNT_KEY = '<alt>M'
+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))
+
+    diskcmd = "lsblk -lno NAME | awk '!/[1-9]/ {sub(/sd/, \"\"); print}'"
+    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} 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)
+
+
+ranger.api.hook_init = hook_init