Make methods private,

Fix error case when someone tries to delete swap even if it doesnt exist in the first place, Fix typo
This commit is contained in:
Lucas Noki 2020-01-23 12:06:00 +01:00
parent 82ec233680
commit 5d519f2380

View File

@ -12,14 +12,14 @@ class SwapModule(AbstractModule):
def run(self): def run(self):
actions = { actions = {
"Create/Resize temp. swap": self.create_resize_swap, "Create/Resize temp. swap": self._create_resize_swap,
"Create/Resize persistent swap": self.create_resize_peristent_swap, "Create/Resize persistent swap": self._create_resize_persistent_swap,
"Delete swap": self.delete_swap, "Delete swap": self._delete_swap,
"Make temp. swap persistent": self.make_swap_persistent, "Make temp. swap persistent": self._make_swap_persistent,
"Get swap location": self.swap_location_check, "Get swap location": self._swap_location_check,
"Get swap size": self.get_swap_size, "Get swap size": self._get_swap_size,
"Show swapiness": self.show_swapiness, "Show swapiness": self._show_swapiness,
"Adjust temp. swapiness": self.adjust_swapiness_temp "Adjust temp. swapiness": self._adjust_swapiness_temp
} }
menu = [ menu = [
{ {
@ -32,11 +32,11 @@ class SwapModule(AbstractModule):
selected_action = prompt(menu)['action'] selected_action = prompt(menu)['action']
actions[selected_action]() actions[selected_action]()
def create_resize_peristent_swap(self): def _create_resize_persistent_swap(self):
self.create_resize_swap() self._create_resize_swap()
self.make_swap_persistent() self._make_swap_persistent()
def swap_location_check(self): def _swap_location_check(self):
output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8") output_swaps = subprocess.check_output(['cat', '/proc/swaps']).decode("UTF-8")
final_list = list(output_swaps.split()) final_list = list(output_swaps.split())
try: try:
@ -44,9 +44,9 @@ class SwapModule(AbstractModule):
self.swap_file = final_list[5] self.swap_file = final_list[5]
except IndexError: except IndexError:
print("Swap file doesn´t exist!") print("Swap file doesn´t exist!")
return None return "NoSwap"
def get_swap_size(self): def _get_swap_size(self):
swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8") swap_size = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
final_list = list(swap_size.split()) final_list = list(swap_size.split())
try: try:
@ -54,7 +54,7 @@ class SwapModule(AbstractModule):
except IndexError: except IndexError:
print("Swap file doesn´t exist!") print("Swap file doesn´t exist!")
def create_resize_swap(self): def _create_resize_swap(self):
output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8") output_swapon = subprocess.check_output(['swapon', '--show']).decode("UTF-8")
if not output_swapon: if not output_swapon:
size = input("How big should the swap be (numbers in Gigabyte)? ") size = input("How big should the swap be (numbers in Gigabyte)? ")
@ -82,8 +82,8 @@ class SwapModule(AbstractModule):
output_free = subprocess.check_output(['free', '-h']).decode("UTF-8") output_free = subprocess.check_output(['free', '-h']).decode("UTF-8")
print(output_free.strip()) print(output_free.strip())
def make_swap_persistent(self): def _make_swap_persistent(self):
self.swap_location_check() self._swap_location_check()
backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak" backup_fstab = "sudo cp /etc/fstab /etc/fstab.bak"
enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(self.swap_file) enable_persistence = "echo '{} none swap sw 0 0' | sudo tee -a /etc/fstab".format(self.swap_file)
@ -91,12 +91,12 @@ class SwapModule(AbstractModule):
subprocess.call(enable_persistence, shell=True) subprocess.call(enable_persistence, shell=True)
print("Swap is now persistent!") print("Swap is now persistent!")
def show_swapiness(self): def _show_swapiness(self):
get_swapiness = "cat /proc/sys/vm/swappiness" get_swapiness = "cat /proc/sys/vm/swappiness"
subprocess.call(get_swapiness, shell=True) subprocess.call(get_swapiness, shell=True)
def adjust_swapiness_temp(self): def _adjust_swapiness_temp(self):
actions = { actions = {
"Light": 25, "Light": 25,
"Default": 60, "Default": 60,
@ -116,9 +116,9 @@ class SwapModule(AbstractModule):
subprocess.call(adjust, shell=True) subprocess.call(adjust, shell=True)
print("Temporary swapiness is " + str(actions[selected_swapiness])) print("Temporary swapiness is " + str(actions[selected_swapiness]))
def delete_swap(self): def _delete_swap(self):
location = self.swap_location_check() location = self._swap_location_check()
if location is None: if location == "NoSwap":
return None return None
else: else:
disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \ disable_swapfile = "sudo swapoff {} && ".format(self.swap_file) + \