-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path__main__.py
More file actions
executable file
·105 lines (93 loc) · 2.59 KB
/
__main__.py
File metadata and controls
executable file
·105 lines (93 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
import argparse
import ipaddress
import logging
import sys
from importlib import metadata
from rich.console import Console
from rich.logging import RichHandler
from synapse.cli import (
apps,
discover,
files,
offline_plot,
rpc,
streaming,
taps,
settings,
)
from synapse.utils.discover import find_device_by_name
def is_valid_ip(input_str):
try:
ipaddress.ip_address(input_str)
return True
except ValueError:
return False
def setup_device_uri(args):
if not args.uri:
# User doesn't want to use something that needs a uri
return args
if not is_valid_ip(args.uri):
# User passed in a name
console = Console()
device_ip = find_device_by_name(args.uri, console, False, 60)
if not device_ip:
return None
args.uri = device_ip
return args
def main():
logging.basicConfig(level=logging.INFO, handlers=[RichHandler()])
parser = argparse.ArgumentParser(
description="Synapse Device Manager",
formatter_class=lambda prog: argparse.HelpFormatter(prog, width=124),
)
parser.add_argument(
"--uri",
"-u",
help="The device identifier to connect to. Can either be the IP address or name",
type=str,
default=None,
)
parser.add_argument(
"--version",
action="version",
version="synapsectl %s" % metadata.version("science-synapse"),
)
parser.add_argument(
"--verbose",
"-v",
action="store_true",
default=False,
help="Enable verbose output",
)
subparsers = parser.add_subparsers(title="Commands")
discover.add_commands(subparsers)
rpc.add_commands(subparsers)
streaming.add_commands(subparsers)
offline_plot.add_commands(subparsers)
files.add_commands(subparsers)
taps.add_commands(subparsers)
apps.add_commands(subparsers)
settings.add_commands(subparsers)
args = parser.parse_args()
# If we need to setup the device URI, do that now
args = setup_device_uri(args)
if not args:
return
try:
if hasattr(args, "func"):
args.func(args)
else:
parser.print_help()
except Exception as e:
console = Console()
console.log(f"[bold red] Uncaught error during function. Why: {e}")
parser.print_help()
except KeyboardInterrupt:
print("User cancelled request")
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"Uncaught error in CLI. Why: {e}")
sys.exit(1)