add json validity checks and remove key constants

This commit is contained in:
Alexander Lutsai 2021-04-26 16:26:41 +03:00
parent 29f73fa456
commit be187c9079

27
menu.py
View File

@ -6,6 +6,7 @@
# Description: This script draws menu to choose, mount and unmount drives # Description: This script draws menu to choose, mount and unmount drives
import curses import curses
import curses.ascii
import subprocess import subprocess
import json import json
@ -22,6 +23,9 @@ class ChoosePartition:
self.screen = curses.initscr() self.screen = curses.initscr()
curses.start_color() curses.start_color()
curses.curs_set(0) curses.curs_set(0)
curses.noecho()
curses.cbreak()
self.screen.keypad(True)
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
self.selected_partn = 1 self.selected_partn = 1
self._read_partitions() self._read_partitions()
@ -62,8 +66,13 @@ class ChoosePartition:
partn = 0 partn = 0
i = 0 i = 0
if 'blockdevices' not in self.blkinfo:
raise Exception('Wrong lsblk json format. No field "blockdevices"')
for bd in self.blkinfo['blockdevices']: for bd in self.blkinfo['blockdevices']:
i += 1 i += 1
if 'model' not in bd or 'size' not in bd or 'name' not in bd:
raise Exception(
'Wrong lsblk json format. No model, size or name in the blockdevice')
model = bd['model'] if bd['model'] is not None else "" model = bd['model'] if bd['model'] is not None else ""
size = bd['size'] if bd['size'] is not None else "" size = bd['size'] if bd['size'] is not None else ""
self.screen.addstr(2 + i, 2, bd['name'] + " " + model + " " + size) self.screen.addstr(2 + i, 2, bd['name'] + " " + model + " " + size)
@ -73,8 +82,16 @@ class ChoosePartition:
i += 1 i += 1
partn += 1 partn += 1
is_selected = 0 if self.selected_partn != partn else 1 is_selected = 0 if self.selected_partn != partn else 1
lab = part['label'] if part['label'] is not None else part['partlabel'] lab = ""
lab = lab if lab is not None else part['parttypename'] if 'label' not in part and part['label'] is not None:
lab = part['label']
elif 'partlabel' not in part and part['partlabel'] is not None:
lab = part['partlabel']
elif 'parttypename' not in part and part['parttypename'] is not None:
lab = part['parttypename']
if 'mountpoint' not in part:
raise Exception('Wrong lsblk json format. No mountpoint')
mp = part['mountpoint'] if part['mountpoint'] is not None else "Not mounted" mp = part['mountpoint'] if part['mountpoint'] is not None else "Not mounted"
s = "{name:<12} {size:<8} {label:<16} {mp}".format( s = "{name:<12} {size:<8} {label:<16} {mp}".format(
@ -107,15 +124,15 @@ class ChoosePartition:
sel = None sel = None
x = 0 x = 0
# quit when pressed `q` or `Esc` or `Ctrl+g` # quit when pressed `q` or `Esc` or `Ctrl+g`
while x != ord('q') and x != 27 and x != 7: while x not in (ord('q'), curses.ascii.ESC, curses.ascii.BEL):
self._select_print(x) self._select_print(x)
x = self.screen.getch() x = self.screen.getch()
if x == ord('j') or x == 66 or x == 14: if x in (ord('j'), curses.ascii.SO, curses.KEY_DOWN):
# down # down
self.selected_partn += 1 self.selected_partn += 1
if self.selected_partn > self.partn: if self.selected_partn > self.partn:
self.selected_partn = self.partn self.selected_partn = self.partn
elif x == ord('k') or x == 65 or x == 16: elif x in (ord('k'), curses.ascii.DLE, curses.KEY_UP):
# up # up
self.selected_partn -= 1 self.selected_partn -= 1
if self.selected_partn <= 0: if self.selected_partn <= 0: