Update following PR review

Moved to format string handling of parameters. Minor refactoring.
This commit is contained in:
Tom Watson 2022-07-06 17:51:19 +07:00
parent 218bfa2235
commit 6f137c4927

View file

@ -1,27 +1,31 @@
""" """
Displays public IP address and, optionally, Country Name, Country Code & City Name Displays zero or more of:
* Public IP address
* Country Name
* Country Code
* City Name
* Geographic Coordinates\
Maximum refresh interval should be 5 minutes to avoid free SLA breach from providers Maximum refresh interval should be 5 minutes to avoid free SLA breach from providers
Note: 1 request/5 minutes is 8640 requests/month Note: 1 request/5 minutes is 8640 requests/month
Provider information contained in core.location Provider information contained in core.location
Left mouse click forces immediate update
Parameters (Default in brackets)_ Left mouse click on the widget forces immediate update
ip (True) Public IP address
country_name (False) Display name of country associated with the IP
country_code (False) Display country code of country associated with the IP
city_name (False) Display name of city associated with the IP
coordinates (False) Display name of city associated with the IP
all (False) Display all information associate with the IP
Examples Parameters:
By default only the public IP is shown publicip.format: Format string (defaults to {ip} ({country_code}))
bumblebee-status -m publicip
To also include the country name... Available format strings:
bumblebee-status -m publicip -p publicip.country_name=True ip
country_name
To include all ava country_code
city_name
coordinates
Examples:
bumblebee-status -m publicip -p publicip.format="{ip} ({country_code})"
bumblebee-status -m publicip -p publicip.format="{ip} which is in {city_name}"
bumblebee-status -m publicip -p publicip.format="Your packets are right here: {coordinates}"
""" """
import core.module import core.module
@ -37,6 +41,7 @@ class Module(core.module.Module):
def __init__(self, config, theme): def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.public_ip)) super().__init__(config, theme, core.widget.Widget(self.public_ip))
# Immediate update (override default) when left click on widget
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__click_update) core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__click_update)
self.__ip = "" # Public IP address self.__ip = "" # Public IP address
@ -45,99 +50,37 @@ class Module(core.module.Module):
self.__city_name = "" # City name associated with public IP address self.__city_name = "" # City name associated with public IP address
self.__coordinates = "" # Coordinated assoicated with public IP address self.__coordinates = "" # Coordinated assoicated with public IP address
# Handle failure to get IP information # By default show: <ip> (<2 letter country code>)
self.__ip_error = False self._format = self.parameter("format", "{ip} ({country_code})")
# Process option paramaters
self.__show_ip = util.format.asbool(
self.parameter("ip", True)
)
self.__show_country_name = util.format.asbool(
self.parameter("country_name", False)
)
self.__show_country_code = util.format.asbool(
self.parameter("country_code", False)
)
self.__show_city_name = util.format.asbool(self.parameter("city_name", False))
self.__show_coordinates = util.format.asbool(
self.parameter("coordinates", False)
)
self.__show_all = util.format.asbool(self.parameter("all", False))
def __click_update(self, event): def __click_update(self, event):
util.location.reset() util.location.reset()
def public_ip(self, widget): def public_ip(self, widget):
__output = "" if not self.__ip:
return "Error fetching IP"
if self.__ip:
if self.__show_ip or self.__show_all:
__output = self.__ip
self.__ip_error = False
else: else:
self.__ip_error = True return self._format.format(
__output = "Error Getting IP" ip=self.__ip,
country_name=self.__country_name,
if not self.__ip_error: country_code=self.__country_code,
if self.__show_country_name or self.__show_all: city_name=self.__city_name,
if self.__country_name: coordinates=self.__coordinates,
__output += " " + self.__country_name )
else:
__output += " " + "?"
if self.__show_country_code or self.__show_all:
if self.__country_code:
__output += " " + "(" + self.__country_code + ")"
else:
__output += " " + "(?)"
if self.__show_city_name or self.__show_all:
if self.__city_name:
__output += " " + self.__city_name
else:
__output += " " + "?"
if self.__show_coordinates or self.__show_all:
if self.__coordinates:
__output += " " + self.__coordinates
else:
__output += " " + "?"
return __output
def update(self): def update(self):
try: try:
self.__ip = util.location.public_ip() self.__ip = util.location.public_ip()
except Exception:
self.__ip = None
if self.__show_country_name or self.__show_all:
try:
self.__country_name = util.location.country_name() self.__country_name = util.location.country_name()
except Exception:
self.__country_name = None
if self.__show_country_code or self.__show_all:
try:
self.__country_code = util.location.country_code() self.__country_code = util.location.country_code()
except Exception:
self.__country_code = None
if self.__show_city_name or self.__show_all:
try:
self.__city_name = util.location.city_name() self.__city_name = util.location.city_name()
except Exception: __lat, __lon = util.location.coordinates()
self.__city_name = None __lat = "{:.2f}".format(__lat)
__lon = "{:.2f}".format(__lon)
if self.__show_coordinates or self.__show_all:
try:
__tmp = util.location.coordinates()
__lat = "{:.2f}".format(__tmp[0])
__lon = "{:.2f}".format(__tmp[1])
__output = __lat + "°N" + "," + " " + __lon + "°E" __output = __lat + "°N" + "," + " " + __lon + "°E"
self.__coordinates = __output self.__coordinates = __output
except Exception: except Exception:
self.__city_name = None pass
# vim: tabstop=7 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=7 expandtab shiftwidth=4 softtabstop=4