add power off drive functionality

This commit is contained in:
Alexander Lutsai 2021-06-12 15:52:06 +03:00
parent a7548bee17
commit 2fa1218ca0
2 changed files with 22 additions and 5 deletions

View file

@ -34,4 +34,5 @@ Enter in ranger `:mount`, than will be shown menu. In this menu you can press:
- `m` to mount selected partition - `m` to mount selected partition
- `u` to unmount selected partition - `u` to unmount selected partition
- `e` to unmount all partitions of selected partition's drive - `e` to unmount all partitions of selected partition's drive
- `ENTER` to open selected mountpoint in current tab - `p` to power off selected the selected partition's drive (also called safely remove). It can be done only when all partitions of the drive are unmounted
- `ENTER` to open selected mountpoint in current tab of the ranger

24
menu.py
View file

@ -18,10 +18,8 @@ class ChoosePartition:
selected_partn = 1 selected_partn = 1
selected_mountpoint = None selected_mountpoint = None
partn = 1 partn = 1
help_message = [("Press 'm' to mount, " + help_message = ["Press 'm' to mount, 'u' to unmount, 'g' to refresh",
"'u' to unmount, " + " and 'e' to unmount, 'p' to poweroff drive, 'enter' to cd"]
"'g' to refresh"),
" and 'e' to unmount, 'enter' to cd"]
message = "" message = ""
def __init__(self): def __init__(self):
@ -62,6 +60,17 @@ class ChoosePartition:
return part return part
return None return None
def _get_drive_by_partn(self):
partn = 0
for bd in self.blkinfo['blockdevices']:
if 'children' not in bd:
continue
for part in bd['children']:
partn += 1
if self.selected_partn == partn:
return bd
return None
def _select_print_part(self, part, is_selected, i): def _select_print_part(self, part, is_selected, i):
if not ('mountpoint' in part and if not ('mountpoint' in part and
'name' in part and 'name' in part and
@ -175,6 +184,10 @@ class ChoosePartition:
sel = self._get_part_by_partn() sel = self._get_part_by_partn()
if sel is not None: if sel is not None:
self.unmount(sel['path']) self.unmount(sel['path'])
elif x == ord('p'):
sel_drive = self._get_drive_by_partn()
if sel_drive is not None:
self.poweroff(sel_drive['path'])
elif x == ord('g') or x == ord('r'): elif x == ord('g') or x == ord('r'):
self._read_partitions() self._read_partitions()
curses.endwin() curses.endwin()
@ -197,6 +210,9 @@ class ChoosePartition:
def unmount(self, dev): def unmount(self, dev):
self._udisk_mount_unmount("unmount", dev) self._udisk_mount_unmount("unmount", dev)
def poweroff(self, dev):
self._udisk_mount_unmount("power-off", dev)
def mount(self, dev): def mount(self, dev):
self._udisk_mount_unmount("mount", dev) self._udisk_mount_unmount("mount", dev)