summaryrefslogtreecommitdiff
path: root/clab
blob: daa2dbc5667882a4424c26961e14dc8b3c00c5ab (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/python
#
# Copyright 2013  Lars Wirzenius
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import cliapp
import logging
import os
import sys
import yaml


class Entry(object):

    def __init__(self, parsed_yaml):
        assert type(parsed_yaml) is dict, repr(parsed_yaml)
        self._dict = parsed_yaml
    
    def as_yaml(self):
        return yaml.safe_dump(self._dict, default_flow_style=False)

    def get_single(self, key, default):
        names = self.get_subdict(key)
        if not names:
            return default
        keys = sorted(names.keys())
        return names[keys[0]]
    
    def get_subdict(self, key):
        if key in self._dict:
            v = self._dict[key]
            if type(v) is dict:
                return v
            return { '': v }
        return {}


class AddressBook(object):

    def __init__(self):
        self.entries = []
    
    def find_yaml_files(self, database_root):
        for dirname, subdirs, basenames in os.walk(database_root):
            subdirs.sort()
            for basename in sorted(basenames):
                if basename.endswith('.yaml'):
                    yield os.path.join(dirname, basename)
    
    def add_from_database(self, database_root):
        logging.info('Adding from database %s' % database_root)
        for yaml_filename in self.find_yaml_files(database_root):
            self.add_from_file(yaml_filename)

    def add_from_file(self, filename):
        logging.info('Adding from file %s' % filename)
        with open(filename) as f:
            parsed_yaml = yaml.safe_load(f)
        if type(parsed_yaml) is list:
            for parsed_entry in parsed_yaml:
                entry = Entry(parsed_entry)
                self.entries.append(entry)
        else:
            entry = Entry(parsed_yaml)
            self.entries.append(entry)

    def find(self, patterns):
        return [e for e in self.entries if self.matches(e, patterns)]

    def matches(self, entry, patterns):
        s = entry.as_yaml().lower()
        return any(p.lower() in s for p in patterns)


class CommandLineAddressBook(cliapp.Application):

    def add_settings(self):
        self.settings.string_list(
            ['database', 'db', 'd'],
            'add DIR to list of databases to use',
            metavar='DIR',
            default=[os.path.expanduser('~/.local/share/clab')])

    def load_address_book(self):
        book = AddressBook()
        for database in self.settings['database']:
            book.add_from_database(database)
        return book

    def cmd_list(self, args):
        book = self.load_address_book()
        for entry in book.entries:
            self.output.write(entry.as_yaml() + '\n')

    def cmd_find(self, args):
        book = self.load_address_book()
        for entry in book.find(args):
            self.output.write(entry.as_yaml() + '\n')

    def cmd_mutt_query(self, args):
        if len(args) != 1:
            raise cliapp.AppException(
                'mutt-query requires exactly one argument')

        book = self.load_address_book()
        entries = book.find(args)
        if not entries:
            self.output.write('No matches\n')
            sys.exit(1)
        self.output.write('clab found matches:\n')
        for entry in entries:
            name = entry.get_single('name', '')
            emails = entry.get_subdict('email')
            for email in emails:
                n = name.encode('utf-8')
                self.output.write('%s\t%s\n' % (emails[email], n))


CommandLineAddressBook().run()