Browse Source

[Cleanup] All the neurons adding the _is_parameters_ok function + check

monf 8 years ago
parent
commit
f498d9b2ee

+ 112 - 104
neurons/openweathermap/Openweathermap.py

@@ -8,110 +8,118 @@ class Openweathermap(NeuronModule):
         # get message to spell out loud
         super(Openweathermap, self).__init__(**kwargs)
 
-        api_key = kwargs.get('api_key', None)
-        location = kwargs.get('location', None)
-        lang = kwargs.get('lang', 'en')
-        temp_unit = kwargs.get('temp_unit', 'celsius')
-        country = kwargs.get('country', None)
-
-        if api_key is None:
+        self.api_key = kwargs.get('api_key', None)
+        self.location = kwargs.get('location', None)
+        self.lang = kwargs.get('lang', 'en')
+        self.temp_unit = kwargs.get('temp_unit', 'celsius')
+        self.country = kwargs.get('country', None)
+
+        # check if parameters have been provided
+        if self._is_parameters_ok():
+            extended_location = self.location
+            if self.country is not None:
+                extended_location = self.location + "," + self.country
+
+
+            owm = pyowm.OWM(API_key=self.api_key, language=self.lang)
+
+            # Tomorrow
+            forecast = owm.daily_forecast(extended_location)
+            tomorrow = pyowm.timeutils.tomorrow()
+            weather_tomorrow = forecast.get_weather_at(tomorrow)
+            weather_tomorrow_status = weather_tomorrow.get_detailed_status()
+            sunset_time_tomorrow = weather_tomorrow.get_sunset_time('iso')
+            sunrise_time_tomorrow = weather_tomorrow.get_sunrise_time('iso')
+
+            temp_tomorrow = weather_tomorrow.get_temperature(unit=self.temp_unit)
+            temp_tomorrow_temp = temp_tomorrow['day']
+            temp_tomorrow_temp_max = temp_tomorrow['max']
+            temp_tomorrow_temp_min = temp_tomorrow['min']
+
+            pressure_tomorrow = weather_tomorrow.get_pressure()
+            pressure_tomorrow_press = pressure_tomorrow['press']
+            pressure_tomorrow_sea_level = pressure_tomorrow['sea_level']
+
+            humidity_tomorrow = weather_tomorrow.get_humidity()
+
+            wind_tomorrow = weather_tomorrow.get_wind()
+            # wind_tomorrow_deg = wind_tomorrow['deg']
+            wind_tomorrow_speed = wind_tomorrow['speed']
+
+            snow_tomorrow = weather_tomorrow.get_snow()
+            rain_tomorrow = weather_tomorrow.get_rain()
+            clouds_coverage_tomorrow = weather_tomorrow.get_clouds()
+
+            # Today
+            observation = owm.weather_at_place(extended_location)
+            weather_today = observation.get_weather()
+            weather_today_status = weather_today.get_detailed_status()
+            sunset_time_today = weather_today.get_sunset_time('iso')
+            sunrise_time_today = weather_today.get_sunrise_time('iso')
+
+            temp_today = weather_today.get_temperature(unit=self.temp_unit)
+            temp_today_temp = temp_today['temp']
+            temp_today_temp_max = temp_today['temp_max']
+            temp_today_temp_min = temp_today['temp_min']
+
+            pressure_today = weather_today.get_pressure()
+            pressure_today_press = pressure_today['press']
+            pressure_today_sea_level = pressure_today['sea_level']
+
+            humidity_today = weather_today.get_humidity()
+
+            wind_today= weather_today.get_wind()
+            wind_today_deg = wind_today['deg']
+            wind_today_speed = wind_today['speed']
+
+            snow_today = weather_today.get_snow()
+            rain_today = weather_today.get_rain()
+            clouds_coverage_today = weather_today.get_clouds()
+
+            message = {
+                "location": self.location,
+
+                "weather_today": weather_today_status,
+                "sunset_today_time": sunset_time_today,
+                "sunrise_today_time": sunrise_time_today,
+                "temp_today_temp": temp_today_temp,
+                "temp_today_temp_max": temp_today_temp_max,
+                "temp_today_temp_min": temp_today_temp_min,
+                "pressure_today_press": pressure_today_press,
+                "pressure_today_sea_level": pressure_today_sea_level,
+                "humidity_today": humidity_today,
+                "wind_today_deg": wind_today_deg,
+                "wind_today_speed": wind_today_speed,
+                "snow_today": snow_today,
+                "rain_today": rain_today,
+                "clouds_coverage_today": clouds_coverage_today,
+
+                "weather_tomorrow": weather_tomorrow_status,
+                "sunset_time_tomorrow": sunset_time_tomorrow,
+                "sunrise_time_tomorrow": sunrise_time_tomorrow,
+                "temp_tomorrow_temp": temp_tomorrow_temp,
+                "temp_tomorrow_temp_max": temp_tomorrow_temp_max,
+                "temp_tomorrow_temp_min": temp_tomorrow_temp_min,
+                "pressure_tomorrow_press": pressure_tomorrow_press,
+                "pressure_tomorrow_sea_level": pressure_tomorrow_sea_level,
+                "humidity_tomorrow": humidity_tomorrow,
+                # "wind_tomorrow_deg": wind_tomorrow_deg,
+                "wind_tomorrow_speed": wind_tomorrow_speed,
+                "snow_tomorrow": snow_tomorrow,
+                "rain_tomorrow": rain_tomorrow,
+                "clouds_coverage_tomorrow": clouds_coverage_tomorrow
+            }
+
+            self.say(message)
+
+    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
+        """
+        if self.api_key is None:
             raise NotImplementedError("OpenWeatherMap neuron needs an api_key")
-        if location is None:
+        if self.location is None:
             raise NotImplementedError("OpenWeatherMap neuron needs a location")
-        extended_location = location
-        if country is not None:
-            extended_location = location + "," + country
-
-
-        owm = pyowm.OWM(API_key=api_key, language=lang)
-
-        # Tomorrow
-        forecast = owm.daily_forecast(extended_location)
-        tomorrow = pyowm.timeutils.tomorrow()
-        weather_tomorrow = forecast.get_weather_at(tomorrow)
-        weather_tomorrow_status = weather_tomorrow.get_detailed_status()
-        sunset_time_tomorrow = weather_tomorrow.get_sunset_time('iso')
-        sunrise_time_tomorrow = weather_tomorrow.get_sunrise_time('iso')
-
-        temp_tomorrow = weather_tomorrow.get_temperature(unit=temp_unit)
-        temp_tomorrow_temp = temp_tomorrow['day']
-        temp_tomorrow_temp_max = temp_tomorrow['max']
-        temp_tomorrow_temp_min = temp_tomorrow['min']
-
-        pressure_tomorrow = weather_tomorrow.get_pressure()
-        pressure_tomorrow_press = pressure_tomorrow['press']
-        pressure_tomorrow_sea_level = pressure_tomorrow['sea_level']
-
-        humidity_tomorrow = weather_tomorrow.get_humidity()
-
-        wind_tomorrow = weather_tomorrow.get_wind()
-        # wind_tomorrow_deg = wind_tomorrow['deg']
-        wind_tomorrow_speed = wind_tomorrow['speed']
-
-        snow_tomorrow = weather_tomorrow.get_snow()
-        rain_tomorrow = weather_tomorrow.get_rain()
-        clouds_coverage_tomorrow = weather_tomorrow.get_clouds()
-
-        # Today
-        observation = owm.weather_at_place(extended_location)
-        weather_today = observation.get_weather()
-        weather_today_status = weather_today.get_detailed_status()
-        sunset_time_today = weather_today.get_sunset_time('iso')
-        sunrise_time_today = weather_today.get_sunrise_time('iso')
-
-        temp_today = weather_today.get_temperature(unit=temp_unit)
-        temp_today_temp = temp_today['temp']
-        temp_today_temp_max = temp_today['temp_max']
-        temp_today_temp_min = temp_today['temp_min']
-
-        pressure_today = weather_today.get_pressure()
-        pressure_today_press = pressure_today['press']
-        pressure_today_sea_level = pressure_today['sea_level']
-
-        humidity_today = weather_today.get_humidity()
-
-        wind_today= weather_today.get_wind()
-        wind_today_deg = wind_today['deg']
-        wind_today_speed = wind_today['speed']
-
-        snow_today = weather_today.get_snow()
-        rain_today = weather_today.get_rain()
-        clouds_coverage_today = weather_today.get_clouds()
-
-        message = {
-            "location": location,
-
-            "weather_today": weather_today_status,
-            "sunset_today_time": sunset_time_today,
-            "sunrise_today_time": sunrise_time_today,
-            "temp_today_temp": temp_today_temp,
-            "temp_today_temp_max": temp_today_temp_max,
-            "temp_today_temp_min": temp_today_temp_min,
-            "pressure_today_press": pressure_today_press,
-            "pressure_today_sea_level": pressure_today_sea_level,
-            "humidity_today": humidity_today,
-            "wind_today_deg": wind_today_deg,
-            "wind_today_speed": wind_today_speed,
-            "snow_today": snow_today,
-            "rain_today": rain_today,
-            "clouds_coverage_today": clouds_coverage_today,
-
-            "weather_tomorrow": weather_tomorrow_status,
-            "sunset_time_tomorrow": sunset_time_tomorrow,
-            "sunrise_time_tomorrow": sunrise_time_tomorrow,
-            "temp_tomorrow_temp": temp_tomorrow_temp,
-            "temp_tomorrow_temp_max": temp_tomorrow_temp_max,
-            "temp_tomorrow_temp_min": temp_tomorrow_temp_min,
-            "pressure_tomorrow_press": pressure_tomorrow_press,
-            "pressure_tomorrow_sea_level": pressure_tomorrow_sea_level,
-            "humidity_tomorrow": humidity_tomorrow,
-            # "wind_tomorrow_deg": wind_tomorrow_deg,
-            "wind_tomorrow_speed": wind_tomorrow_speed,
-            "snow_tomorrow": snow_tomorrow,
-            "rain_tomorrow": rain_tomorrow,
-            "clouds_coverage_tomorrow": clouds_coverage_tomorrow
-        }
-
-        self.say(message)
-
 
+        return True

+ 20 - 8
neurons/push_message/Push_message.py

@@ -8,8 +8,7 @@ class Push_message(NeuronModule):
     """
     Neuron based on pushetta api. http://www.pushetta.com/
     """
-
-    def __init__(self, message=None, api_key=None, channel_name=None, **kwargs):
+    def __init__(self, **kwargs):
         """
         Send a push message to an android phone via Pushetta API
         :param message: Message to send
@@ -18,13 +17,26 @@ class Push_message(NeuronModule):
         :return:
         """
         super(Push_message, self).__init__(**kwargs)
-        if message is None:
-            raise NotImplementedError("Pushetta neuron needs message to send")
 
-        if api_key is None:
+        self.message = kwargs.get('message', None)
+        self.api_key = kwargs.get('api_key', None)
+        self.channel_name = kwargs.get('channel_name', None)
+
+        # check if parameters have been provided
+        if self._is_parameters_ok():
+            p = Pushetta( self.api_key)
+            p.pushMessage(self.channel_name, self.message)
+
+    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
+        """
+        if self.message is None:
+            raise NotImplementedError("Pushetta neuron needs message to send")
+        if self.api_key is None:
             raise NotImplementedError("Pushetta neuron needs api_key")
-        if channel_name is None:
+        if self.channel_name is None:
             raise NotImplementedError("Pushetta neuron needs channel_name")
 
-        p = Pushetta(api_key)
-        p.pushMessage(channel_name, message)
+        return True

+ 13 - 6
neurons/say/say.py

@@ -4,10 +4,17 @@ from core.NeuronModule import NeuronModule, MissingParameterException
 class Say(NeuronModule):
     def __init__(self, **kwargs):
         super(Say, self).__init__(**kwargs)
-        # get message to spell out loud
-        message = kwargs.get('message', None)
-        # user must specify a message
-        if message is None:
+        self.message = kwargs.get('message', None)
+
+        # check if parameters have been provided
+        if self._is_parameters_ok():
+            self.say(self.message)
+
+    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
+        """
+        if self.message is None:
             raise MissingParameterException("You must specify a message string or a list of messages as parameter")
-        else:
-            self.say(message)
+        return True

+ 26 - 21
neurons/shell/shell.py

@@ -28,27 +28,32 @@ class Shell(NeuronModule):
         super(Shell, self).__init__(**kwargs)
 
         # get the command
-        cmd = kwargs.get('cmd', None)
+        self.cmd = kwargs.get('cmd', None)
         # get if the user select a blocking command or not
-        async = kwargs.get('async', False)
-
-        if cmd is None:
+        self.async = kwargs.get('async', False)
+
+        # check parameters
+        if self._is_parameters_ok():
+            # run the command
+            if not self.async:
+                p = subprocess.Popen(self.cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
+                (output, err) = p.communicate()
+                message = {
+                    "output": output,
+                    "returncode": p.returncode
+                }
+                self.say(message)
+
+            else:
+                async_shell = AsyncShell(cmd=self.cmd)
+                async_shell.start()
+
+    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
+        """
+        if self.cmd is None:
             raise MissingParameterException("cmd parameter required")
 
-        # run the command
-        if not async:
-            p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
-            (output, err) = p.communicate()
-            message = {
-                "output": output,
-                "returncode": p.returncode
-            }
-            self.say(message)
-
-        else:
-            async_shell = AsyncShell(cmd=cmd)
-            async_shell.start()
-
-
-
-
+        return True

+ 4 - 0
neurons/sleep/sleep.py

@@ -13,6 +13,10 @@ class Sleep(NeuronModule):
             time.sleep(self.seconds)
 
         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
+            """
             if self.seconds is None:
                 raise MissingParameterException("You must set a number of seconds as parameter")
 

+ 21 - 15
neurons/tasker_autoremote/tasker_autoremote.py

@@ -13,21 +13,27 @@ class Tasker_autoremote(NeuronModule):
         super(Tasker_autoremote, self).__init__(**kwargs)
 
         # check if parameters have been provided
-        key = kwargs.get('key', None)
-        message = kwargs.get('message', None)
-
-        if key is None:
+        self.key = kwargs.get('key', None)
+        self.message = kwargs.get('message', None)
+
+        # check parameters
+        if self._is_parameters_ok():
+            # create the payload
+            data = {'key': self.key,
+                    'message': self.message}
+            url = "https://autoremotejoaomgcd.appspot.com/sendmessage"
+            # post
+            r = requests.post(url, data=data)
+            logging.debug("Post to tasker automore response: %s" % r.status_code)
+
+    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
+        """
+        if self.key is None:
             raise MissingParameterException("key parameter required")
-
-        if message is None:
+        if self.message is None:
             raise MissingParameterException("message parameter required")
 
-        # create the payload
-        data = {'key': key,
-                'message': message}
-        url = "https://autoremotejoaomgcd.appspot.com/sendmessage"
-        # post
-        r = requests.post(url, data=data)
-        logging.debug("Post to tasker automore response: %s" % r.status_code)
-
-
+        return True

+ 31 - 22
neurons/twitter/Twitter.py

@@ -8,34 +8,43 @@ class Twitter(NeuronModule):
 
         super(Twitter, self).__init__(**kwargs)
 
-        consumer_key = kwargs.get('consumer_key', None)
-        consumer_secret = kwargs.get('consumer_secret', None)
-        access_token_key = kwargs.get('access_token_key', None)
-        access_token_secret = kwargs.get('access_token_secret', None)
-        tweet = kwargs.get('tweet', None)
-
-        if consumer_key is None:
+        self.consumer_key = kwargs.get('consumer_key', None)
+        self.consumer_secret = kwargs.get('consumer_secret', None)
+        self.access_token_key = kwargs.get('access_token_key', None)
+        self.access_token_secret = kwargs.get('access_token_secret', None)
+        self.tweet = kwargs.get('tweet', None)
+
+        # check parameters
+        if self._is_parameters_ok():
+            api = twitter.Api(consumer_key=self.consumer_key,
+                              consumer_secret=self.consumer_secret,
+                              access_token_key=self.access_token_key,
+                              access_token_secret=self.access_token_secret)
+
+            status = api.PostUpdate(self.tweet)
+            message = {
+                "tweet" : status.text
+            }
+
+            self.say(message)
+
+    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
+        """
+        if self.consumer_key is None:
             raise InvalidParameterException("Twitter needs a consumer_key")
-        if consumer_secret is None:
+        if self.consumer_secret is None:
             raise InvalidParameterException("Twitter needs a consumer_secret")
-        if access_token_key is None:
+        if self.access_token_key is None:
             raise InvalidParameterException("Twitter needs an access_token_key")
-        if access_token_secret is None:
+        if self.access_token_secret is None:
             raise InvalidParameterException("Twitter needs and access_token_secret")
-        if tweet is None:
+        if self.tweet is None:
             raise InvalidParameterException("You need to provide something to tweet !")
 
-        api = twitter.Api(consumer_key=consumer_key,
-                          consumer_secret=consumer_secret,
-                          access_token_key=access_token_key,
-                          access_token_secret=access_token_secret)
-
-        status = api.PostUpdate(tweet)
-        message = {
-            "tweet" : status.text
-        }
-
-        self.say(message)
+        return True
 
 
 

+ 28 - 19
neurons/wake_on_lan/Wake_on_lan.py

@@ -12,25 +12,34 @@ class Wake_on_lan(NeuronModule):
     def __init__(self, **kwargs):
         super(Wake_on_lan, self).__init__(**kwargs)
 
-        mac_address = kwargs.get('mac_address', None)
-        broadcast_address = kwargs.get('broadcast_address', '255.255.255.255')
-        port = kwargs.get('port', 9)
-
+        self.mac_address = kwargs.get('mac_address', None)
+        self.broadcast_address = kwargs.get('broadcast_address', '255.255.255.255')
+        self.port = kwargs.get('port', 9)
+
+        # check parameters
+        if self._is_parameters_ok():
+            # convert to unicode for testing
+            broadcast_address_unicode = self.broadcast_address.decode('utf-8')
+            # check the ip address is a valid one
+            ipaddress.ip_address(broadcast_address_unicode)
+
+            logger.debug("Call Wake_on_lan_neuron with parameters: mac_address: %s, broadcast_address: %s, port: %s"
+                         % (self.mac_address, self.broadcast_address, self.port))
+
+            # send the magic packet, the mac address format will be check by the lib
+            # wol.send_magic_packet(mac_address, ip_address=broadcast_address, port=port)
+
+    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
+        """
         # check we provide a mac address
-        if mac_address is None:
+        if self.mac_address is None:
             raise MissingParameterException("mac_address parameter required")
+            # check the port
+        if type(self.port) is not int:
+            raise InvalidParameterException(
+                "port argument must be an integer. Remove quotes in your configuration.")
 
-        # convert to unicode for testing
-        broadcast_address_unicode = broadcast_address.decode('utf-8')
-        # check the ip address is a valid one
-        ipaddress.ip_address(broadcast_address_unicode)
-
-        # check the port
-        if type(port) is not int:
-            raise InvalidParameterException("port argument must be an integer. Remove quotes in your configuration.")
-
-        logger.debug("Call Wake_on_lan_neuron with parameters: mac_address: %s, broadcast_address: %s, port: %s"
-                     % (mac_address, broadcast_address, port))
-
-        # send the magic packet, the mac address format will be check by the lib
-        # wol.send_magic_packet(mac_address, ip_address=broadcast_address, port=port)
+        return True