Repository navigation
Expand file tree
/
Copy pathconsole_view.py
More file actions
73 lines (61 loc) · 2.27 KB
/
Copy pathconsole_view.py
File metadata and controls
73 lines (61 loc) · 2.27 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
"""
Print a Viron environment to the console, as an example of using text_renderer.
The environment must already exist, for example from a run of main.py, whose
environments.json records the ID. Nothing is created in Viron and no file is written.
Usage:
python console_view.py <environmentId>
The exit status is 0 if the environment was printed, 1 if it could not be fetched or has
no locations, and 2 if the argument is missing, is not an integer, or is followed by
further arguments.
"""
import sys
from Viron.src.main.python.preponderous.viron.services.locationService import LocationService
from text_renderer import renderLocations
from viron_address import getVironAddress
usage = "Usage: python console_view.py <environmentId>"
def parseEnvironmentId(argv):
"""
Read the environment ID from the command line.
Args:
argv (list): The argument vector, including the program name at index 0
Returns:
int: The environment ID, or None if there is not exactly one integer argument
"""
if len(argv) != 2:
return None
try:
return int(argv[1])
except ValueError:
return None
def main(argv, locationService=None):
"""
Print the environment named on the command line.
Args:
argv (list): The argument vector, including the program name at index 0
locationService (LocationService): The location service to use, or None to build one
Returns:
int: The exit status
"""
if "--help" in argv[1:] or "-h" in argv[1:]:
print(usage)
return 0
environmentId = parseEnvironmentId(argv)
if environmentId is None:
print(usage)
return 2
if locationService is None:
url, port = getVironAddress()
locationService = LocationService(url, port)
try:
locations = locationService.get_locations_in_environment(environmentId)
except Exception as e:
print(f"Error fetching the locations of environment {environmentId}: {e}")
return 1
# Viron answers an unknown environment ID with an empty list rather than an error
if not locations:
print(f"Environment {environmentId} has no locations.")
return 1
print(renderLocations(locations))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))