def _dialog_option(self,name): formating = (lc.bright,lc.red) s = self._str_option_help(name) print(s) value = None while value is None: value = input(lc.format("\t\tenter value (default:{}): ", (self._defaults[name],lc.bright,lc.green), default=lc.bright)) value = value.strip() if value=='': value = self._defaults[name] return value try : value = self._cast(name,value) except ValueError as e: print(lc.format("\t\t\t"+str(e),*formating)) value = None if value is not None: try : self._check_integrity(name,value) return value except ValueError as e: print(lc.format("\t\t\tvalue {} was not accepted: {}", (value,*formating), (str(e),*formating), default=formating)) value=None return None
def _get_str(self,enum): r = ["\n"] args = sorted(self._options.union(self._operations)) for index,arg in enumerate(args): if enum : r.append("\t{} | {}:\t{}".format(lc.format(str(index),lc.bright,lc.green), arg, lc.format(getattr(self,arg),lc.bright))) else: r.append("\t{}:\t{}".format(arg,getattr(self,arg))) r.append("\n") return "\n".join(r)
def dialog(self,change_all,args=None): if args : try : done = self.parse(args) if not done: return False except ValueError as e: print() print(lc.format(e,lc.bright,lc.red)) print() return False return True if change_all: for option in self._options: value = self._dialog_option(option) self._set_option_value(option,value) print("\n") for operation in self._operations: value = self._dialog_operation(operation) self._set_operation_value(operation,value) print("\n") return True else: try : self._dialog_propose_defaults() except KeyboardInterrupt: return False return True
def _str_operation_help(self,name): name_str = "--"+name s = str("{}\t{}") help_ = self._helps[name] if help_ is None: help_ = "" return lc.format(s, (name_str,lc.green,lc.bright), (help_,lc.bright))
def print_help(self): if self._help : print(lc.format("\n\t{}\n",(self._help,lc.bright))) else: print("\n") for operation in self._operations: self._print_operation_help(operation) for option in self._options: self._print_option_help(option) print()
def _dialog_propose_defaults(self): def _is_bool(v): if type(v)==type(True): return True def _is_int(v): if type(v)==type(1): return True def _change_value(index): args = sorted(self._options.union(self._operations)) arg = args[index] if arg in self._options: value = self._dialog_option(arg) self._set_option_value(arg,value) else: value = self._dialog_operation(arg) self._set_operation_value(arg,value) def _get_value(): print(self._get_str(True)) value = input(lc.format("\t\tuse these values ? ['y', index to change, 'h' for help]: ", lc.bright)) value = value.strip() value = value.lower() if value=='y': return True if value=='h': return False try: index = int(value) except: raise ValueError(str(value)) if index<0 or index>=(len(self._options)+len(self._operations)): raise ValueError(index) return index while True: try : v = _get_value() except ValueError as e: print(lc.format("\t\t\tinvalid value: {}, accepted values: {}", (e,lc.bright), ("'y','h',index (int)",lc.bright), default=lc.red)) continue except KeyboardInterrupt as ki: raise ki if _is_int(v): print("\n") _change_value(v) else: if v: return v else: print() self.print_help()
def _dialog_operation(self,name): s = self._str_operation_help(name) print(s) value = None while value is None: value = input(lc.format("\t\tuse ? (y, n, default:n): ",lc.bright)) value = value.lower() value = value.strip() if value in ('y','n'): if value=='y': return True if value=='n': return False if value=='': return False
def _get_value(): print(self._get_str(True)) value = input(lc.format("\t\tuse these values ? ['y', index to change, 'h' for help]: ", lc.bright)) value = value.strip() value = value.lower() if value=='y': return True if value=='h': return False try: index = int(value) except: raise ValueError(str(value)) if index<0 or index>=(len(self._options)+len(self._operations)): raise ValueError(index) return index
def _str_option_help(self,name): name_str = "-"+name s = str("\t\t{} ({}, default:{})\t{}{}") class_ = str(self._classes[name]) if "'" in class_: class_ = class_[class_.find("'")+1:class_.rfind("'")] default = self._defaults[name] help_ = self._helps[name] if help_ is None: help_ = "" integrity_str = self._integrity_checks_str(name) return lc.format(s, (name_str,lc.green,lc.bright), (class_,lc.cyan,lc.dim), (default,lc.cyan,lc.dim), (help_,lc.bright), (integrity_str,lc.cyan,lc.dim), default=(lc.cyan,lc.dim))
from lightargs import lightcoloring as lc a = lc.format("\nWelcome to light coloring example", lc.green, lc.bright) b = lc.format("This is a {} to have you {}\n", ("pleasure", lc.cyan, lc.bright), ("here", lc.red), default=(lc.green, lc.dim)) print(a) print(b)