From: Olivier Matz Date: Mon, 23 Jan 2023 14:31:34 +0000 (+0100) Subject: tmp X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=bc30d7a50e5950225ed6f31e827f84e237547e63;p=imapami.git tmp --- diff --git a/imapami/__init__.py b/imapami/__init__.py index b817646..4854210 100644 --- a/imapami/__init__.py +++ b/imapami/__init__.py @@ -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 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() diff --git a/imapami/actions.py b/imapami/actions.py index 67cda16..4991f07 100644 --- a/imapami/actions.py +++ b/imapami/actions.py @@ -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): diff --git a/imapami/conditions.py b/imapami/conditions.py index 3836b75..1572294 100644 --- a/imapami/conditions.py +++ b/imapami/conditions.py @@ -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): diff --git a/imapami/rules.py b/imapami/rules.py index 0b17eaa..fc51da9 100644 --- a/imapami/rules.py +++ b/imapami/rules.py @@ -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",