intermediary commit because I have to switch branch because I'm an idiot.

This commit is contained in:
CMDR-Kiel42 2019-08-04 09:59:00 +02:00
parent ebf54319cb
commit 62d808b08a
3 changed files with 201 additions and 169 deletions

193
SpanshRouter/SpanshRouter.py Executable file
View File

@ -0,0 +1,193 @@
#! /usr/bin/env python2
import os
import sys
import csv
import subprocess
import Tkinter as tk
import tkFileDialog as filedialog
import tkMessageBox as confirmDialog
from . import AutoCompleter
from . import PlaceHolderEntry
class SpanshRouter():
def __init__(self, plugin_dir):
version_file = os.path.join(plugin_dir, "version.json")
with open(version_file, 'r') as version_fd:
self.plugin_version = version_fd.read()
self.update_available = False
self.next_stop = "No route planned"
self.route = []
self.next_wp_label = "Next waypoint: "
self.jumpcountlbl_txt = "Estimated jumps left: "
self.parent = None
self.save_route_path = os.path.join(plugin_dir, 'route.csv')
self.offset_file_path = os.path.join(plugin_dir, 'offset')
self.offset = 0
self.jumps_left = 0
self.error_txt = tk.StringVar()
self.plot_error = "Error while trying to plot a route, please try again."
def init_gui(self, parent):
self.parent = parent
parentwidth = parent.winfo_width()
self.frame = tk.Frame(parent, borderwidth=2)
self.frame.grid(sticky=tk.NSEW)
# Route info
self.waypoint_prev_btn = tk.Button(self.frame, text="^", command=self.goto_prev_waypoint)
self.waypoint_btn = tk.Button(self.frame, text=self.next_wp_label + self.next_stop, command=self.copy_waypoint)
self.waypoint_next_btn = tk.Button(self.frame, text="v", command=self.goto_next_waypoint)
self.jumpcounttxt_lbl = tk.Label(self.frame, text=self.jumpcountlbl_txt + str(self.jumps_left))
self.error_lbl = tk.Label(self.frame, textvariable=self.error_txt)
# Plotting GUI
self.source_ac = AutoCompleter(self.frame, "Source System", width=30)
self.dest_ac = AutoCompleter(self.frame, "Destination System", width=30)
self.range_entry = PlaceHolderEntry(self.frame, "Range (LY)", width=10)
self.efficiency_slider = tk.Scale(self.frame, from_=1, to=100, orient=tk.HORIZONTAL, label="Efficiency (%)")
self.efficiency_slider.set(60)
self.plot_gui_btn = tk.Button(self.frame, text="Plot route", command=self.show_plot_gui)
self.plot_route_btn = tk.Button(self.frame, text="Calculate", command=plot_route)
self.cancel_plot = tk.Button(self.frame, text="Cancel", command=lambda: self.show_plot_gui(False))
self.csv_route_btn = tk.Button(self.frame, text="Import CSV", command=plot_csv)
self.clear_route_btn = tk.Button(self.frame, text="Clear route", command=clear_route)
row = 0
self.waypoint_prev_btn.grid(row=row, columnspan=2)
row += 1
self.waypoint_btn.grid(row=row, columnspan=2)
row += 1
self.waypoint_next_btn.grid(row=row, columnspan=2)
row += 1
self.source_ac.grid(row=row,columnspan=2, pady=(10,0)) # The AutoCompleter takes two rows to show the list when needed, so we skip one
row += 2
self.dest_ac.grid(row=row,columnspan=2, pady=(10,0))
row += 2
self.range_entry.grid(row=row, pady=10, sticky=tk.W)
row += 1
self.efficiency_slider.grid(row=row, pady=10, columnspan=2, sticky=tk.EW)
row += 1
self.csv_route_btn.grid(row=row, pady=10, padx=0)
self.plot_route_btn.grid(row=row, pady=10, padx=0)
self.plot_gui_btn.grid(row=row, column=1, pady=10, padx=5, sticky=tk.W)
self.cancel_plot.grid(row=row, column=1, pady=10, padx=5, sticky=tk.E)
row += 1
self.clear_route_btn.grid(row=row,column=1)
row += 1
self.jumpcounttxt_lbl.grid(row=row, pady=5, sticky=tk.W)
row += 1
self.error_lbl.grid(row=row, columnspan=2)
self.error_lbl.grid_remove()
row += 1
show_plot_gui(False)
if not self.route.__len__() > 0:
self.waypoint_prev_btn.grid_remove()
self.waypoint_btn.grid_remove()
self.waypoint_next_btn.grid_remove()
self.jumpcounttxt_lbl.grid_remove()
self.clear_route_btn.grid_remove()
if self.update_available:
update_txt = ("A SpanshRouter update is available.\n"
"It will be installed next time you start EDMC.\n"
"Click to dismiss this message, right click to see what's new.")
self.update_btn = tk.Button(self.frame, text=update_txt, command=lambda: self.update_btn.grid_forget())
self.update_btn.bind("<Button-3>", goto_changelog_page)
self.update_btn.grid(row=row, pady=5, columnspan=2)
row += 1
update_gui()
return self.frame
def open_last_route(self):
try:
with open(self.save_route_path, 'r') as csvfile:
route_reader = csv.reader(csvfile)
for row in route_reader:
if row not in (None, "", []):
self.route.append(row)
try:
with open(self.offset_file_path, 'r') as offset_fh:
self.offset = int(offset_fh.readline())
except:
self.offset = 0
for row in self.route[self.offset:]:
self.jumps_left += int(row[1])
self.next_stop = self.route[self.offset][0]
self.copy_waypoint()
except:
print("No previously saved route.")
def copy_waypoint(self):
if sys.platform == "linux" or sys.platform == "linux2":
command = subprocess.Popen(["echo", "-n", self.next_stop], stdout=subprocess.PIPE)
subprocess.Popen(["xclip", "-selection", "c"], stdin=command.stdout)
else:
self.parent.clipboard_clear()
self.parent.clipboard_append(self.next_stop)
self.parent.update()
def goto_next_waypoint(self):
if self.offset < self.route.__len__()-1:
update_route(1)
def goto_prev_waypoint(self):
if self.offset > 0:
update_route(-1)
def show_plot_gui(self, show=True):
if show:
self.waypoint_prev_btn.grid_remove()
self.waypoint_btn.grid_remove()
self.waypoint_next_btn.grid_remove()
self.jumpcounttxt_lbl.grid_remove()
self.clear_route_btn.grid_remove()
self.plot_gui_btn.grid_remove()
self.csv_route_btn.grid_remove()
self.source_ac.grid()
self.dest_ac.grid()
self.range_entry.grid()
self.efficiency_slider.grid()
self.plot_route_btn.grid()
self.cancel_plot.grid()
# Workaround because EDMC keeps switching the placeholder to bright white
if self.source_ac.get() == self.source_ac.placeholder:
self.source_ac.force_placeholder_color()
if self.dest_ac.get() == self.dest_ac.placeholder:
self.dest_ac.force_placeholder_color()
if self.range_entry.get() == self.range_entry.placeholder:
self.range_entry.force_placeholder_color()
show_route_gui(False)
else:
self.source_ac.put_placeholder()
self.dest_ac.put_placeholder()
self.source_ac.grid_remove()
self.dest_ac.grid_remove()
self.range_entry.grid_remove()
self.efficiency_slider.grid_remove()
self.plot_gui_btn.grid_remove()
self.plot_route_btn.grid_remove()
self.cancel_plot.grid_remove()
self.plot_gui_btn.grid()
self.csv_route_btn.grid()
show_route_gui(True)
if __name__ == "__main__":
pass

View File

@ -1,3 +1,4 @@
from updater import SpanshUpdater
from AutoCompleter import AutoCompleter
from PlaceHolderEntry import PlaceHolderEntry
from SpanshRouter import SpanshRouter

176
load.py
View File

@ -13,24 +13,12 @@ from time import sleep
from SpanshRouter import updater as SpanshUpdater
from SpanshRouter import AutoCompleter
from SpanshRouter import PlaceHolderEntry
from SpanshRouter import SpanshRouter
this = sys.modules[__name__]
this.plugin_version = "2.1.1"
this.update_available = False
this.next_stop = "No route planned"
this.route = []
this.next_wp_label = "Next waypoint: "
this.jumpcountlbl_txt = "Estimated jumps left: "
this.parent = None
this.save_route_path = ""
this.offset_file_path = ""
this.offset = 0
this.jumps_left = 0
this.error_txt = tk.StringVar()
this.plot_error = "Error while trying to plot a route, please try again."
spansh_router = None
# Done
def plugin_start(plugin_dir):
# Check for newer versions
url = "https://raw.githubusercontent.com/CMDR-Kiel42/EDMC_SpanshRouter/master/version.json"
@ -51,32 +39,9 @@ def plugin_start(plugin_dir):
lines = traceback.format_exception(exc_type, exc_value, exc_traceback)
sys.stderr.write(''.join('!! ' + line for line in lines))
finally:
this.save_route_path = os.path.join(plugin_dir, 'route.csv')
this.offset_file_path = os.path.join(plugin_dir, 'offset')
try:
# Open the last saved route
with open(this.save_route_path, 'r') as csvfile:
route_reader = csv.reader(csvfile)
for row in route_reader:
if row not in (None, "", []):
this.route.append(row)
try:
with open(this.offset_file_path, 'r') as offset_fh:
this.offset = int(offset_fh.readline())
except:
this.offset = 0
for row in this.route[this.offset:]:
this.jumps_left += int(row[1])
this.next_stop = this.route[this.offset][0]
copy_waypoint()
except:
print("No previously saved route.")
global spansh_router
spansh_router = SpanshRouter(plugin_dir)
spansh_router.open_last_route()
def plugin_stop():
if this.route.__len__() != 0:
@ -136,62 +101,8 @@ def show_route_gui(show):
def update_gui():
show_route_gui(True)
def show_plot_gui(show=True):
if show:
this.waypoint_prev_btn.grid_remove()
this.waypoint_btn.grid_remove()
this.waypoint_next_btn.grid_remove()
this.jumpcounttxt_lbl.grid_remove()
this.clear_route_btn.grid_remove()
this.plot_gui_btn.grid_remove()
this.csv_route_btn.grid_remove()
this.source_ac.grid()
this.dest_ac.grid()
this.range_entry.grid()
this.efficiency_slider.grid()
this.plot_route_btn.grid()
this.cancel_plot.grid()
# Workaround because EDMC keeps switching the placeholder to bright white
if this.source_ac.get() == this.source_ac.placeholder:
this.source_ac.force_placeholder_color()
if this.dest_ac.get() == this.dest_ac.placeholder:
this.dest_ac.force_placeholder_color()
if this.range_entry.get() == this.range_entry.placeholder:
this.range_entry.force_placeholder_color()
show_route_gui(False)
else:
this.source_ac.put_placeholder()
this.dest_ac.put_placeholder()
this.source_ac.grid_remove()
this.dest_ac.grid_remove()
this.range_entry.grid_remove()
this.efficiency_slider.grid_remove()
this.plot_gui_btn.grid_remove()
this.plot_route_btn.grid_remove()
this.cancel_plot.grid_remove()
this.plot_gui_btn.grid()
this.csv_route_btn.grid()
show_route_gui(True)
def copy_waypoint(self=None):
if sys.platform == "linux" or sys.platform == "linux2":
command = subprocess.Popen(["echo", "-n", this.next_stop], stdout=subprocess.PIPE)
subprocess.Popen(["xclip", "-selection", "c"], stdin=command.stdout)
else:
this.parent.clipboard_clear()
this.parent.clipboard_append(this.next_stop)
this.parent.update()
def goto_next_waypoint(self=None):
if this.offset < this.route.__len__()-1:
update_route(1)
def goto_prev_waypoint(self=None):
if this.offset > 0:
update_route(-1)
def plot_csv(self=None):
filename = filedialog.askopenfilename(filetypes = (("csv files", "*.csv"),)) # show an "Open" dialog box and return the path to the selected file
@ -359,77 +270,4 @@ def goto_changelog_page(self=None):
webbrowser.open(changelog_url)
def plugin_app(parent):
this.parent = parent
parentwidth = parent.winfo_width()
this.frame = tk.Frame(parent, borderwidth=2)
this.frame.grid(sticky=tk.NSEW)
# Route info
this.waypoint_prev_btn = tk.Button(this.frame, text="^", command=goto_prev_waypoint)
this.waypoint_btn = tk.Button(this.frame, text=this.next_wp_label + this.next_stop, command=copy_waypoint)
this.waypoint_next_btn = tk.Button(this.frame, text="v", command=goto_next_waypoint)
this.jumpcounttxt_lbl = tk.Label(this.frame, text=this.jumpcountlbl_txt + str(this.jumps_left))
this.error_lbl = tk.Label(this.frame, textvariable=this.error_txt)
# Plotting GUI
this.source_ac = AutoCompleter(this.frame, "Source System", width=30)
this.dest_ac = AutoCompleter(this.frame, "Destination System", width=30)
this.range_entry = PlaceHolderEntry(this.frame, "Range (LY)", width=10)
this.efficiency_slider = tk.Scale(this.frame, from_=1, to=100, orient=tk.HORIZONTAL, label="Efficiency (%)")
this.efficiency_slider.set(60)
this.plot_gui_btn = tk.Button(this.frame, text="Plot route", command=show_plot_gui)
this.plot_route_btn = tk.Button(this.frame, text="Calculate", command=plot_route)
this.cancel_plot = tk.Button(this.frame, text="Cancel", command=lambda: show_plot_gui(False))
this.csv_route_btn = tk.Button(this.frame, text="Import CSV", command=plot_csv)
this.clear_route_btn = tk.Button(this.frame, text="Clear route", command=clear_route)
row = 0
this.waypoint_prev_btn.grid(row=row, columnspan=2)
row += 1
this.waypoint_btn.grid(row=row, columnspan=2)
row += 1
this.waypoint_next_btn.grid(row=row, columnspan=2)
row += 1
this.source_ac.grid(row=row,columnspan=2, pady=(10,0)) # The AutoCompleter takes two rows to show the list when needed, so we skip one
row += 2
this.dest_ac.grid(row=row,columnspan=2, pady=(10,0))
row += 2
this.range_entry.grid(row=row, pady=10, sticky=tk.W)
row += 1
this.efficiency_slider.grid(row=row, pady=10, columnspan=2, sticky=tk.EW)
row += 1
this.csv_route_btn.grid(row=row, pady=10, padx=0)
this.plot_route_btn.grid(row=row, pady=10, padx=0)
this.plot_gui_btn.grid(row=row, column=1, pady=10, padx=5, sticky=tk.W)
this.cancel_plot.grid(row=row, column=1, pady=10, padx=5, sticky=tk.E)
row += 1
this.clear_route_btn.grid(row=row,column=1)
row += 1
this.jumpcounttxt_lbl.grid(row=row, pady=5, sticky=tk.W)
row += 1
this.error_lbl.grid(row=row, columnspan=2)
this.error_lbl.grid_remove()
row += 1
show_plot_gui(False)
if not this.route.__len__() > 0:
this.waypoint_prev_btn.grid_remove()
this.waypoint_btn.grid_remove()
this.waypoint_next_btn.grid_remove()
this.jumpcounttxt_lbl.grid_remove()
this.clear_route_btn.grid_remove()
if this.update_available:
update_txt = ("A SpanshRouter update is available.\n"
"It will be installed next time you start EDMC.\n"
"Click to dismiss this message, right click to see what's new.")
this.update_btn = tk.Button(this.frame, text=update_txt, command=lambda: this.update_btn.grid_forget())
this.update_btn.bind("<Button-3>", goto_changelog_page)
this.update_btn.grid(row=row, pady=5, columnspan=2)
row += 1
update_gui()
return this.frame