edsm-getnearest.py

Now in a usable form with help text and stuff. Bear with me, I literally
picked Python up again about today after having used it like 5y ago on
for completely unrelated things.
This commit is contained in:
alterNERDtive 2019-09-03 21:00:51 +02:00
parent f677b453cd
commit f21e32f653
2 changed files with 132 additions and 83 deletions

21
README.md Normal file
View file

@ -0,0 +1,21 @@
# A collection fo useful scripts around Elite Dangerous #
## edsm-getnearest.py ##
```
usage: edsm-getnearest.py [-h] --system SYSTEM [--short] [--gui | --text]
CMDR [CMDR ...]
Locate your CMDRs using EDSM and find their distance to a given system.
positional arguments:
CMDR a list of CMDR names (must have their location public on
EDSM!)
optional arguments:
-h, --help show this help message and exit
--system SYSTEM the target system (must be in EDDN!)
--short short output (only makes sense with `--text`)
--gui explicitly run the GUI
--text explicitly give text output
```

View file

@ -1,13 +1,11 @@
#!/usr/bin/env python3
import argparse
import math
import requests
import sys
import tkinter as tk
from tkinter import messagebox
if len(sys.argv) < 3:
exit(1)
# =================================================================================
class EdsmApiException(Exception):
pass
@ -45,6 +43,8 @@ def getDistances (system, cmdrs):
distances[cmdr] = round(distance(cmdrcoords, systemcoords))
return distances
# =================================================================================
def outputGui():
def runsearch(event=None):
for child in frame.winfo_children():
@ -79,11 +79,13 @@ def outputGui():
frame.grid(row=1, columnspan=3)
btn = tk.Button(window, text='get distances', command=runsearch)
btn.grid(row=0, column=2)
window.geometry('450x200+550+-650')
window.bind('<Return>', runsearch)
window.attributes('-topmost', True)
runsearch()
window.mainloop()
# =================================================================================
def outputText():
try:
distances = getDistances(system, cmdrs)
@ -91,18 +93,44 @@ def outputText():
print(e)
exit(1)
nearestCmdr = min(distances,key=distances.get)
if shortOutput:
print('nearest CMDR: {} ({}ly)'.format(nearestCmdr,
distances[nearestCmdr]))
else:
print('nearest CMDR: {} ({}ly from {})'.format(nearestCmdr,
distances[nearestCmdr], system))
print()
for cmdr in distances:
print('{}: {}ly'.format(cmdr, distances[cmdr]))
sys.argv.pop(0) # script name
system = sys.argv.pop(0)
cmdrs = sys.argv
# =================================================================================
parser = argparse.ArgumentParser(description='Locate your CMDRs using EDSM and '
+ 'find their distance to a given system.')
parser.add_argument('cmdrs', metavar='CMDR', nargs='+', help='a list of CMDR names '
+ '(must have their location public on EDSM!)')
parser.add_argument('--system', nargs=1, help='the target system (must be in '
+ 'EDDN!)', required=True)
parser.add_argument('--short', action='store_true', help='short output (only '
+ 'makes sense with `--text`)')
group = parser.add_mutually_exclusive_group()
group.add_argument('--gui', action='store_true', help='explicitly run the GUI')
group.add_argument('--text', action='store_true', help='explicitly give text output')
args = parser.parse_args()
system = args.system[0]
cmdrs = args.cmdrs
shortOutput = args.short
# =================================================================================
if args.text:
outputText()
elif args.gui:
outputGui()
# try:
# outputGui()
# except tk.TclError:
# outputText()
else:
try:
outputGui()
except tk.TclError:
outputText()