proton-director/debug.py

70 lines
1.9 KiB
Python
Executable File

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
import os
import socket
import sys
import argparse
'''
AI director debugger
Interactive and dynamic debug user interface
* The debugger server opens a local socket connection for realtime commands
* The debugger server has a many methods available (list, set, etc.)
* The user can activate tags: the debugger will output messages with active flag at reception
* The user can debug objects: the object will output part or all of its internals
Licensed under GNU GPLv3
by alban
'''
valid_commands = {
'list': {
"api": "list_tags",
"help": "List available tags",
"tags": None
},
'add': {
"api": "add_tags",
"help": "Debug the given tag(s)",
"tags": "tags"
},
'del': {
"api": "def_tags",
"help": "Remove from debug the given tag(s)",
"tags": "tags"
},
'help': {
"api": "help",
"help": "Request help",
"tags": None
},
}
socket_addr = "/tmp/._my_debug_socket"
parser = argparse.ArgumentParser(description="AI Director debugger utility")
subparsers = parser.add_subparsers(dest='command', help="API request")
for i in valid_commands:
sub = subparsers.add_parser(i)
item = valid_commands[i]
if item["tags"]:
sub.add_argument("tags", type=str, nargs="+", help=item["help"])
parser.add_argument("--socket", help="Socket location. Default: '{}' ".format(socket_addr), default=socket_addr,
required=False)
args = parser.parse_args()
if args.command not in valid_commands:
raise Exception("Invalid command '{}' not in {}".format(args.command, ", ".join(valid_commands)))
command = valid_commands[args.command]["api"]
tags = " ".join(args.tags) if "tags" in args else ""
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(socket_addr)
s.send(bytes("{} {}".format(command,tags), "utf-8"))
data = s.recv(1024)
print('Received', str(data, "utf-8"))