index : archinstall32 | |
Archlinux32 installer | gitolite user |
summaryrefslogtreecommitdiff |
author | Daniel <blackrabbit256@gmail.com> | 2022-04-22 21:24:12 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-22 13:24:12 +0200 |
commit | 477b5b120e120766d789a691fce60cec843aff43 (patch) | |
tree | d2580a7510c2c3ffff694d6145ee1b93c1fd6ea6 /archinstall/lib/menu/list_manager.py | |
parent | 2529d6a5f59eb6a16f95bf9d1117a6033c527df9 (diff) |
-rw-r--r-- | archinstall/lib/menu/list_manager.py | 126 |
diff --git a/archinstall/lib/menu/list_manager.py b/archinstall/lib/menu/list_manager.py index 5b4f720c..5da34e06 100644 --- a/archinstall/lib/menu/list_manager.py +++ b/archinstall/lib/menu/list_manager.py @@ -89,10 +89,21 @@ from .text_input import TextInput from .menu import Menu from os import system from copy import copy -from typing import Union +from typing import Union, Any, List, TYPE_CHECKING + +if TYPE_CHECKING: + _: Any + class ListManager: - def __init__(self,prompt :str, base_list :Union[list,dict] ,base_actions :list = None,null_action :str = None, default_action :Union[str,list] = None, header :Union[str,list] = None): + def __init__( + self, + prompt :str, + base_list :Union[list,dict] , + base_actions :list = None, + null_action :str = None, + default_action :Union[str,list] = None, + header :Union[str,list] = None): """ param :prompt Text which will appear at the header type param: string | DeferredTranslation @@ -115,16 +126,16 @@ class ListManager: """ explainer = str(_('\n Choose an object from the list, and select one of the available actions for it to execute')) - self.prompt = prompt + explainer if prompt else explainer + self._prompt = prompt + explainer if prompt else explainer - self.null_action = str(null_action) if null_action else None + self._null_action = str(null_action) if null_action else None if not default_action: - self.default_action = [self.null_action,] + self._default_action = [self._null_action] elif isinstance(default_action,(list,tuple)): - self.default_action = default_action + self._default_action = default_action else: - self.default_action = [str(default_action),] + self._default_action = [str(default_action),] self.header = header if header else None self.cancel_action = str(_('Cancel')) @@ -133,24 +144,23 @@ class ListManager: self.bottom_list = [self.confirm_action,self.cancel_action] self.bottom_item = [self.cancel_action] self.base_actions = base_actions if base_actions else [str(_('Add')),str(_('Copy')),str(_('Edit')),str(_('Delete'))] - self.base_data = base_list - self.data = copy(base_list) # as refs, changes are immediate + self._data = copy(base_list) # as refs, changes are immediate # default values for the null case self.target = None - self.action = self.null_action - if len(self.data) == 0 and self.null_action: - self.exec_action() + self.action = self._null_action + if len(self._data) == 0 and self._null_action: + self.exec_action(self._data) def run(self): while True: - self.data_formatted = self.reformat() - options = self.data_formatted + [self.separator] - if self.default_action: - options += self.default_action + self._data_formatted = self.reformat(self._data) + options = self._data_formatted + [self.separator] + if self._default_action: + options += self._default_action options += self.bottom_list system('clear') - target = Menu(self.prompt, + target = Menu(self._prompt, options, sort=False, clear_screen=False, @@ -162,53 +172,53 @@ class ListManager: break if target and target == self.separator: continue - if target and target in self.default_action: + if target and target in self._default_action: self.action = target target = None self.target = None - self.exec_action() + self.exec_action(self._data) continue - if isinstance(self.data,dict): - key = list(self.data.keys())[self.data_formatted.index(target)] - self.target = {key: self.data[key]} + if isinstance(self._data,dict): + key = list(self._data.keys())[self._data_formatted.index(target)] + self.target = {key: self._data[key]} else: - self.target = self.data[self.data_formatted.index(target)] + self.target = self._data[self._data_formatted.index(target)] # Possible enhacement. If run_actions returns false a message line indicating the failure self.run_actions(target) if not target or target == self.cancel_action: # TODO dubious return self.base_data # return the original list else: - return self.data + return self._data def run_actions(self,prompt_data=None): options = self.action_list() + self.bottom_item prompt = _("Select an action for < {} >").format(prompt_data if prompt_data else self.target) - self.action = Menu(prompt, - options, - sort=False, - skip=False, - clear_screen=False, - clear_menu_on_exit=False, - preset_values=self.bottom_item, - show_search_hint=False).run() - if self.action == self.cancel_action: + self.action = Menu( + prompt, + options, + sort=False, + clear_screen=False, + clear_menu_on_exit=False, + preset_values=self.bottom_item, + show_search_hint=False).run() + if not self.action or self.action == self.cancel_action: return False else: - return self.exec_action() + return self.exec_action(self._data) """ The following methods are expected to be overwritten by the user if the needs of the list are beyond the simple case """ - def reformat(self): + def reformat(self, data: Any) -> List[Any]: """ method to get the data in a format suitable to be shown - It is executed once for run loop and processes the whole self.data structure + It is executed once for run loop and processes the whole self._data structure """ - if isinstance(self.data,dict): - return list(map(lambda x:f"{x} : {self.data[x]}",self.data)) + if isinstance(data,dict): + return list(map(lambda x:f"{x} : {data[x]}",data)) else: - return list(map(lambda x:str(x),self.data)) + return list(map(lambda x:str(x),data)) def action_list(self): """ @@ -217,32 +227,32 @@ class ListManager: """ return self.base_actions - def exec_action(self): + def exec_action(self, data: Any): """ what's executed one an item (self.target) and one action (self.action) is selected. Should be overwritten by the user - The result is expected to update self.data in this routine, else it is ignored + The result is expected to update self._data in this routine, else it is ignored The basic code is useful for simple lists and dictionaries (key:value pairs, both strings) """ # TODO guarantee unicity - if isinstance(self.data,list): + if isinstance(self._data,list): if self.action == str(_('Add')): self.target = TextInput(_('Add :'),None).run() - self.data.append(self.target) + self._data.append(self.target) if self.action == str(_('Copy')): while True: target = TextInput(_('Copy to :'),self.target).run() if target != self.target: - self.data.append(self.target) + self._data.append(self.target) break elif self.action == str(_('Edit')): tgt = self.target - idx = self.data.index(self.target) + idx = self._data.index(self.target) result = TextInput(_('Edite :'),tgt).run() - self.data[idx] = result + self._data[idx] = result elif self.action == str(_('Delete')): - del self.data[self.data.index(self.target)] - elif isinstance(self.data,dict): + del self._data[self._data.index(self.target)] + elif isinstance(self._data,dict): # allows overwrites if self.target: origkey,origval = list(self.target.items())[0] @@ -252,27 +262,15 @@ class ListManager: if self.action == str(_('Add')): key = TextInput(_('Key :'),None).run() value = TextInput(_('Value :'),None).run() - self.data[key] = value + self._data[key] = value if self.action == str(_('Copy')): while True: key = TextInput(_('Copy to new key:'),origkey).run() if key != origkey: - self.data[key] = origval + self._data[key] = origval break elif self.action == str(_('Edit')): value = TextInput(_(f'Edit {origkey} :'),origval).run() - self.data[origkey] = value + self._data[origkey] = value elif self.action == str(_('Delete')): - del self.data[origkey] - - return True - - -if __name__ == "__main__": - # opciones = ['uno','dos','tres','cuatro'] - # opciones = archinstall.list_mirrors() - opciones = {'uno':1,'dos':2,'tres':3,'cuatro':4} - acciones = ['editar','borrar','aƱadir'] - cabecera = ["En Jaen Donde Resido","Vive don Lope de Sosa"] - opciones = ListManager('Vamos alla',opciones,None,_('Add'),default_action=acciones,header=cabecera).run() - print(opciones) + del self._data[origkey] |