-
Notifications
You must be signed in to change notification settings - Fork 0
/
webbrowser.py
50 lines (42 loc) · 1.45 KB
/
webbrowser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from __future__ import absolute_import
import webbrowser
from urlparse import urlparse
from kalliope import Utils
from kalliope.core.NeuronModule import (
NeuronModule,
MissingParameterException,
InvalidParameterException)
OPTIONS = {
'current': 0,
'new': 1,
'tab': 2
}
class Webbrowser(NeuronModule):
def __init__(self, **kwargs):
super(Webbrowser, self).__init__(**kwargs)
self.url = kwargs.get('url', None)
self.option = kwargs.get('option', 'tab')
if self._is_parameters_ok():
webbrowser.open(self.url, OPTIONS[self.option])
def _is_parameters_ok(self):
"""
Check if received parameters are ok to perform operations in the neuron.
:return: True if parameters are ok, raise an exception otherwise.
.. raises:: MissingParameterException, InvalidParameterException
"""
if self.url is None:
raise MissingParameterException("Webbrowser needs an url")
if self.url_check(self.url) is False:
raise InvalidParameterException("Webbrowser: invalid url")
if self.option not in OPTIONS:
raise InvalidParameterException("Webbrowser: invalid option")
return True
def url_check(self, url):
try:
result = urlparse(url)
if all([result.scheme, result.netloc]):
return True
else:
return False
except:
return False