diff options
author | Nathaniel <natemaia10@gmail.com> | 2018-07-07 18:08:22 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-07 18:08:22 -0700 |
commit | 970d2ae77f7988e4af0dc604ff6444306f5d9be4 (patch) | |
tree | 160c4ece547f985eb24a98bcf86cb045ba8051d0 /examples | |
parent | a523b1deaf1add8d105082720bf0ed9324eb69da (diff) | |
download | ranger-970d2ae77f7988e4af0dc604ff6444306f5d9be4.tar.gz |
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 <alt><m or M><device letter> 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 <alt><m or M><device letter><partition number> 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.?
Diffstat (limited to 'examples')
-rw-r--r-- | examples/plugin_pmount.py | 67 |
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) |