ED_LRR/ed_lrr_gui/__main__.py

111 lines
4.9 KiB
Python

import sys
import multiprocessing as MP
import queue
import ctypes
from math import floor
import click
from click_default_group import DefaultGroup
import requests as RQ
from ed_lrr_gui import Router
from ed_lrr_gui import Preprocessor
import ed_lrr_gui.gui as ED_LRR_GUI
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.group(invoke_without_command=True,context_settings=CONTEXT_SETTINGS)
@click.pass_context
def main(ctx):
"Elite: Dangerous long range router, command line interface"
if ctx.invoked_subcommand is None:
ctx.invoke(gui)
return
return
@main.command()
@click.option("--debug",help="Debug print",is_flag=True)
def gui(debug):
"Run the ED LRR GUI (default)"
if not debug:
ctypes.windll.kernel32.FreeConsole()
sys.stdin=open("NUL","rt")
sys.stdout=open("NUL","wt")
sys.stderr=open("NUL","wt")
sys.exit(ED_LRR_GUI.main())
@main.command()
@click.option("--url","-u",help="Base URL",default="https://www.edsm.net/dump/",show_default=True)
@click.option("--systems","-s",help="Target path for systemsWithCoordinates.json",default="systemsWithCoordinates.json",show_default=True)
@click.option("--bodies","-b",help="Target path for bodies.json",default="bodies.json",show_default=True)
def download(*args,**kwargs):
"Download EDSM dumps"
print("Download:",args,kwargs)
click.pause()
@main.command()
def preprocess(*args,**kwargs):
"Preprocess EDSM dumps"
print("PreProcess:",ctx,args,kwargs)
click.pause()
@main.command()
@click.option("--path","-i",required=True,metavar="<path>",help="Path to stars.csv",default="./stars.csv",type=click.Path(exists=True,dir_okay=False),show_default=True )
@click.option("--precomp_file","-pf",metavar="<path>",help="Precomputed routing graph to use",type=click.Path(exists=True,dir_okay=False))
@click.option("--range","-r",required=True,metavar="<float>",help="Jump range (Ly)",type=click.FloatRange(min=0))
@click.option("--prune","-d",default=(0,0),metavar="<n> <m>",help="Prune search branches",nargs=2,type=click.Tuple([click.IntRange(min=0),click.FloatRange(min=0)]))
@click.option("--permute","-p",type=click.Choice(["all","keep_first","keep_last","keep_both"]),default=None,help="Permute hops to find shortest route",show_default=True)
@click.option("--primary","-ps",is_flag=True,default=False,help="Only route through primary stars")
@click.option("--factor","-g",metavar="<float>",default=0.5,help="Greedyness factor for A-Star",show_default=True)
@click.option("--mode","-m",default="bfs",help="Search mode",type=click.Choice(["bfs","a-star","greedy"]),show_default=True)
@click.argument('systems',nargs=-1)
def route(**kwargs):
"Compute a route"
if kwargs['prune']==(0,0):
kwargs['prune']=None
def to_string(state):
if state:
return "[{}] {}".format(state['depth'],state['system'])
keep_first,keep_last={
"all":(False,False),
"keep_first":(True,False),
"keep_last":(False,True),
"keep_both":(True,True),
None: (False,False)
}[kwargs['permute']]
args=[kwargs['systems'],kwargs['range'],kwargs['prune'],kwargs['mode'],kwargs['primary'],kwargs['permute']!=None,keep_first,keep_last,kwargs['factor'],None,kwargs['path']]
with click.progressbar(length=100,label="Computing route",show_percent=True,item_show_func=to_string,width=50) as pbar:
router=Router(*args)
router.start()
state={}
pstate={}
while not (router.queue.empty() and router.is_alive()==False):
try:
event = router.queue.get(True,0.1)
state.update(event)
if state!=pstate:
pbar.current_item=state.get("status")
if pbar.current_item:
pbar.pos=floor(pbar.current_item["prc_done"]*10)/10
pbar.update(0)
pstate=state
except queue.Empty:
pass
pbar.pos=100
pbar.update(0)
print(state.get("result"))
print("DONE!")
@main.command()
@click.option("--path","-i",required=True,help="Path to stars.csv",default="./stars.csv",type=click.Path(exists=True,dir_okay=False),show_default=True )
@click.option("--precomp_file","-pc",help="Precomputed routing graph to use",type=click.Path(exists=True,dir_okay=False))
@click.option("--range","-r",required=True,help="Jump range (Ly)",type=click.FloatRange(min=0))
@click.option("--primary","-ps",help="Only route through primary stars")
@click.option("--output","-o",required=True,help="Output path",default="./stars.idx",type=click.Path(exists=False,dir_okay=False),show_default=True )
@click.argument('systems',nargs=-1)
def precompute(*args,**kwargs):
"Precompute routing graph"
print("PreComp:",ctx,args,kwargs)
if __name__ == '__main__':
MP.freeze_support()
main()