]> git.droids-corp.org - imapami.git/commitdiff
tmp
authorOlivier Matz <olivier.matz@6wind.com>
Mon, 23 Jan 2023 14:31:34 +0000 (15:31 +0100)
committerOlivier Matz <olivier.matz@6wind.com>
Mon, 23 Jan 2023 14:31:34 +0000 (15:31 +0100)
imapami/__init__.py
imapami/actions.py
imapami/conditions.py
imapami/rules.py

index b817646791fdbb2f8641792397c0942d41d9629d..48542104d9f2678f4079909fae5d40801c842caf 100644 (file)
@@ -36,9 +36,11 @@ import getpass
 import imaplib
 import inspect
 import logging
+import os
 import pydoc
 import re
 import sys
+import time
 import yaml
 
 _LOGLEVELS = [logging.CRITICAL, logging.ERROR, logging.WARNING,
@@ -88,7 +90,7 @@ class Imapami(object):
     and set them in rule actions.
 
     """
-    def __init__(self, config, loglevel=3):
+    def __init__(self, config, loglevel=3, password=None, update_logger=True):
         """
         Create a Imapami object.
 
@@ -102,7 +104,7 @@ class Imapami(object):
                        "port": None,
                        "ssl": True,
                        "login": None,
-                       "password": None,
+                       "password": password,
                        "logfile": None,
                        "loglevel": 3,
                        "inbox": "INBOX",
@@ -111,7 +113,8 @@ class Imapami(object):
         self.variables = {}
         self.logger = self._get_logger(loglevel)
         self._load_config(config)
-        self._update_logger()
+        if update_logger:
+            self._update_logger()
         self.uidnext = {}
 
     def _get_logger(self, loglevel):
@@ -203,6 +206,7 @@ class Imapami(object):
         password = self.config.get("password")
         if password is None:
             password = getpass.getpass()
+            self.config["password"] = password
         server = self.config.get("server")
         port = self.config.get("port")
         if port is None:
@@ -323,6 +327,11 @@ def main():
     parser.add_argument(
         '-H', '--config-help', dest='config_help', action='store_true',
         default=False, help='Show help about configuration file')
+    parser.add_argument(
+        '-r', '--refresh',
+        metavar='secs',
+        help='Call in a loop, with a pause of <secs> seconds.',
+        type=int)
     parser.add_argument(
         '-d', '--debug',
         help='Console debug level, from 0 (no output) to 4 (debug).',
@@ -338,15 +347,31 @@ def main():
         parser.print_help()
         sys.exit(1)
 
-    p = Imapami(open(args.config).read(), args.debug)
+    password = os.getenv("IMAPAMI_PASSWORD")
+    p = Imapami(open(args.config).read(), args.debug, password=password)
     if args.check == True:
         sys.stderr.write('Configuration parsing ok\n')
         sys.exit(0)
 
-    p.connect()
-    p.get_uidnext()
-    p.process_rules()
-    p.close()
+    # XXX does not work :(
+    if args.refresh:
+        while True:
+            try:
+                p.connect()
+                p.get_uidnext()
+                p.process_rules()
+                p.close()
+            except:
+                p.logger.error("Exception during processing", exc_info=True)
+            time.sleep(args.refresh)
+            # XXX why needed?
+            p = Imapami(open(args.config).read(), args.debug,
+                        password=p.config.get('password'), update_logger=False)
+    else:
+        p.connect()
+        p.get_uidnext()
+        p.process_rules()
+        p.close()
 
 if __name__ == '__main__':
     main()
index 67cda16fb7083588c255da0a14a8b4d9f847ea22..4991f07f037f66a5f61efcf4a4b2d0c960afd935 100644 (file)
@@ -655,7 +655,7 @@ def new(config):
     if len(config) != 1:
         raise ValueError("the action config must be a dictionary whose only "
                          "key is the action name")
-    action_name = config.keys()[0]
+    action_name = next(iter(config.keys()))
     argslist = []
     argsdict = {}
     if isinstance(config[action_name], list):
index 3836b75ae56b13290082da73d9813db16cee0009..157229435937e1a9772b7387e6eb00b409ea3d34 100644 (file)
@@ -770,7 +770,7 @@ def new(config):
     if len(config) != 1:
         raise ValueError("the condition config must be a dictionary whose only "
                          "key is the condition name")
-    cond_name = config.keys()[0]
+    cond_name = next(iter(config.keys()))
     argslist = []
     argsdict = {}
     if isinstance(config[cond_name], list):
index 0b17eaa5c3a0af7402ba1c8da37bef2979883a0c..fc51da97100eb84c7fd4a0d1c46569c323aab38d 100644 (file)
@@ -220,7 +220,6 @@ class ImapamiRule(object):
             mail_data = {'item': item, 'inbox': inbox}
             if parts != []:
                 resp, data = ami.imap.uid("FETCH", item, parts_str)
-                print resp, data
                 if resp != 'OK':
                     ami.logger.warning(
                         "search failed: server response = %s, skip item",