summaryrefslogtreecommitdiff
path: root/alot/init.py
blob: 16d4d870b27821fd5481a3a86b1a44b8dc4c7f6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
import sys
import argparse
import logging
import os

import settings
from account import AccountManager
from db import DBManager
from ui import UI
import alot.commands as commands
from commands import *
from alot.commands import CommandParseError


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('-c', dest='configfile',
                        default=None,
                        help='alot\'s config file')
    parser.add_argument('-n', dest='notmuchconfigfile',
                        default='~/.notmuch-config',
                        help='notmuch\'s config file')
    parser.add_argument('-C', dest='colours',
                        type=int,
                        choices=[1, 16, 256],
                        help='colour mode')
    parser.add_argument('-r', dest='read_only',
                        action='store_true',
                        help='open db in read only mode')
    parser.add_argument('-p', dest='db_path',
                        help='path to notmuch index')
    parser.add_argument('-d', dest='debug_level',
                        default='info',
                        choices=['debug', 'info', 'warning', 'error'],
                        help='debug level')
    parser.add_argument('-l', dest='logfile',
                        default='/dev/null',
                        help='logfile')
    parser.add_argument('command', nargs='?',
                        default='',
                        help='initial command')
    return parser.parse_args()


def main():
    # interpret cml arguments
    args = parse_args()

    # locate and read config file
    configfiles = [
        os.path.join(os.environ.get('XDG_CONFIG_HOME',
                                    os.path.expanduser('~/.config')),
                     'alot', 'config'),
        os.path.expanduser('~/.alot.rc'),
    ]
    if args.configfile:
        expanded_path = os.path.expanduser(args.configfile)
        if not os.path.exists(expanded_path):
            sys.exit('File %s does not exist' % expanded_path)
        configfiles.insert(0, expanded_path)

    for configfilename in configfiles:
        if os.path.exists(configfilename):
            settings.config.read(configfilename)
            break  # use only the first

    # read notmuch config
    notmuchfile = os.path.expanduser(args.notmuchconfigfile)
    settings.notmuchconfig.read(notmuchfile)
    settings.hooks.setup(settings.config.get('general', 'hooksfile'))

    # setup logging
    numeric_loglevel = getattr(logging, args.debug_level.upper(), None)
    logfilename = os.path.expanduser(args.logfile)
    logging.basicConfig(level=numeric_loglevel, filename=logfilename)
    logger = logging.getLogger()

    #logger.debug(commands.COMMANDS)
    #accountman
    aman = AccountManager(settings.config)

    # get ourselves a database manager
    dbman = DBManager(path=args.db_path, ro=args.read_only)

    # get initial searchstring
    try:
        if args.command != '':
            cmd = commands.commandfactory(args.command, 'global')
        else:
            default_commandline = settings.config.get('general',
                                                      'initial_command')
            cmd = commands.commandfactory(default_commandline, 'global')
    except CommandParseError, e:
        sys.exit(e)

    # set up and start interface
    UI(dbman,
       logger,
       aman,
       cmd,
       args.colours,
    )

if __name__ == "__main__":
    main()