[publicip] fix tests and bugs
This commit is contained in:
parent
61ebc3aea6
commit
a78403d3e8
2 changed files with 61 additions and 55 deletions
|
@ -38,19 +38,45 @@ import util.format
|
||||||
import util.location
|
import util.location
|
||||||
|
|
||||||
|
|
||||||
def update_publicip_information(module):
|
class Module(core.module.Module):
|
||||||
widget = module.widget()
|
@core.decorators.every(minutes=60)
|
||||||
__previous_default_route = None
|
def __init__(self, config, theme):
|
||||||
__current_default_route = None
|
super().__init__(config, theme, core.widget.Widget(self.publicip))
|
||||||
__interval = 5 # Interval between default route change checks
|
|
||||||
|
|
||||||
while True:
|
self.__previous_default_route = None
|
||||||
__current_default_route = netifaces.gateways()["default"][2]
|
self.__current_default_route = None
|
||||||
|
self.background = True
|
||||||
|
|
||||||
|
# Immediate update (override default) when left click on widget
|
||||||
|
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__click_update)
|
||||||
|
|
||||||
|
# By default show: <ip> (<2 letter country code>)
|
||||||
|
self._format = self.parameter("format", "{ip} ({country_code})")
|
||||||
|
|
||||||
|
def publicip(self, widget):
|
||||||
|
if widget.get("public_ip") == None:
|
||||||
|
return "n/a"
|
||||||
|
return self._format.format(
|
||||||
|
ip=widget.get("public_ip", "-"),
|
||||||
|
country_name=widget.get("country_name", "-"),
|
||||||
|
country_code=widget.get("country_code", "-"),
|
||||||
|
city_name=widget.get("city_name", "-"),
|
||||||
|
coordinates=widget.get("coordinates", "-"),
|
||||||
|
)
|
||||||
|
|
||||||
|
def __click_update(self, event):
|
||||||
|
util.location.reset()
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
widget = self.widget()
|
||||||
|
|
||||||
|
self.__current_default_route = netifaces.gateways()["default"][2]
|
||||||
|
|
||||||
|
try:
|
||||||
# Updates public ip information if a change to default route is detected
|
# Updates public ip information if a change to default route is detected
|
||||||
if __current_default_route != __previous_default_route:
|
if self.__current_default_route != self.__previous_default_route:
|
||||||
# Sets __previous_default_route in preparation for next change check
|
# Sets __previous_default_route in preparation for next change check
|
||||||
__previous_default_route = __current_default_route
|
self.__previous_default_route = self.__current_default_route
|
||||||
|
|
||||||
# Refresh location information
|
# Refresh location information
|
||||||
util.location.reset()
|
util.location.reset()
|
||||||
|
@ -72,42 +98,8 @@ def update_publicip_information(module):
|
||||||
|
|
||||||
# Update widget values
|
# Update widget values
|
||||||
core.event.trigger("update", [widget.module.id], redraw_only=True)
|
core.event.trigger("update", [widget.module.id], redraw_only=True)
|
||||||
|
except:
|
||||||
# Wait __interval seconds before checking for default route changes again
|
widget.set("public_ip", None)
|
||||||
time.sleep(__interval)
|
|
||||||
|
|
||||||
class Module(core.module.Module):
|
|
||||||
@core.decorators.every(minutes=60)
|
|
||||||
def __init__(self, config, theme):
|
|
||||||
super().__init__(config, theme, core.widget.Widget(self.publicip))
|
|
||||||
|
|
||||||
self.__thread = None
|
|
||||||
|
|
||||||
# Immediate update (override default) when left click on widget
|
|
||||||
core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.__click_update)
|
|
||||||
|
|
||||||
# By default show: <ip> (<2 letter country code>)
|
|
||||||
self._format = self.parameter("format", "{ip} ({country_code})")
|
|
||||||
|
|
||||||
def publicip(self, widget):
|
|
||||||
return self._format.format(
|
|
||||||
ip=widget.get("public_ip", "-"),
|
|
||||||
country_name=widget.get("country_name", "-"),
|
|
||||||
country_code=widget.get("country_code", "-"),
|
|
||||||
city_name=widget.get("city_name", "-"),
|
|
||||||
coordinates=widget.get("coordinates", "-"),
|
|
||||||
)
|
|
||||||
|
|
||||||
def __click_update(self, event):
|
|
||||||
util.location.reset()
|
|
||||||
|
|
||||||
def update(self):
|
|
||||||
if self.__thread is not None and self.__thread.is_alive():
|
|
||||||
return
|
|
||||||
self.__thread = threading.Thread(
|
|
||||||
target=update_publicip_information, args=(self,)
|
|
||||||
)
|
|
||||||
self.__thread.start()
|
|
||||||
|
|
||||||
def state(self, widget):
|
def state(self, widget):
|
||||||
return widget.get("state", None)
|
return widget.get("state", None)
|
||||||
|
|
|
@ -17,27 +17,41 @@ class PublicIPTest(TestCase):
|
||||||
def test_load_module(self):
|
def test_load_module(self):
|
||||||
__import__("modules.contrib.publicip")
|
__import__("modules.contrib.publicip")
|
||||||
|
|
||||||
@mock.patch('util.location.public_ip')
|
@mock.patch('util.location.location_info')
|
||||||
def test_public_ip(self, public_ip_mock):
|
def test_public_ip(self, location_mock):
|
||||||
public_ip_mock.return_value = '5.12.220.2'
|
location_mock.return_value = {
|
||||||
|
'public_ip': '5.12.220.2',
|
||||||
|
'latitude': 0,
|
||||||
|
'longitude': 2,
|
||||||
|
'country': 'some country',
|
||||||
|
'country_code': 'sc',
|
||||||
|
'city_name': '???',
|
||||||
|
}
|
||||||
|
|
||||||
module = build_module()
|
module = build_module()
|
||||||
module.update()
|
module.update()
|
||||||
|
|
||||||
assert widget(module).full_text() == '5.12.220.2'
|
assert widget(module).full_text() == '5.12.220.2 (sc)'
|
||||||
|
|
||||||
@mock.patch('util.location.public_ip')
|
@mock.patch('util.location.location_info')
|
||||||
def test_public_ip(self, public_ip_mock):
|
def test_public_ip2(self, location_mock):
|
||||||
public_ip_mock.return_value = None
|
location_mock.return_value = {
|
||||||
|
'public_ip': None,
|
||||||
|
'latitude': 0,
|
||||||
|
'longitude': 2,
|
||||||
|
'country': 'some country',
|
||||||
|
'country_code': 'sc',
|
||||||
|
'city_name': '???',
|
||||||
|
}
|
||||||
|
|
||||||
module = build_module()
|
module = build_module()
|
||||||
module.update()
|
module.update()
|
||||||
|
|
||||||
assert widget(module).full_text() == 'n/a'
|
assert widget(module).full_text() == 'n/a'
|
||||||
|
|
||||||
@mock.patch('util.location.public_ip')
|
@mock.patch('util.location.location_info')
|
||||||
def test_public_ip_with_exception(self, public_ip_mock):
|
def test_public_ip_with_exception(self, location_mock):
|
||||||
public_ip_mock.side_effect = Exception
|
location_mock.side_effect = Exception
|
||||||
|
|
||||||
module = build_module()
|
module = build_module()
|
||||||
module.update()
|
module.update()
|
||||||
|
|
Loading…
Reference in a new issue