Selaa lähdekoodia

[Doc] Fix Gmail rename error + fix gmail_checker neuron

monf 8 vuotta sitten
vanhempi
commit
eea10dfd00

+ 1 - 1
Docs/neuron_list.md

@@ -5,7 +5,7 @@ A neuron is a module that will perform some actions attached to an order. You ca
 | Name                                               | Description                                                                             | Used to sort      |
 |----------------------------------------------------|-----------------------------------------------------------------------------------------|-------------------|
 | [ansible_task](../neurons/ansible_task/)           | Run an ansible playbook                                                                 | ansible_task      |
-| [gmail](../neurons/gmail/)                         | Get the number of unread email and their subjects from a gmail account                  | gmail             |
+| [gmail_checker](../neurons/gmail_checker/)         | Get the number of unread email and their subjects from a gmail account                  | gmail_checker     |
 | [kill_switch](../neurons/kill_switch/)             | Stop Kalliope process                                                                   | kill_switch       |
 | [neurotransmitter](../neurons/neurotransmitter/)   | Link synapse together                                                                   | neurotransmitter  |
 | [push_message](../neurons/push_message/)           | Send a push message to a remote device like Android/iOS/Windows Phone or Chrome browser | push_message      |

+ 1 - 1
brain.yml

@@ -1,7 +1,7 @@
 ---
 - includes:
   - brains/ansible_playbook.yml
-  - brains/gmail.yml
+  - brains/gmail_checker.yml
   - brains/kill_switch.yml
   - brains/openweathermap.yml
   - brains/push_message.yml

+ 1 - 1
brains/gmail.yml → brains/gmail_checker.yml

@@ -2,7 +2,7 @@
 
   - name: "check-email"
     neurons:
-      - gmail:
+      - gmail_checker:
           username: "me@gmail.com"
           password: "my_password"
           file_template: fr_gmail.j2

+ 1 - 1
neurons/__init__.py

@@ -5,7 +5,7 @@ from say import Say
 from script import Script
 from sleep import Sleep
 from systemdate import Systemdate
-from gmail import Gmail
+from gmail_checker import Gmail_checker
 from push_message import Push_message
 from openweathermap import Openweathermap
 from tasker_autoremote import Tasker_autoremote

+ 0 - 1
neurons/gmail/__init__.py

@@ -1 +0,0 @@
-from gmail import Gmail

+ 0 - 72
neurons/gmail/gmail.py

@@ -1,72 +0,0 @@
-# -*- coding: utf-8 -*-
-import logging
-
-from gmail import Gmail as Gmail_lib
-from email.header import decode_header
-from core.NeuronModule import NeuronModule, MissingParameterException
-
-logging.basicConfig()
-logger = logging.getLogger("kalliope")
-
-
-class Gmail(NeuronModule):
-    def __init__(self, **kwargs):
-        super(Gmail, self).__init__(**kwargs)
-
-        # check if parameters have been provided
-        username = kwargs.get('username', None)
-        password = kwargs.get('password', None)
-
-        if username is None:
-            raise MissingParameterException("Username parameter required")
-
-        if password is None:
-            raise MissingParameterException("Password parameter required")
-
-        # prepare a returned dict
-        returned_dict = dict()
-
-        g = Gmail_lib()
-        g.login(username, password)
-
-        # check if login succeed
-        logging.debug("Gmail loggin ok: %s" % g.logged_in)  # Should be True, AuthenticationError if login fails
-
-        # get unread mail
-        unread = g.inbox().mail(unread=True)
-
-        returned_dict["unread"] = len(unread)
-
-        if len(unread) > 0:
-            # add a list of subject
-            subject_list = list()
-            for email in unread:
-                email.fetch()
-                encoded_subject = email.subject
-                subject = self._parse_subject(encoded_subject)
-                subject_list.append(subject)
-
-            returned_dict["subjects"] = subject_list
-
-        logger.debug("gmail neuron returned dict: %s" % str(returned_dict))
-        # logout of gmail
-        g.logout()
-        self.say(returned_dict)
-
-    def _parse_subject(self, encoded_subject):
-        dh = decode_header(encoded_subject)
-
-        return ''.join([self.try_parse(t[0], t[1]) for t in dh])
-
-    @staticmethod
-    def try_parse(header, encoding):
-
-        if encoding is None:
-            encoding = 'ASCII'
-        try:
-            return unicode(header, encoding)
-        except UnicodeDecodeError:
-            try:
-                return unicode(header, 'ISO-8859-1')
-            except UnicodeDecodeError:
-                return unicode(header, 'UTF-8')

+ 2 - 2
neurons/gmail/README.md → neurons/gmail_checker/README.md

@@ -1,4 +1,4 @@
-# gmail
+# gmail_checker
 
 ## Synopsis
 
@@ -25,7 +25,7 @@ Simple example :
 ```
   - name: "check-email"
     neurons:
-      - gmail:
+      - gmail_checker:
           username: "me@gmail.com"
           password: "my_password"
           say_template: 

+ 1 - 0
neurons/gmail_checker/__init__.py

@@ -0,0 +1 @@
+from gmail_checker import Gmail_checker

+ 82 - 0
neurons/gmail_checker/gmail_checker.py

@@ -0,0 +1,82 @@
+# -*- coding: utf-8 -*-
+import logging
+
+from gmail import Gmail
+from email.header import decode_header
+from core.NeuronModule import NeuronModule, MissingParameterException
+
+logging.basicConfig()
+logger = logging.getLogger("kalliope")
+
+
+class Gmail_checker(NeuronModule):
+    def __init__(self, **kwargs):
+        super(Gmail_checker, self).__init__(**kwargs)
+
+        self.username = kwargs.get('username', None)
+        self.password = kwargs.get('password', None)
+
+        # check if parameters have been provided
+        if self._is_parameters_ok():
+
+            # prepare a returned dict
+            returned_dict = dict()
+
+            g = Gmail()
+            g.login(self.username, self.password)
+
+            # check if login succeed
+            logging.debug("Gmail loggin ok: %s" % g.logged_in)  # Should be True, AuthenticationError if login fails
+
+            # get unread mail
+            unread = g.inbox().mail(unread=True)
+
+            returned_dict["unread"] = len(unread)
+
+            if len(unread) > 0:
+                # add a list of subject
+                subject_list = list()
+                for email in unread:
+                    email.fetch()
+                    encoded_subject = email.subject
+                    subject = self._parse_subject(encoded_subject)
+                    subject_list.append(subject)
+
+                returned_dict["subjects"] = subject_list
+
+            logger.debug("gmail neuron returned dict: %s" % str(returned_dict))
+
+            # logout of gmail
+            g.logout()
+            self.say(returned_dict)
+
+    def _parse_subject(self, encoded_subject):
+        dh = decode_header(encoded_subject)
+
+        return ''.join([self.try_parse(t[0], t[1]) for t in dh])
+
+    @staticmethod
+    def try_parse(header, encoding):
+
+        if encoding is None:
+            encoding = 'ASCII'
+        try:
+            return unicode(header, encoding)
+        except UnicodeDecodeError:
+            try:
+                return unicode(header, 'ISO-8859-1')
+            except UnicodeDecodeError:
+                return unicode(header, 'UTF-8')
+
+    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.username is None:
+            raise MissingParameterException("Username parameter required")
+
+        if self.password is None:
+            raise MissingParameterException("Password parameter required")
+
+        return True

+ 1 - 1
test.py

@@ -25,7 +25,7 @@ logger.setLevel(logging.DEBUG)
 #
 brain = BrainLoader.get_brain()
 
-order = "lance le script"
+order = "est-ce que j'ai des emails"
 
 oa = OrderAnalyser(order=order, brain=brain)