diff options
author | Wojciech Siewierski <wojciech.siewierski@onet.pl> | 2018-09-07 23:59:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-07 23:59:13 +0200 |
commit | 7a8c7e6f486268e91c40617684315090a193d78e (patch) | |
tree | 367261fcb9a2ec9350ba18e6dc154e4b0c426717 | |
parent | 4e7d402f4f58350d9bd3f553c572cbffcc7caf74 (diff) | |
parent | e79b08089ba6d91c18905e58f7c4749300473b98 (diff) | |
download | ranger-7a8c7e6f486268e91c40617684315090a193d78e.tar.gz |
Merge pull request #1228 from natemaia/patch-4
Update plugin_pmount.py
-rw-r--r-- | examples/plugin_pmount_dynamic.py | 70 |
1 files changed, 70 insertions, 0 deletions
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 |