Completed
Push — main ( 6f109e...210312 )
by Alexander
04:04
created

src.mailbox_cli   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 89
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 6

2 Functions

Rating   Name   Duplication   Size   Complexity  
B handle_arguments() 0 52 1
B main() 0 32 5
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
4
"""
5
Module to download and to detach/strip/remove attachments
6
from e-mails on IMAP servers.
7
"""
8
9
from __future__ import print_function
10
11
import argparse
12
import logging
13
import os
14
15
from src.mailbox_imap import MailboxCleanerIMAP
16
17
18
__author__ = "Alexander Willner"
19
__copyright__ = "Copyright 2020, Alexander Willner"
20
__credits__ = ["github.com/guido4000",
21
               "github.com/halteproblem", "github.com/jamesridgway"]
22
__license__ = "MIT"
23
__version__ = "1.0.0"
24
__maintainer__ = "Alexander Willner"
25
__email__ = "[email protected]"
26
__status__ = "Development"
27
28
29
def handle_arguments() -> argparse.ArgumentParser:
30
    """Provide CLI handler for application."""
31
32
    parser = argparse.ArgumentParser()
33
    parser.add_argument("-a", "--all",
34
                        help="iterate over all folders",
35
                        action='store_true')
36
    parser.add_argument("-d", "--detach",
37
                        help="remove attachments",
38
                        action='store_true')
39
    parser.add_argument("-k", "--skip-download",
40
                        help="don't download attachments",
41
                        action='store_true')
42
    parser.add_argument("-c", "--reset-cache",
43
                        help="reset cache",
44
                        action='store_true')
45
    parser.add_argument("-r", "--read-only",
46
                        help="read-only mode for the imap server",
47
                        action='store_true')
48
    parser.add_argument("-m", "--max-size",
49
                        help="max attachment size in KB",
50
                        default=200, type=int)
51
    parser.add_argument("-f", "--folder",
52
                        help="imap folder to process", default="Inbox")
53
    parser.add_argument("-l", "--upload",
54
                        help="local folder with messages to upload")
55
    parser.add_argument("-t", "--target",
56
                        help="download attachments to this local folder",
57
                        default="attachments")
58
    parser.add_argument("-s", "--server", help="imap server", required=True)
59
    parser.add_argument("-u", "--user", help="imap user", required=True)
60
    parser.add_argument("-o", "--port", help="imap port", required=False,
61
                        type=int)
62
    parser.add_argument("-p", "--password", help="imap user", required=True)
63
    parser.add_argument(
64
        "-v",
65
        "--verbose",
66
        action="count",
67
        default=0,
68
        dest="verbosity",
69
        help="be more verbose (-v, -vv)")
70
    parser.add_argument(
71
        "--version",
72
        action="version",
73
        version="%(prog)s (version {version})".format(version=__version__))
74
75
    args = parser.parse_args()
76
77
    logging.basicConfig(level=logging.WARNING - args.verbosity * 10,
78
                        format="%(message)s")
79
80
    return args
81
82
83
def main():
84
    """Setup and run remover."""
85
86
    args = handle_arguments()
87
    args.target = os.path.expanduser(
88
        args.target) if args.target is not None else None
89
    args.upload = os.path.expanduser(
90
        args.upload) if args.upload is not None else None
91
    imap = MailboxCleanerIMAP(args)
92
93
    # todo: repeat here a couple of times  pylint: disable=W0511
94
    try:
95
        imap.login()
96
        logging.warning('Server\t\t: %s@%s', args.user, args.server)
97
        logging.warning('Read Only\t: %s', args.read_only)
98
        logging.warning('Detach\t\t: %s', args.detach)
99
        logging.warning('Cache Enabled\t: %s', not args.reset_cache)
100
        logging.warning('Download\t: %s', not args.skip_download)
101
        logging.warning('Max Size\t: %s KB', args.max_size)
102
        logging.warning('Target\t\t: %s', args.target)
103
        logging.warning('Upload\t\t: %s', args.upload)
104
        logging.warning('All Folders\t: %s', args.all)
105
106
        if args.upload:
107
            imap.process_directory()
108
        else:
109
            imap.process_folders()
110
    except KeyboardInterrupt as error:
111
        raise SystemExit('\nCancelling...') from error
112
    finally:
113
        imap.cleanup()
114
        imap.logout()
115
116
117
if __name__ == '__main__':
118
    main()
119