Add configuration prompts and validation to `RichInterface`
- Enhanced `RichInterface` with interactive prompts for API key and user preferences (e.g., speed, temperature, visibility, and pressure units). - Added input validation to ensure correct configuration values. - Integrated parent `Application` reference for seamless configuration updates. - Improved error handling to enforce configuration setup before application execution.
This commit is contained in:
parent
772020a9e5
commit
e90ecc60cb
|
|
@ -1,6 +1,6 @@
|
||||||
import argparse
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
import rich
|
|
||||||
|
|
||||||
|
|
||||||
from metar_navigate.ui import RichInterface
|
from metar_navigate.ui import RichInterface
|
||||||
|
|
@ -22,7 +22,7 @@ class Application:
|
||||||
self._validate_args()
|
self._validate_args()
|
||||||
|
|
||||||
if self.args.no_tui:
|
if self.args.no_tui:
|
||||||
self.ui = RichInterface()
|
self.ui = RichInterface(self)
|
||||||
if self.args.configure:
|
if self.args.configure:
|
||||||
self.ui.setup(configure=True)
|
self.ui.setup(configure=True)
|
||||||
if self.args.detailed:
|
if self.args.detailed:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
|
import time
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
from rich import console
|
from rich import console
|
||||||
|
from rich.live import Live
|
||||||
|
|
||||||
|
|
||||||
class UIInterface(ABC):
|
class UIInterface(ABC):
|
||||||
|
|
@ -14,8 +16,9 @@ class UIInterface(ABC):
|
||||||
|
|
||||||
|
|
||||||
class RichInterface(UIInterface):
|
class RichInterface(UIInterface):
|
||||||
def __init__(self):
|
def __init__(self,parent):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.__parent = parent
|
||||||
self.console = console.Console()
|
self.console = console.Console()
|
||||||
self.console.show_cursor(False)
|
self.console.show_cursor(False)
|
||||||
self.mode = None
|
self.mode = None
|
||||||
|
|
@ -35,9 +38,118 @@ class RichInterface(UIInterface):
|
||||||
def configure_run_mode(self):
|
def configure_run_mode(self):
|
||||||
self.console.print("This Application is in [bold red]Configure Mode[/bold red]")
|
self.console.print("This Application is in [bold red]Configure Mode[/bold red]")
|
||||||
self.console.print("This Application requires a CheckWX API Key to run")
|
self.console.print("This Application requires a CheckWX API Key to run")
|
||||||
|
print("Please enter your CheckWX API:",end='')
|
||||||
|
api_key = input()
|
||||||
|
print(f"\033[A\rPlease enter your CheckWX API:{' ' * len(api_key)}\rPlease enter your CheckWX API:",end='')
|
||||||
|
while api_key == '':
|
||||||
|
self.console.print("[bold red]Invalid API Key: Can't be empty [/bold red]")
|
||||||
|
print("Please enter your CheckWX API:", end='')
|
||||||
|
api_key = input()
|
||||||
|
print(f"\033[A\rPlease enter your CheckWX API:{' ' * len(api_key)}\rPlease enter your CheckWX API:", end='')
|
||||||
|
print("\n")
|
||||||
|
always_show_detailed = self.console.input("Always Show Detailed (default is: [green]No[/green]) [[u]y[/u]/[u]N[/u]]: ").upper()
|
||||||
|
if always_show_detailed == '':
|
||||||
|
always_show_detailed = 'N'
|
||||||
|
while always_show_detailed not in ['Y', 'N']:
|
||||||
|
always_show_detailed = self.console.input("Always Show Detailed (default is: [green]No[/green]) [[u]y[/u]/[u]N[/u]]: ").upper()
|
||||||
|
if always_show_detailed == '':
|
||||||
|
always_show_detailed = 'N'
|
||||||
|
|
||||||
|
wind_speed_unit = self.console.input(
|
||||||
|
'Speed Unit (default is: [green]K[/green]) [[u]K[/u]eep/[u]M[/u]iles/K[u]N[/u]ots/KM/[u]H[/u]]: '
|
||||||
|
).upper()
|
||||||
|
|
||||||
|
if wind_speed_unit == '':
|
||||||
|
wind_speed_unit = 'K'
|
||||||
|
while wind_speed_unit not in ['K', 'M', 'N', 'H']:
|
||||||
|
wind_speed_unit = self.console.input(
|
||||||
|
'Speed Unit (default is: [green]K[/green]) [[u]K[/u]eep/[u]M[/u]iles/K[u]N[/u]ots/KM/[u]H[/u]]: '
|
||||||
|
).upper()
|
||||||
|
if wind_speed_unit == '':
|
||||||
|
wind_speed_unit = 'K'
|
||||||
|
|
||||||
|
temp_unit = self.console.input(
|
||||||
|
'Temperature Unit (default is: [green]K[/green]) [[u]K[/u]eep/[u]C[/u]elsius/[u]F[/u]ahrenheit]: '
|
||||||
|
).upper()
|
||||||
|
if temp_unit == '':
|
||||||
|
temp_unit = 'K'
|
||||||
|
while temp_unit not in ['K', 'C', 'F']:
|
||||||
|
temp_unit = self.console.input(
|
||||||
|
'Temperature Unit (default is: [green]K[/green]) [[u]K[/u]eep/[u]C[/u]elsius/[u]F[/u]ahrenheit]: '
|
||||||
|
).upper()
|
||||||
|
if temp_unit == '':
|
||||||
|
temp_unit = 'K'
|
||||||
|
|
||||||
|
visibility_unit = self.console.input(
|
||||||
|
'Visibility Unit (default is: [green]K[/green]) [[u]K[/u]eep/[u]S[/u]M/[u]M[/u]]: '
|
||||||
|
).upper()
|
||||||
|
if visibility_unit == '':
|
||||||
|
visibility_unit = 'K'
|
||||||
|
while visibility_unit not in ['K', 'S', 'M']:
|
||||||
|
visibility_unit = self.console.input(
|
||||||
|
'Visibility Unit (default is: [green]K[/green]) [[u]K[/u]eep/[u]S[/u]M/[u]M[/u]]: '
|
||||||
|
).upper()
|
||||||
|
if visibility_unit == '':
|
||||||
|
visibility_unit = 'K'
|
||||||
|
|
||||||
|
pressure_unit = self.console.input(
|
||||||
|
'Pressure Unit (default is: [green]K[/green]) [[u]K[/u]eep/[u]I[/u]nHg/[u]H[/u]Pa]: '
|
||||||
|
).upper()
|
||||||
|
if pressure_unit == '':
|
||||||
|
pressure_unit = 'K'
|
||||||
|
while pressure_unit not in ['K', 'H', 'U']:
|
||||||
|
pressure_unit = self.console.input(
|
||||||
|
'Pressure Unit (default is: [green]K[/green]) [[u]K[/u]eep/[u]I[/u]nHg/[u]H[/u]Pa]: '
|
||||||
|
).upper()
|
||||||
|
if pressure_unit == '':
|
||||||
|
pressure_unit = 'K'
|
||||||
|
|
||||||
|
self.__parent.config.config_data["checkwx_api_key"] = api_key
|
||||||
|
|
||||||
|
if always_show_detailed == 'Y':
|
||||||
|
self.__parent.config.config_data["always_show_detailed"] = True
|
||||||
|
else:
|
||||||
|
self.__parent.config.config_data["always_show_detailed"] = False
|
||||||
|
|
||||||
|
if wind_speed_unit == 'K':
|
||||||
|
self.__parent.config.config_data["wind_speed_unit"] = "Keep"
|
||||||
|
elif wind_speed_unit == 'M':
|
||||||
|
self.__parent.config.config_data["wind_speed_unit"] = "Miles"
|
||||||
|
elif wind_speed_unit == 'N':
|
||||||
|
self.__parent.config.config_data["wind_speed_unit"] = "Knots"
|
||||||
|
elif wind_speed_unit == 'H':
|
||||||
|
self.__parent.config.config_data["wind_speed_unit"] = "KM/H"
|
||||||
|
|
||||||
|
if temp_unit == 'K':
|
||||||
|
self.__parent.config.config_data["temp_unit"] = "Keep"
|
||||||
|
elif temp_unit == 'C':
|
||||||
|
self.__parent.config.config_data["temp_unit"] = "Celsius"
|
||||||
|
elif temp_unit == 'F':
|
||||||
|
self.__parent.config.config_data["temp_unit"] = "Fahrenheit"
|
||||||
|
|
||||||
|
if visibility_unit == 'K':
|
||||||
|
self.__parent.config.config_data["visibility_unit"] = "Keep"
|
||||||
|
elif visibility_unit == 'S':
|
||||||
|
self.__parent.config.config_data["visibility_unit"] = "SM"
|
||||||
|
elif visibility_unit == 'M':
|
||||||
|
self.__parent.config.config_data["visibility_unit"] = "Meters"
|
||||||
|
|
||||||
|
if pressure_unit == 'K':
|
||||||
|
self.__parent.config.config_data["pressure_unit"] = "Keep"
|
||||||
|
elif pressure_unit == 'H':
|
||||||
|
self.__parent.config.config_data["pressure_unit"] = "HPa"
|
||||||
|
elif pressure_unit == 'I':
|
||||||
|
self.__parent.config.config_data["pressure_unit"] = "InHg"
|
||||||
|
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
|
if not self.__parent.is_configured and self.mode != "configure":
|
||||||
|
self.console.print("[bold red]This Application Requires a configuration to run please run with -c flag to configure [/bold red]")
|
||||||
|
self.console.show_cursor(True)
|
||||||
|
exit(69)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
if self.mode == "configure":
|
if self.mode == "configure":
|
||||||
self.configure_run_mode()
|
self.configure_run_mode()
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue