From 970d2ae77f7988e4af0dc604ff6444306f5d9be4 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 7 Jul 2018 18:08:22 -0700 Subject: Update plugin_pmount.py Currently the pmount plugin uses static device letters and partition numbers. this change uses present devices and partitions. - Changes the binds to remove the need for uppercase device letters, if a device has no partitions mounts/unmounts the device. - With only one partition the same binds will handle it instead of the whole device. - With greater than one partitions, map for each. This is still not fully functional, devices not present when ranger is started *(usb drives)* won't have a map created unless ranger is restarted :| ... Is there a simple way to have ranger 'reload' this plugin, or use a different hook, etc.? --- examples/plugin_pmount.py | 67 +++++++++++++++++++++++++++++++++-------------- 1 file 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 : mount /dev/sd -# alt+m : mount /dev/sd -# alt+shift+m : unmount /dev/sd -# alt+shift+m : unmount /dev/sd -# alt+shift+n : list the devices +# (multiple partitions): alt+m : mount /dev/sd +# (one partition): alt+m : mount /dev/sd1 +# (no partitions): alt+m : mount /dev/sd +# +# (multiple partitions): alt+M : unmount /dev/sd +# (one partition): alt+M : unmount /dev/sd1 +# (no partitions): alt+M : unmount /dev/sd +# +# alt+n : list the devices from __future__ import (absolute_import, division, print_function) import ranger.api +import subprocess MOUNT_KEY = 'm' UMOUNT_KEY = 'M' -LIST_MOUNTS_KEY = 'N' - - +LIST_MOUNTS_KEY = '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) -- cgit 1.4.1-2-gfad0 From 58bcf8691d522d658e8b4a1c0389d5bb53c3d1aa Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 7 Jul 2018 19:01:13 -0700 Subject: Indentation and whitespace cleanup --- examples/plugin_pmount.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py index feb54c83..f7e1bd3c 100644 --- a/examples/plugin_pmount.py +++ b/examples/plugin_pmount.py @@ -27,15 +27,13 @@ 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', '') + 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', '')) + numparts = int(subprocess.check_output(partcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) except ValueError: numparts = 0 @@ -48,18 +46,16 @@ def hook_init(fm): 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 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 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)) -- cgit 1.4.1-2-gfad0 From 0e4577ea8ac0ea99e2c44164e2e81182d1ec4d07 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 7 Jul 2018 19:07:30 -0700 Subject: import order and break long lines --- examples/plugin_pmount.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py index f7e1bd3c..3079b5a3 100644 --- a/examples/plugin_pmount.py +++ b/examples/plugin_pmount.py @@ -15,8 +15,8 @@ from __future__ import (absolute_import, division, print_function) -import ranger.api import subprocess +import ranger.api MOUNT_KEY = 'm' UMOUNT_KEY = 'M' @@ -27,13 +27,15 @@ 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', '') + 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', '')) + numparts = int(subprocess.check_output( + partcmd, shell=True).decode('utf-8').replace('\r', '').replace('\n', '')) except ValueError: numparts = 0 @@ -43,19 +45,21 @@ def hook_init(fm): 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 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 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)) -- cgit 1.4.1-2-gfad0 From d0f4e2c56af9e0e6452289fd136bb3c9934af225 Mon Sep 17 00:00:00 2001 From: Nathaniel Date: Sat, 7 Jul 2018 19:10:47 -0700 Subject: 2 lines around function definition --- examples/plugin_pmount.py | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/plugin_pmount.py b/examples/plugin_pmount.py index 3079b5a3..f5bd1c67 100644 --- a/examples/plugin_pmount.py +++ b/examples/plugin_pmount.py @@ -23,6 +23,7 @@ UMOUNT_KEY = 'M' LIST_MOUNTS_KEY = '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)) -- cgit 1.4.1-2-gfad0 From 573bc3aff2c6881928fae984280ff03ad5360fe7 Mon Sep 17 00:00:00 2001 From: natemaia Date: Wed, 5 Sep 2018 20:32:04 -0700 Subject: Remove extra pipe in subprocess call, rename plugin and add original --- examples/plugin_pmount.py | 66 +++++++++++------------------------- examples/plugin_pmount_dynamic.py | 70 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 47 deletions(-) create mode 100644 examples/plugin_pmount_dynamic.py 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 : mount /dev/sd -# (one partition): alt+m : mount /dev/sd1 -# (no partitions): alt+m : mount /dev/sd -# -# (multiple partitions): alt+M : unmount /dev/sd -# (one partition): alt+M : unmount /dev/sd1 -# (no partitions): alt+M : unmount /dev/sd -# -# alt+n : list the devices +# alt+m : mount /dev/sd +# alt+m : mount /dev/sd +# alt+shift+m : unmount /dev/sd +# alt+shift+m : unmount /dev/sd +# alt+shift+n : list the devices from __future__ import (absolute_import, division, print_function) -import subprocess import ranger.api MOUNT_KEY = 'm' UMOUNT_KEY = 'M' -LIST_MOUNTS_KEY = 'n' +LIST_MOUNTS_KEY = '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 : mount /dev/sd +# (one partition): alt+m : mount /dev/sd1 +# (no partitions): alt+m : mount /dev/sd +# +# (multiple partitions): alt+M : unmount /dev/sd +# (one partition): alt+M : unmount /dev/sd1 +# (no partitions): alt+M : unmount /dev/sd +# +# alt+n : list the devices + +from __future__ import (absolute_import, division, print_function) + +import subprocess +import ranger.api + +MOUNT_KEY = 'm' +UMOUNT_KEY = 'M' +LIST_MOUNTS_KEY = '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 -- cgit 1.4.1-2-gfad0