better support for saving variables

This commit is contained in:
boyska 2023-12-25 15:30:39 +01:00
parent 1c9adef883
commit 3ded0784be

View file

@ -188,8 +188,10 @@ class Pira:
def read_variable(self, variable: str):
return self.execute_command(variable)
def set_variable(self, variable: str, value: str):
def set_variable(self, variable: str, value: str, save: bool = False):
cmd = f"{variable}={value}"
if save:
cmd = "*" + cmd
self.execute_command(cmd)
def save_variable(self, variable: str):
@ -221,6 +223,7 @@ def get_parser():
setvar = sub.add_parser("set")
setvar.set_defaults(func=main_set)
setvar.add_argument("--save", action="store_true", default=False)
setvar.add_argument("variable")
setvar.add_argument("value")
@ -260,7 +263,7 @@ def main_get(args, pira: Pira):
def main_set(args, pira: Pira):
try:
pira.set_variable(args.variable, args.value)
pira.set_variable(args.variable, args.value, save=args.save)
except UnknownCommandError:
print("Variable not found: {args.variable}", file=sys.stderr)
sys.exit(1)
@ -293,16 +296,11 @@ def main_synctime(args, pira: Pira):
local_time = datetime.now(tz)
date = local_time.strftime("%d.%m.%y")
time = local_time.strftime("%H:%M:%S")
pira.set_variable("TIME", time)
pira.set_variable("DATE", date)
pira.set_variable("TIME", time, save=args.save)
pira.set_variable("DATE", date, save=args.save)
delta = tz.utcoffset(datetime.now())
offset = delta.total_seconds() / 1800 # multiple of half-hours. So +10800s => +6
pira.set_variable("LTO", "%+d" % offset)
if args.save:
pira.save_variable("DATE")
pira.save_variable("TIME")
pira.save_variable("LTO")
pira.set_variable("LTO", "%+d" % offset, save=args.save)
def make_connection(args) -> Backends: