diff --git a/bumblebee_status/modules/contrib/publicip.py b/bumblebee_status/modules/contrib/publicip.py index 5b061f4..f65e3b7 100644 --- a/bumblebee_status/modules/contrib/publicip.py +++ b/bumblebee_status/modules/contrib/publicip.py @@ -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 Note: 1 request/5 minutes is 8640 requests/month Provider information contained in core.location -Left mouse click forces immediate update -Parameters (Default in brackets)_ -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 +Left mouse click on the widget forces immediate update -Examples -By default only the public IP is shown -bumblebee-status -m publicip +Parameters: +publicip.format: Format string (defaults to ‘{ip} ({country_code})’) -To also include the country name... -bumblebee-status -m publicip -p publicip.country_name=True - -To include all ava +Available format strings: +ip +country_name +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 @@ -37,107 +41,46 @@ class Module(core.module.Module): def __init__(self, config, theme): 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) - self.__ip = "" # Public IP address - self.__country_name = "" # Country name associated with public IP address - self.__country_code = "" # Country code associated with public IP address - self.__city_name = "" # City name associated with public IP address - self.__coordinates = "" # Coordinated assoicated with public IP address + self.__ip = "" # Public IP address + self.__country_name = "" # Country name associated with public IP address + self.__country_code = "" # Country code associated with public IP address + self.__city_name = "" # City name associated with public IP address + self.__coordinates = "" # Coordinated assoicated with public IP address - # Handle failure to get IP information - self.__ip_error = False - - # 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)) + # By default show: (<2 letter country code>) + self._format = self.parameter("format", "{ip} ({country_code})") def __click_update(self, event): util.location.reset() def public_ip(self, widget): - __output = "" - - if self.__ip: - if self.__show_ip or self.__show_all: - __output = self.__ip - self.__ip_error = False + if not self.__ip: + return "Error fetching IP" else: - self.__ip_error = True - __output = "Error Getting IP" - - if not self.__ip_error: - if self.__show_country_name or self.__show_all: - if self.__country_name: - __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 + return self._format.format( + ip=self.__ip, + country_name=self.__country_name, + country_code=self.__country_code, + city_name=self.__city_name, + coordinates=self.__coordinates, + ) def update(self): try: self.__ip = util.location.public_ip() + self.__country_name = util.location.country_name() + self.__country_code = util.location.country_code() + self.__city_name = util.location.city_name() + __lat, __lon = util.location.coordinates() + __lat = "{:.2f}".format(__lat) + __lon = "{:.2f}".format(__lon) + __output = __lat + "°N" + "," + " " + __lon + "°E" + self.__coordinates = __output except Exception: - self.__ip = None - - if self.__show_country_name or self.__show_all: - try: - 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() - except Exception: - self.__country_code = None - - if self.__show_city_name or self.__show_all: - try: - self.__city_name = util.location.city_name() - except Exception: - self.__city_name = None - - 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" - self.__coordinates = __output - except Exception: - self.__city_name = None + pass # vim: tabstop=7 expandtab shiftwidth=4 softtabstop=4