-
Notifications
You must be signed in to change notification settings - Fork 1
/
edi.py
186 lines (144 loc) · 4.34 KB
/
edi.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
from __future__ import print_function
import sys
import shlex
import logging
import time
import cmd
import argparse
import traceback
if __name__ == '__main__':
print("Loading environment...")
import env
log = logging.getLogger("edi")
# Now env is loaded, import the apps
import ship
import edts
import close_to
import coords
import distance
import find
import galmath
import fuel_usage
class EDI(cmd.Cmd):
def __init__(self):
# super (EDI, self).__init__()
cmd.Cmd.__init__(self)
self.prompt = "EDI> "
self.state = {}
def run_application(self, ns, args):
try:
args = shlex.split(args)
app = ns.Application(args, True, self.state)
app.run()
except KeyboardInterrupt:
log.debug("Interrupt detected")
pass
except SystemExit:
pass
except Exception as e:
log.error("Error in application: {}".format(e))
log.debug(traceback.format_exc())
pass
return True
def run_help(self, ns):
try:
ns.Application(['-h'], True, self.state).run()
except SystemExit:
pass
return True
#
# Begin commands
#
def help_edts(self):
return self.run_help(edts)
def do_edts(self, args):
return self.run_application(edts, args)
def help_distance(self):
return self.run_help(distance)
def do_distance(self, args):
return self.run_application(distance, args)
def help_raikogram(self):
return self.help_distance()
def do_raikogram(self, args):
return self.do_distance(args)
def help_close_to(self):
return self.run_help(close_to)
def do_close_to(self, args):
return self.run_application(close_to, args)
def help_coords(self):
return self.run_help(coords)
def do_coords(self, args):
return self.run_application(coords, args)
def help_find(self):
return self.run_help(find)
def do_find(self, args):
return self.run_application(find, args)
def help_galmath(self):
return self.run_help(galmath)
def do_galmath(self, args):
return self.run_application(galmath, args)
def help_fuel_usage(self):
return self.run_help(fuel_usage)
def do_fuel_usage(self, args):
return self.run_application(fuel_usage, args)
def help_set_verbosity(self):
print("usage: set_verbosity N")
print("")
print("Set log level (0-3)")
return True
def do_set_verbosity(self, args):
env.set_verbosity(int(args))
return True
def help_set_ship(self):
print("usage: set_ship -m N -t N -f NC [-c N]")
print("")
print("Set the current ship to be used in other commands")
return True
def do_set_ship(self, args):
ap = argparse.ArgumentParser(fromfile_prefix_chars="@", prog = "set_ship")
ap.add_argument("-f", "--fsd", type=str, required=True, help="The ship's frame shift drive in the form 'A6 or '6A'")
ap.add_argument("-m", "--mass", type=float, required=True, help="The ship's unladen mass excluding fuel")
ap.add_argument("-t", "--tank", type=float, required=True, help="The ship's fuel tank size")
ap.add_argument("-c", "--cargo", type=int, default=0, help="The ship's cargo capacity")
try:
argobj = ap.parse_args(shlex.split(args))
except SystemExit:
return True
s = ship.Ship(argobj.fsd, argobj.mass, argobj.tank, argobj.cargo)
self.state['ship'] = s
print("")
print("Ship [FSD: {0}, mass: {1:.1f}T, fuel: {2:.0f}T]: jump range {3:.2f}Ly ({4:.2f}Ly)".format(s.fsd.drive, s.mass, s.tank_size, s.range(), s.max_range()))
print("")
return True
def help_quit(self):
print("Exit this shell by typing \"exit\", \"quit\" or Control-D.")
return True
def do_quit(self, args):
return False
def help_exit(self):
return self.help_quit()
def do_exit(self, args):
return False
#
# End commands
#
def do_EOF(self, args):
print()
return False
def precmd(self, line):
self.start_time = time.time()
return line
def postcmd(self, retval, line):
if retval is False:
return True
log.debug("Command complete, time taken: {0:.4f}s".format(time.time() - self.start_time))
# Prevent EOF showing up in the list of commands
def print_topics(self, header, cmds, cmdlen, maxcol):
if cmds:
cmds = [c for c in cmds if c != "EOF"]
cmd.Cmd.print_topics(self, header, cmds, cmdlen, maxcol)
if __name__ == '__main__':
env.start()
EDI().cmdloop()
env.stop()