-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-ntkey.py
More file actions
54 lines (40 loc) · 1.43 KB
/
get-ntkey.py
File metadata and controls
54 lines (40 loc) · 1.43 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
import logging
import sys
import time
import argparse
from networktables import NetworkTables
#https://docs.python.org/3.3/library/argparse.html
parser = argparse.ArgumentParser(description='Get the value of a variable on a NetworkTable')
parser.add_argument('key', nargs='?', default='')
parser.add_argument('-d', '--debug', action='store_true')
parser.add_argument('-s', '--server', default='10.2.79.2')
parser.add_argument('-nt', '--table', default='Preferences')
args = parser.parse_args(sys.argv[1:len(sys.argv)])
if args.debug:
print('Setting value')
print('Server: ' + str(args.server))
print('Table: ' + str(args.table))
print('Key: ' + str(args.key))
print('Debug: ' + str(args.debug))
logging.basicConfig(level=logging.DEBUG)
#connecting isn't instant, and getting the table will not error,
# just not return anything and be confusing if you skip waiting...
#print('Waiting to connect...')
NetworkTables.initialize(server=str(args.server))
while NetworkTables.isConnected() == False:
time.sleep(0.1)
nt = NetworkTables.getTable(str(args.table))
if args.key == '':
print("All keys on table " + str(args.table))
keys = nt.getKeys()
keys.sort()
for key in keys:
val = nt.getValue(key, '')
print('{:<40}{:<40}'.format(key, str(val)))
else:
if nt.containsKey(str(args.key)):
val = nt.getValue(str(args.key), '')
print(str(args.key) + ": " + str(val))
else:
print("Key not found: " + str(args.key))
NetworkTables.shutdown()