|
@@ -154,12 +154,15 @@ class CommandStatus(LookupEnum, enum.StrEnum):
|
|
|
|
|
|
|
|
|
class Pira:
|
|
|
- END_OF_MESSAGE = "".join([chr(13), chr(10), chr(13), chr(10)])
|
|
|
+ END_OF_MESSAGE = "\r\n\r\n"
|
|
|
|
|
|
def __init__(self, backend: Backends):
|
|
|
self.log = logging.getLogger("Pira")
|
|
|
self.backend = backend
|
|
|
- self.set_variable("ECHO", "0")
|
|
|
+ try:
|
|
|
+ self.set_variable("ECHO", "0")
|
|
|
+ except CommandError: # this sometimes fails, it's a fact
|
|
|
+ pass
|
|
|
|
|
|
def recv_response(self) -> str:
|
|
|
def msg_complete(s: str) -> bool:
|
|
@@ -185,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):
|
|
@@ -212,12 +217,17 @@ def get_parser():
|
|
|
|
|
|
sub = p.add_subparsers()
|
|
|
|
|
|
+ execute = sub.add_parser("exec")
|
|
|
+ execute.set_defaults(func=main_exec)
|
|
|
+ execute.add_argument("cmd")
|
|
|
+
|
|
|
get = sub.add_parser("get")
|
|
|
get.set_defaults(func=main_get)
|
|
|
get.add_argument("variable")
|
|
|
|
|
|
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")
|
|
|
|
|
@@ -246,20 +256,29 @@ def output(args, content):
|
|
|
print(repr(content["value"]))
|
|
|
|
|
|
|
|
|
+
|
|
|
+def main_exec(args, pira: Pira):
|
|
|
+ try:
|
|
|
+ out = pira.execute_command(args.cmd)
|
|
|
+ except CommandError as exc:
|
|
|
+ print(f"Command error: {exc}", file=sys.stderr)
|
|
|
+ sys.exit(1)
|
|
|
+ output(args, dict(cmd=args.cmd, value=out))
|
|
|
+
|
|
|
def main_get(args, pira: Pira):
|
|
|
try:
|
|
|
out = pira.read_variable(args.variable)
|
|
|
except UnknownCommandError:
|
|
|
- print("Variable not found: {args.variable}", file=sys.stderr)
|
|
|
+ print(f"Variable not found: {args.variable}", file=sys.stderr)
|
|
|
sys.exit(1)
|
|
|
output(args, dict(variable=args.variable, value=out))
|
|
|
|
|
|
|
|
|
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)
|
|
|
+ print(f"Variable not found: {args.variable}", file=sys.stderr)
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
@@ -267,7 +286,7 @@ def main_save(args, pira: Pira):
|
|
|
try:
|
|
|
pira.save_variable(args.variable)
|
|
|
except UnknownCommandError:
|
|
|
- print("Variable not found: {args.variable}", file=sys.stderr)
|
|
|
+ print(f"Variable not found: {args.variable}", file=sys.stderr)
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
@@ -290,16 +309,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:
|