From daed08d20659e1726a141904f53069100a57d96e Mon Sep 17 00:00:00 2001 From: Athanasius Date: Wed, 26 Aug 2020 10:18:10 +0100 Subject: [PATCH] system/station providers: Sanitise {system,station}_url logic * Make all plugins use `requests.utils.requote_uri()` * Make all plugins use roughly the same logic, without if/else trees (as the bodies do a `return ...`), ending with `return ''` if input parameters are None. This throws away the inara fallback to `this.station or this.system` as it's unlikely the in-plugin tracking did a better job than the monitor.py code. --- plugins/eddb.py | 9 +++------ plugins/edsm.py | 18 ++++++++++-------- plugins/inara.py | 12 ++++-------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/plugins/eddb.py b/plugins/eddb.py index 683b1b9a..b9dc6b78 100644 --- a/plugins/eddb.py +++ b/plugins/eddb.py @@ -51,20 +51,17 @@ def system_url(system_name: str) -> str: if this.system_address: return requests.utils.requote_uri(f'https://eddb.io/system/ed-address/{this.system_address}') - elif system_name: + if system_name: return requests.utils.requote_uri(f'https://eddb.io/system/name/{system_name}') - else: - return '' + return '' def station_url(system_name: str, station_name: str) -> str: if this.station_marketid: return requests.utils.requote_uri(f'https://eddb.io/station/market-id/{this.station_marketid}') - else: - return system_url('') - + return system_url(system_name) def plugin_start3(plugin_dir): return 'eddb' diff --git a/plugins/edsm.py b/plugins/edsm.py index 27cb6c41..90066ee4 100644 --- a/plugins/edsm.py +++ b/plugins/edsm.py @@ -14,9 +14,6 @@ import json import requests import sys -import urllib.request -import urllib.error -import urllib.parse from queue import Queue from threading import Thread import logging @@ -60,14 +57,19 @@ STATION_UNDOCKED: str = '×' # "Station" name to display when not docked = U+00 # Main window clicks def system_url(system_name): - return 'https://www.edsm.net/en/system?systemName=%s' % urllib.parse.quote(system_name) + if system_name: + return requests.utils.requote_uri(f'https://www.edsm.net/en/system?systemName={system_name}') + + return '' def station_url(system_name, station_name): - if station_name: - return 'https://www.edsm.net/en/system?systemName=%s&stationName=%s' % (urllib.parse.quote(system_name), urllib.parse.quote(station_name)) - else: - return 'https://www.edsm.net/en/system?systemName=%s&stationName=ALL' % urllib.parse.quote(system_name) + if system_name and station_name: + return requests.utils.requote_uri(f'https://www.edsm.net/en/system?systemName={system_name}&stationName={station_name}') + if system_name: + return requests.utils.requote_uri(f'https://www.edsm.net/en/system?systemName={system_name}&stationName=ALL') + + return '' def plugin_start3(plugin_dir): # Can't be earlier since can only call PhotoImage after window is created diff --git a/plugins/inara.py b/plugins/inara.py index ebba39c0..529596f9 100644 --- a/plugins/inara.py +++ b/plugins/inara.py @@ -133,18 +133,14 @@ def system_url(system_name: str): return this.system +def station_url(system_name, station_name): + if system_name and station_name: + return requests.utils.requote_uri(f'https://inara.cz/galaxy-station/?search={system_name}%20[{station_name}]') -def station_url(system_name: Optional[str], station_name: Optional[str]): if system_name: - if station_name: - return requests.utils.requote_uri( - f'https://inara.cz/galaxy-station/?search={system_name}%20[{station_name}]' - ) - return system_url(system_name) - return this.station if this.station else this.system - + return '' def plugin_start3(plugin_dir): this.thread = Thread(target=new_worker, name='Inara worker')